Read and draw a metafile in C#

[example]

This example shows how to read a metafile and draw its contents. This is really easy in C#. When the program starts, it executes the following code.

private void Form1_Load(object sender, EventArgs e)
{
    Metafile mf1 = (Metafile)Metafile.FromFile("test.emf");
    picMetafile1.Image = mf1;
    picMetafile1Stretched.Image = mf1;

    Metafile mf2 = (Metafile)Metafile.FromFile("Volleyball.wmf");
    picMetafile2.Image = mf2;
    picMetafile2Stretched.Image = mf2;
}

The code simply uses the Metafile class’s FromFile method to load two metafiles. The file “test.emf” is the file created by the example Make and use a metafile in C#. The file “Volleyball.wmf” is a clip art file I downloaded by using Word.

Oddly FromFile method returns a more general Image object so the program casts it into the more specific Metafile class. (It actually could just use the object as an Image in this example.)

After creating the metafile objects, the program simply assigns them to the Image properties of some PictureBox controls. All of them have SizeMode set to StretchImage so they resize the metafile images to fit the controls.

The two images containing shapes are nice and smooth.

The volleyball images are a bit rougher for two reasons. First, their images are enormous. The were recorded at 1000 dpi (dots per inch) and have bounds -1024 ≤ X ≤ 953 and -849 ≤ Y ≤ 614. When the controls shrink the images to fit, their are some aliasing problems.

The second reason they are rough is that they were not recorded with anti-aliasing. If you look at the previous example, you’ll see that the program contains this statement:

gr.SmoothingMode = SmoothingMode.AntiAlias;

That makes the metafile record a command to draw smoothly. If you load the volleyball metafile into a PictureBox at full size (set SizeMode to AutoSize), you can see that the shapes drawn are not smoothed.

Final Notes

Note that both of the metafiles have transparent backgrounds. In places where they don’t draw, the form’s background color shows through.

Some of the documentation on the internet may say that the .NET Framework’s Metafile class doesn’t correctly support saving or loading metafiles and instead converts the results into PNG files. That doesn’t seem to be the case any more. As this example and the previous one show, the Metafile class can save and read metafiles correctly.

Some other programs don’t, however. For example, if you open a metafile in MS Paint, you get a bitmapped version not a true metafile.

PowerPoint and Microsoft Word do load metafiles correctly, so you can use them to test your results if you like. They also somehow resize the volleyball metafile smoothly. Perhaps they load it into a bitmap at its full scale and then resizes it smoothly.

In fact, PowerPoint and Word can even edit metafile data! Insert the metafile as an image. Then right-click it and select Edit Picture. When PowerPoint or Word asks, convert the metafile into a Microsoft Office drawing object. Then you can click on the elements in the image and modify them.

I haven’t found a way to make Word save the modified image as an EMF file, but you can do it in PowerPoint, sort of. Make a new presentation, put the metafile on a slide, and make the image fill the entire slide. With that slide selected, open the File menu and select Save As. Pick the file type “Enhanced Windows Metafile (*.emf)” and click Save. When the program asks, save only the current slide. The result will be a metafile containing the image.

Now you can use PowerPoint as a handy metafile editor!


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.

This entry was posted in drawing, graphics and tagged , , , , , , , , , , , , , . Bookmark the permalink.

One Response to Read and draw a metafile in C#

Leave a Reply

Your email address will not be published. Required fields are marked *