Click here to Skip to main content
15,075,806 members
Articles / Desktop Programming / MFC
Article
Posted 27 Apr 2004

Stats

130.6K views
27 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
3.10/5 (10 votes)
27 Apr 2004
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

TeleStar
China China
I am now a faculty in Engineering. But I am also a programmer who use VC from VC1.5 to VC8.0. However, now I prefer to use C# and VB.Net in my job.

Comments and Discussions

 
SuggestionThe following code works and looks more simple Pin
Miller Chan12-Feb-16 15:42
MemberMiller Chan12-Feb-16 15:42 
GeneralImage->Save Pin
Ziggy Short21-Feb-05 6:31
MemberZiggy Short21-Feb-05 6:31 
GeneralRe: Image->Save Pin
TeleStar22-Feb-05 5:38
MemberTeleStar22-Feb-05 5:38 
QuestionHow to save GDI+ bmp file as PDF File Format. Pin
pubududilena13-Jul-04 19:52
Memberpubududilena13-Jul-04 19:52 
AnswerRe: How to save GDI+ bmp file as PDF File Format. Pin
Miller Chan12-Feb-16 15:27
MemberMiller Chan12-Feb-16 15:27 
QuestionHow to save GDI+ bmp as CMYK color GDI+ bmp Pin
pubududilena27-May-04 23:42
Memberpubududilena27-May-04 23:42 
AnswerRe: How to save GDI+ bmp as CMYK color GDI+ bmp Pin
sanull13-Mar-06 23:28
Membersanull13-Mar-06 23:28 
GeneralDownload GDI+ version 1.1 Pin
pubududilena23-May-04 17:47
Memberpubududilena23-May-04 17:47 
QuestionHow to save GDI + bmp Image as Interlaced PNG file Format. Pin
pubududilena20-May-04 0:49
Memberpubududilena20-May-04 0:49 
AnswerI think you need libpng Pin
TeleStar20-May-04 7:07
MemberTeleStar20-May-04 7:07 
QuestionHow do I save Images as EPS File Format Pin
pubududilena28-Apr-04 23:54
Memberpubududilena28-Apr-04 23:54 
AnswerEasy, please read Jonathan de Halleux's nice article "Crossing the bridge between Ghostscript and GDI+" Pin
TeleStar29-Apr-04 6:34
MemberTeleStar29-Apr-04 6:34 
GeneralRe: Easy, please read Jonathan de Halleux's nice article "Crossing the bridge between Ghostscript and GDI+" Pin
pubududilena29-Apr-04 17:09
Memberpubududilena29-Apr-04 17:09 
Generaloops, for the inverse rendering Pin
TeleStar30-Apr-04 5:57
MemberTeleStar30-Apr-04 5:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.