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.
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.
相关文章推荐
- MFC三种不同方式实现图形的保存和重绘---方法一:通过兼容DC(CompatibleDC)的方式
- MFC三种不同方式实现图形的保存和重绘---方法一:通过兼容DC(CompatibleDC)的方式
- 【框架-MFC】关于CFileDialog保存文件获取扩展名(后缀名)
- MFC三种不同方式实现图形的保存和重绘---方法二: 运用CMetaFileDC
- VC++兼容DC 和兼容位图保存位图资源
- 关于兼容dc 兼容bmp(用来得到dc数值)
- 关于Application 保存UI状态的用法
- CreateCompatibleBitmap,关于兼容DC的创建与资源回收
- MFC三种不同方式实现图形的保存和重绘---方法二: 运用CMetaFileDC
- 【转】关于MFC程序中ScrollBar的用法简单介绍
- 关于Session的保存用户信息的用法。
- 关于MFC一些小用法
- MFC 双缓冲技术和兼容DC
- 一,关于MFC里面的EDIT Control控件的用法
- MFC 双缓冲技术和兼容DC
- 关于Excel保存为csv文件时提示“可能含有与CSV(逗号分隔)不兼容的功能”
- MFC中的图象保存一般有三种方法:
- MFC TeeChart用法整理 四(保存图片)
- 关于MFC中CString的用法
- 关于MFC的EDIT控件获取输入字符操作的用法