I need to create some in-memory (not file-based) metafiles and display them. In the OnPaint
handler of a panel on my form I call this code:
Metafile metafile;
using( Graphics offScreenGraphics = Graphics.FromHwndInternal( IntPtr.Zero ) )
{
IntPtr hDC = offScreenGraphics.GetHdc();
metafile = new Metafile( hDC, EmfType.EmfPlusDual );
offScreenGraphics.ReleaseHdc();
}
Graphics graphics = Graphics.FromImage( metafile );
graphics.FillRectangle( Brushes.LightYellow, 0, 0, 100, 100 );
graphics.DrawLine( Pens.Red, 50, 50, 100, 100 );
e.Graphics.DrawImage( metafile, 50, 50 );
However, nothing is displayed and regardless of what I try to draw in the metafile
, its PhysicalDimension
is always something like 35x35, which is too small given that the units are hundredths of a millimeter!
NOTE: Of course this is a contrived example, I won't be creating a metafile just to draw some graphic in the same method. I need to pass the metafile around in my application. This is just a simple test to make sure I will be able to draw it with the DrawImage
method.