Simplified Image Processing in GDI+ - Sample One-Record and Replay Metafiles
(Page 3 of 6 )
As is indicated above, to record a meta file is in fact to persist all the drawing instructions into a disk file. In this simple sample, you will learn how to save (record) the drawing instructions and replay them on the screen. Figure 1 shows the running-time snapshot.
Figure 1-the running-time snapshot for sample 1

The following indicates the related key code implementation:
Graphics metagraph=this.CreateGraphics();
//create a new meta file
IntPtr hdc =metagraph.GetHdc();
Metafile metaFile1 = new Metafile("SampMeta1.emf", hdc);
//use the address of the Metafile object as the drawing plane
Graphics graphics=Graphics.FromImage(metaFile1);
//define a red to blue gradient brush
LinearGradientBrush RtoBBrush=new LinearGradientBrush(
new Point(0, 10),
new Point(200, 10),
Color.Red,
Color.Blue);
//define another blue to yellow gradient brush
LinearGradientBrush BtoYBrush=new LinearGradientBrush(
new Point(0, 10),
new Point(200, 10),
Color.Blue,
Color.Yellow);
Pen bluePen=new Pen(Color.Blue);
//draw a ancient Chinese picture of the Eight Diagrams onto the 'screen'
Rectangle ellipseRect=new Rectangle(0, 0, 200, 200);
Rectangle left=new Rectangle(0, 50, 100, 100);
graphics.DrawArc(bluePen,left,180.0f,180.0f);
Rectangle right=new Rectangle(100, 50, 100, 100);
graphics.FillPie(RtoBBrush, ellipseRect,0.0f,180.0f);
graphics.FillPie(BtoYBrush, ellipseRect,180.0f,180.0f);
graphics.FillPie(RtoBBrush, left,180.0f,180.0f);
graphics.FillPie(BtoYBrush, right,0.0f,180.0f);
//render some text
SolidBrush solidBrush=new SolidBrush(Color.Black);
FontFamily fontFamily=new FontFamily("MS Gothic");
Font font=new Font(fontFamily, 27,
FontStyle.Regular, GraphicsUnit.Pixel);
graphics.DrawString("Meta File Test", font, solidBrush,
new PointF(5.0f, 80.0f));
//so far, GDI+ has merely store drawing instruction into the harddisk
//release all the related resources used above
graphics.Dispose();
metaFile1.Dispose();
metagraph.ReleaseHdc(hdc);
metagraph.Dispose();
//replay the drawing info created above
Graphics playbackGraphics=this.CreateGraphics();
playbackGraphics.Clear(Color.White);
//open and render the previously-created meta file
Metafile metaFile2 = new Metafile("SampMeta1.emf");
playbackGraphics.DrawImage(metaFile2,new Point(0,0));
//close the open meta file
metaFile2.Dispose();
There are two crucial steps in the previous sample. The first step is to create an empty meta file named "SampMeta1.emf,"draw images onto the image, and then render text in it. All of the drawing actions are done on a file in a hard disk-you can see nothing on the screen during the first step.
The second step picks up from the last few lines. First create a Graphics object, then open the meta file created in the first step and finally, by using the DrawImage() method of the Graphics object, the true rendering plays on the screen. It is only then that you can visualize the rendering result, i.e. replay the meta file on the screen.
One last word: after running the first sample, you may find a metafile named "SampMeta1.emf," which corresponds to the drawing instructions; without the second step to invoke the DrawImage() method you will not see anything on the screen.
Next: Sample Two-Clipping and Scaling Images >>
More Windows Scripting Articles
More By Xianzhong Zhu