65.9K
CodeProject 正在变化。 阅读更多。
Home

如何使用 GDI+ 以 WMF、EXIF 或 EMF 格式保存图像

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.50/5 (11投票s)

2004年4月28日

viewsIcon

143594

一篇关于 GDI+ 以及如何以 WMF、EXIF 或 EMF 格式保存图像的文章。

引言

当我使用 GDI+ 中的 Image::Save() 函数以 BMP、GIF、TIFF、PNG 或 JPEG 格式保存图像时,程序运行良好。但如果我想将其保存为 WMF、EXIF 或 EMF 格式时,则会失败。

这种原因在 MSDN 中已经得到了很好的解释。

然而,我也发现,在 C# 中,我们可以使用 Image::Save() 函数以 WMF、EXIF 或 EMF 格式保存图像,而不会遇到任何问题。

最终,冯远 帮助我解决了这个问题。非常感谢他。

解决方案

实际上,解决方案代码非常简单。首先,构造一个 Metafile 对象,然后在它上面绘制图形。例如,我们可以有

Bitmap* m_pBitmap;
...
// do some paintings on m_pBitmap
...
CString sFileName = "c:\\1.emf";
// you can use "c:\\1.exi" or "c:\\1.wmf" here,
// and you can also build it as IStream
CDC *cdc = GetDC();
Metafile *metafile = 
         new Metafile(ToWChar(sFileName.GetBuffer(sFileName.GetLength())), 
         cdc->m_hDC);
Graphics graphics(metafile);
graphics.DrawImage(m_pBitmap, 0, 0, m_pBitmap->GetWidth(), 
                                  m_pBitmap->GetHeight());

然后,我们可以在驱动器 C: 上找到生成的图像。很简单,不是吗?这里,ToWChar() 是一个简单的函数,用于将 CString 对象转换为 WCHAR*。它可以写成:

static WCHAR* ToWChar(char * str) 
{
  // in GDI+, all the parameters about the symbol are WCHAR type
  // this funciton is a tranformation function

  static WCHAR buffer[1024];
  wcsset(buffer,0);
  MultiByteToWideChar(CP_ACP,0,str,strlen(str),buffer,1024);
  return buffer;
}

历史

  • 初始发布日期:2004-04-27。
© . All rights reserved.