There are plenty of examples of converting a wmf into a bitmap like: Reliable .wmf/wmf to Pixel based image conversion
But I need the reverse operation. I do not look for a vectorizer. I just want to embed a picture inside a wmf file without having to bother about the bits and bytes of the wmf format. I need a solution for .NET preferably in C#.
I first thought this would do the job:
using (Image img = Image.FromFile (path)) {
img.Save (myStream, System.Drawing.Imaging.ImageFormat.Wmf);
}
But this complains at runtime that the encoder is null. Where/How can I build such an encoder? I do not need a complicated one, just one that wraps an image into a wmf. Are there some requirements on the supported formats in WMF? I suppose png and bmp are supported but is gif also supported?
An improved version of what jdehaan posted (kudos btw to him and Vincent)
This one does not leave temp files behind and also avoids copying _bufferSize to a temp file only to then copy it to another buffer. Thanks again guys.
Here's a Win32 GDI+ example that worked for me (credit to http://www.codeproject.com/Articles/6879/How-to-use-GDI-to-save-image-in-WMF-EXIF-or-EMF-fo)
From here:
But I guess you already got that far :)
Here someone is putting a bitmap in a FileStream.
with MakeMetafileStream() being:
Interesting stuff. But as to the encoder thing...
Here Peter Huang from MS posted this unmanaged approach:
Hope this'll get you there :)
Here is the full answer to the question including my modifications. Vincent's answer is fully correct. Only some definitions and one enum were missing. That is why I post here the "clean" working code in the hope it can be useful for someone else.