Search code examples
winapiwmf

WMF rendered badly with PlayEnhMetafile


this is a question for somebody who is familiar with WMF/EMF formats.

I wonder why the attached metafile does not render correctly with PlayEnhMetafile (after conversion with SetWinMetaFileBits, I am testing it in Delphi). It does not render correctly with many other apps (e.g. Total Commander viewer, Irfanview,...) but renders correctly with MS Office 2010 image viewer.

Note: This image comes from RTF file produced by MS Word 2010 and is the result of Equation Object.

What could be the cause and how to render this image correctly.

Link to image


Solution

  • It would seem that the Windows metafile does not like getting converted to an Enhanced metafile. The following code seems to render the wmf as intended:

    [DllImport("gdi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern IntPtr GetMetaFile(string filename);
    
    [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
    private static extern bool PlayMetaFile(IntPtr hdc, IntPtr hmf);
    
    var bmpNew = new Bitmap(7104, 992);
    using (var gfx = Graphics.FromImage(bmpNew))
    {
        var bmpHdc = gfx.GetHdc();
        var mfHdc = GetMetaFile(@"C:\Users\mrtel_000\Desktop\test.wmf");
        var res = PlayMetaFile(bmpHdc, mfHdc);
        if (!res)
        {
            throw new Win32Exception();
        }
        gfx.ReleaseHdc();
    }
    
    bmpNew.Save("demo.png");
    

    The above is obviously leaking resources, but demonstrates a proper playback.

    Result:

    enter image description here
    (Click for Full Size)