How to use GDI+ to save image in WMF, EXIF or EMF format






2.50/5 (11 votes)
Apr 28, 2004

140545
An article on GDI+ and how to save image in WMF, EXIF or EMF format.
Introduction
When I use the Image::Save()
function in GDI+ to save an image as BMP, GIF, TIFF, PNG or JPEG, the program works very well. But it fails to do so if I want to save an image as WMF, EXIF or EMF.
The reason of such a case has been well explained in MSDN.
However, I also found that in C#, we could use Image::Save()
function to save an image as WMF, EXIF or EMF format, with no pains.
Finally, Feng Yuan helped me out of this problem. Thanks a lot for him.
The solution
Actually, the solution code is quite simple. First, construct a Metafile object and then draw graphics on it. For example, we can have:
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());
Then, we can find the resulting image on driver C:. Simple, isn't it? Here, ToWChar()
is a simple function to transform a CString
object to WCHAR*
. It can be written as:
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; }
History
- Initial release 2004-04-27.