Techniques - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Techniques arrow Page 2 - I Never Metafile I Didn't Like
I Never Metafile I Didn't Like
By Peter Aitken

Rate This Article: Add This Article To:

I Never Metafile I Didn't Like - ' Metafiles and the '
( Page 2 of 3 )

.Net Framework">

A metafile is one of the two fundamental ways that a graphics image can be stored. The other, which most people are more familiar with, is a bitmap in which the image is defined as a grid of pixels with each pixel having a color value. Most graphics files that you work with, including the BMP, JPG, GIF, and PNG extensions, are bitmaps. In contrast, a metafile stores an image as the series of instructions required to create the image. Bitmap images are sometimes called raster images, and metafile images are also called vector images.

Metafiles have both advantages and disadvantages with respect to bitmaps. On the plus side, the file size is almost always a lot smaller. Perhaps more important, a metafile is scalable, meaning that it can be reproduced at any size without loss of resolution. In contrast, bitmaps are not scalable. If you display a bitmap at more than its native resolution — for example, a 100x200 bitmap displayed at 200x400 pixels — the rendering engine must interpolate the extra pixels with an unavoidable loss of resolution.

ADVERTISEMENT

On the negative side, metafiles can be used for only certain types of images: charts, diagrams, and certain types of drawings. They are not suitable for photographs and the like.

Metafiles and the .Net Framework

Windows and the .Net Framework support three kinds of metafiles. The original format, which dates from the long-gone days of 16-bit Windows, is called Windows Metafile and the files use the WMF extension. Along with 32-bit Windows, a new format was introduced, called Enhanced Metafile, using the EMF extension. More recently, the Enhanced MetaFile Plus (EMF+) format was developed to support new drawing commands in GDI+; it too uses the EMF extension. A variant on EMF+ is called dual format and contains both the older GDI and newer GDI+ commands to create the image.

Basic support for both metafiles and bitmaps is provided in the Image class. This class is the base class for the Bitmap and Metafile classes which provide additional capabilities for working with each kind of image. The Metafile class is in the System.Drawing.Imaging namespace.

Loading and displaying an existing metafile is a simple matter, no different really from displaying a bitmap:

public void DisplayMetafile(PaintEventArgs e)
{
   Graphics gr = e.Graphics;
   Metafile mf = new Metafile("Chart01.emf");
   gr.DrawImage(mf, 200, 200);
}

Metafile Headers

Every metafile includes a header that contains various information about the metafile. You use the GetMetfileHeader method to return a metafile's header as a type MetafileHeader. Here's an example:

Metafile mf = new Metafile("Chart01.emf");
MetafileHeader mfh = mf.GetMetafileHeader();

If you want to get the header without loading the file, use the static version of the method:

MetafileHeader mfh = Metafile.GetMetafileHeader(:chart01.emf");

MetafileHeader has numerous properties that provide information about the metafile. The ones you'll need most often are described in Table 1.

Table 1. Frequently used MetafileHeader properties.

Property Description
Bounds Returns a Rectangle structure that represents the bounds of the image.
DpiX, DpiY Returns the horizontal or vertical resolution, respectively, of the image in dots per inch.
Type Returns the type of the metafile. See Table 2 for possible return values.
IsDisplay Returns True if the metafile was created based on a video display, false if created based on a printer (see the main text for details).

Table 2. Return values of the MetafileHeader.Type property.

Return value Meaning
Emf Enhanced metafile format containing only GDI commands.
EmfPlusDual A dual file that contains both GDI and GDI+ commands and can be displayed using either set of commands depending on the rendering program.
EmfPlusOnly Enhanced metafile plus format containing only GDI+ commands.
Invalid A format that is not recognized by GDI+.
Wmf Metafile format containing only GDI commands.
WmfPlaceable Windows metafile with a placeable header.

Metafile Image Size, Resolution, and Bounds

A bitmap image has an inherent size that is defined by its pixel dimensions. In contrast, a metafile has no inherent size. Even so, when a metafile is represented by a Metafile object it has properties (inherited from the Image class) that specify its width, height, and resolution. Likewise, the MetafileHeader class has a Bounds property that returns the image's bounding rectangle. What do these properties mean when referencing a metafile?

To understand, consider a simple metafile that consists of two instructions:

  1. Draw a line from coordinates (10,10) to (100,120).
  2. Draw a line from coordinates (100,120) to 50,40).

Figure 1: The "size" of a metafile image is defined by its bounding rectangle.

The image will look something like the solid lines in Figure 1. The bounding rectangle, represented by the dotted box, is the smallest rectangle that encompasses all the points in the image. This is the size of the image, in this case 90 units wide and 70 units height. What's the unit? There is none; the size of a metafile is relative.

However, a metafile can also have a resolution assigned to it, as reflected in the DpiX and DpiY values in the file header. It's not really "dots" per inch but units per inch. Thus, if the above image were assigned vertical and horizontal resolution of 100, its "size" would be 0.9 inches wide (90 units at 100 units per inch) and 0.7 inches high (70 units at 100 units per inch). This "size" is not relevant to anything in the physical world, as the image can still be displayed at any desired size, but a metafile's size can be assigned to define its intended display size or to relate the arbitrary units in the metafile to real world units (for example, in an engineering drawing).



 
 
>>> More Techniques Articles          >>> More By Peter Aitken
 



Microsoft's Future: A Chat With Their CTO, Barry Briggs

Play Video >

All Videos >

Julia explores the Robotics Studio!

Read now >

Messages to Bill Gates!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.