您的位置:首页 > 其它

MFC中的图象保存--关于兼容DC和CMetaFileDc的用法

2007-08-14 22:20 260 查看
MFC中的图象保存一般有三种方法:
1、把图象中的各个图象的完整相关信息(点坐标,图象类型)以一个结构体保存,将其指针加入到CPtrArray类对象中,然后在OnDraw函数中取出相关信息绘图。

2、使用兼容DC:先将当前图象贴到一个内存CBitmap对象块中,然后再用兼容DC将其SelectObject,OnDraw函数中在拷贝兼容DC的内容到当前的绘图DC中完成重绘操作。
//保存当前位图的操作
m_dcCmp.CreateCompatibleDC(&dc); //创建当前绘图DC的兼容DC
CRect rect;
GetClientRect(&rect);
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height()); //将当前绘图DC位图信息头贴到内存bitmap中
m_dcCmp.SelectObject(&bitmap); //在兼容DC中选入内存bitmap
m_dcCmp.BitBlt(0, 0, rect.Width(), rect.Height(), &dc, 0, 0, SRCCOPY); //将当前绘图DC的眼色表和象素数据块拷贝到兼容DC
//OnDraw函数中重绘位图的操作:
CRect rect;
GetClientRect(&rect);
pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &m_dcCmp, 0, 0, SRCCOPY); //将兼容DC的内容拷贝到当前绘图DC输出

3、使用CMetaFileDc类:首先创建一个CMetaFileDC对象来关联一个文件或者一个内存块,然后使用这个对象的绘图函数来绘图,调用该对象的Close函数返回一个HMETAFILE句柄,使用CDC的PlayMetaFile函数完成绘图操作,最后::DeleteMetaFile(hmetafile)释放这个句柄。
//在OnDraw函数中:
HMETAFILE hmetafile;
hmetafile = m_dcMetaFile.Close(); //返回该CMetaFileDC对象的句柄
pDC->PlayMetaFile(hmetafile); //使用绘图DC绘图
m_dcMetaFile.Create(); //用该CMetaFileDC对象关联一个新的内存块来保存下次的绘图操作
m_dcMetaFile.PlayMetaFile(hmetafile); //将上次的绘图操作保存到该CMetaFileDC对象关联的新的内存块中
::DeleteMetaFile(hmetafile); //释放该句柄

以下摘录自msdn:

A Windows metafile contains a sequence of graphics device interface (GDI) commands that you can replay to create a desired image or text.

To implement a Windows metafile, first create a CMetaFileDC object. Invoke the CMetaFileDC constructor, then call the Create member function, which creates a Windows metafile device context and attaches it to the CMetaFileDC object.

Next send the CMetaFileDC object the sequence of CDC GDI commands that you intend for it to replay. Only those GDI commands that create output, such as MoveTo and LineTo, can be used.

After you have sent the desired commands to the metafile, call the Close member function, which closes the metafile device contexts and returns a metafile handle. Then dispose of the CMetaFileDC object.

CDC::PlayMetaFile can then use the metafile handle to play the metafile repeatedly. The metafile can also be manipulated by Windows functions such as

CopyMetaFile, which copies a metafile to disk.

When the metafile is no longer needed, delete it from memory with the

DeleteMetaFile Windows function.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: