Click here to Skip to main content
Click here to Skip to main content

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

By , 27 Apr 2004
 

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

About the Author

TeleStar
China China
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralImage->SavememberZiggy Short21 Feb '05 - 6:31 
I use Image to convert between many file formats. Only trouble is, my background is turned black. How can I make my background white?
GeneralRe: Image->SavememberAI200422 Feb '05 - 5:38 
Please see the discussions at the following address
 
http://www.dotnet247.com/247reference/msgs/7/36972.aspx
QuestionHow to save GDI+ bmp file as PDF File Format.susspubba13 Jul '04 - 19:52 
Hi everybody,
I want to export GDI+ bmp files as PDF File format.
I have created GDI+ bmp Objects like this.
Bitmap bmp7(L"myImage.jpg");
 
Now I want to save this bmp7 Object as PDF File format. u please help me if any one know this.
Thanks.
QuestionHow to save GDI+ bmp as CMYK color GDI+ bmpsusspubba27 May '04 - 23:42 
hi
normally GDI+ images are saved as RGB Color Format. But I want to save GDI+ bmp with CMYK Color.
If any one know it please help me.Frown | :(
AnswerRe: How to save GDI+ bmp as CMYK color GDI+ bmpmembersanull13 Mar '06 - 23:28 
Hi,
 
I'm having the same problem, we use gdi:graphics and CImage to work with bitmaps, at time of save as you said they are saved in rgb, I want to know if you could solve this problem.
 
Thanks, Laia
GeneralDownload GDI+ version 1.1susspubba23 May '04 - 17:47 
Hi,
I want to apply Bitmap::ConvertFormat () method for my project. But above method is not in the GDI+ version 1.0 .So I have to download GDI+ version 1.1. From where can I download it? ,I search it .But I didn't get suitable URL for that. please helpFrown | :(
QuestionHow to save GDI + bmp Image as Interlaced PNG file Format.susspubba20 May '04 - 0:49 
I created bmp Objects using Image Class in GDI+.
 
Image image(L"Mosaic.bmp");
 
// Draw the image.
graphics.DrawImage(&image, 10, 10);
 
// Construct a Graphics object based on the image.
Graphics imageGraphics(&image);
 
// Alter the image.
SolidBrush brush(Color(255, 0, 0, 255));
imageGraphics.FillEllipse(&brush, 20, 30, 80, 50);
 
// Draw the altered image.
graphics.DrawImage(&image, 200, 10);
 

Now I want to save this bmp as INTERLACED Png File format. How do I do this?.If any one know these things send a reply...Frown | :(
 


AnswerI think you need libpngmemberAI200420 May '04 - 7:07 
http://www.libpng.org/pub/png/libpng.html
QuestionHow do I save Images as EPS File Formatsusspubba28 Apr '04 - 23:54 
Hi
I want to save Images as EPS File format.I start like this..I am creating GDI+ Bitmap first & then I want to save it as EPS File.
Image image(L"myImage.jpg"); Can anybody help me please..

AnswerEasy, please read Jonathan de Halleux's nice article "Crossing the bridge between Ghostscript and GDI+"memberAI200429 Apr '04 - 6:34 
http://www.codeproject.com/vcpp/gdiplus/ghostwrapper.asp
 
Thanks a lot for Jonathan de Halleux to share it!

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 28 Apr 2004
Article Copyright 2004 by TeleStar
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid