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

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.

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值