Why does EnumerateMetafile only work with Aero enabled

731 Views Asked by At

My code enumerates a metafile:

private void Parse()
{
    Graphics graphics = Graphics.FromHwnd(IntPtr.Zero);
    PointF pointf = new PointF();
    graphics.EnumerateMetafile(_metafile, pointf, ParseCallback);
}

private bool ParseCallback(EmfPlusRecordType recordType, 
    int flags, int dataSize, IntPtr data, PlayRecordCallback callbackData)
{
    // do stuff
}

My development machine is Windows 7 VirtualBox guest on Ubuntu host.

The code used to work fine. However, when I turned off Aero, the code stopped working: The ParseCallback would never be called.

Only when I turned Aero back on, ParseCallback was executed again.

Why and how can I make this code work on non-Aero-enabled machines?

1

There are 1 best solutions below

0
On

I don't have a full answer to the "why?" question, but it does not work because you're getting the Graphics GDI+ object from the Window handle. Instead, you want to get it from a GDI DC, like this:

private void Parse()
{
    IntPtr hdc = GetDC(IntPtr.Zero); // get entire screen dc
    Graphics graphics = Graphics.FromHdc(hdc));
    PointF pointf = new PointF();
    graphics.EnumerateMetafile(_metafile, pointf, ParseCallback);
    ReleaseDC(IntPtr.Zero, hdc);
}

[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr hWnd);

[DllImport("user32.dll")]
static extern IntPtr ReleaseDC(IntPtr hwnd, IntPtr hdc);

Note you could also use the Graphics object from the Form OnPaint(PaintEventArgs e) method, it should also work, just like in the official sample code for EnumerateMetafile method here: Graphics.EnumerateMetafile Method