[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Display a Windows metafile (wmf) in C#

[Display a Windows metafile (wmf) in C#]

A Windows metafile (wmf) is an image file that contains instructions for drawing shapes. In contrast, bitmap, PNG, JPG, and other raster image files indicate exactly what colors individual pixels should have. Because a Windows metafile contains drawing instructions instead of pixel values, you can resize a Windows metafile without producing ugly anti-aliasing effects.

This example loads a Windows metafile in two ways and enlarges it slightly. When loaded as a bitmap (on the left), the enlarged version is blocky and pixelated. When loaded as a metafile (on the right), the result is much smoother.

This example uses the following code to load a Windows metafile as a bitmap and as a metafile.

// Load the images. private void Form1_Load(object sender, EventArgs e) { // Load as a bitmap to see how big it is. string filename = "Epitrochoid.wmf"; picImage.Image = new Bitmap(filename); picMetafile.Image = new Metafile(filename); }

First the program creates a new Bitmap object, passing its constructor the metafile's name. It then creates a new Metafile object, passing its constructor the metafile's name.

The code displays the loaded Bitmap and Metafile objects in two PictureBox controls. Those controls are slightly larger than the original Windows metafile and their SizeMode properties are set to Zoom so they enlarge their images.

For some annoying reason, if you set a PictureBox control's Image property to a Windows metafile at design time, the program loads it as a Bitmap instead of as a Metafile. That means if you make the PictureBox display the image at other than its original size, you get aliasing effects.

Therefore to get the best result, you should load metafiles at run time as shown by this example.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.