The EMF is being converted to a bitmap by the Save
operation.
An EMF can be successfully written thanks to this answer, with minor modifications.
using System.Runtime.InteropServices;
including in the class
[DllImport("gdi32.dll")]
internal static extern uint GetEnhMetaFileBits(IntPtr hemf,
uint cbBuffer, byte[] lpbBuffer);
[DllImport("gdi32.dll")]
internal static extern bool DeleteEnhMetaFile(IntPtr hemf);
and modifying the code
private void button1_Click(object sender, EventArgs e)
{
Metafile emf = null;
using (var ms = new MemoryStream(Properties.Resources.blank))
{
emf = new Metafile(ms);
}
IntPtr h = emf.GetHenhmetafile();
uint size = GetEnhMetaFileBits(h, 0, null);
byte[] data = new byte[size];
GetEnhMetaFileBits(h, size, data);
using (FileStream w = File.
Create("C:\\Users\\chrisd\\Documents\\emfbitmap1.emf"))
{
w.Write(data, 0, (int)size);
}
DeleteEnhMetaFile(h);
}
Of course, if it is not required to have the EMF in memory it can be written to disc directly. E.g.
private void button1_Click(object sender, EventArgs e)
{
File.WriteAllBytes("C:\\Users\\chrisd\\Documents\\emfbitmap1.emf",
Properties.Resources.blank);
}