I am using a MemoryStream to write my GDI+ drawing commands to a Metafile, and display the Metafile later in order to accelerate such trivial tasks as panning and zooming. That works fine.
When I wrote the code in the past, I took care not to dispose the MemoryStream before the Metafile is actually drawn on screen. Today, out of a hazardous mood, I already disposed the MemoryStream before drawing the image on screen, and surprisingly, it still worked well without throwing an exception. So I wonder if this is legal, or if it just worked by accident, and there are conditions where it might fail.
I suppose this situation is somehow similar to a metafile that resides on a hard drive, which is deleted after the metafile is loaded, but before it is drawn on screen, right?
MemoryStream memoryStream = new MemoryStream();
Metafile metafile = new Metafile(memoryStream, deviceContextHandle, rectangleF, MetafileFrameUnit.Pixel, EmfType.EmfPlusOnly);
using (Graphics g = Graphics.FromImage(metafile))
{
SomeGraphicsObject.Draw(g);
}
screenGraphics.DrawImage(metafile, x, y, dx, dy);
memoryStream.Dispose(); // legit to move this one line up?