Introduction to GDI Metafiles

Metafiles are a type of graphics object that contains a collection of GDI drawing commands rather than a direct representation of pixel data. This makes them more flexible and scalable than bitmaps. When a metafile is displayed, the GDI commands are re-executed to render the image. This allows for device-independent scaling and editing.

Metafiles are particularly useful for storing complex graphics, vector artwork, and for inter-application data transfer where the exact pixel representation is less important than the descriptive drawing instructions.

Note: While GDI metafiles (EMF, WMF) are still supported, modern Windows applications are encouraged to use more advanced graphics technologies like Direct2D for new development.

Types of Metafiles

Windows GDI supports two primary metafile formats:

  • Enhanced Metafile (EMF): A more modern and extensible format than WMF. EMF files store drawing commands in a format that is largely independent of the output device. They can contain both vector and raster data.
  • Windows Metafile (WMF): An older, 16-bit format that is less flexible than EMF. WMF files are typically more device-dependent and have limitations in terms of complexity and resolution.

Creating Metafiles

Metafiles are created by first establishing a metafile device context (DC). All subsequent GDI drawing operations sent to this DC are recorded in the metafile instead of being rendered directly. Once recording is complete, the metafile can be saved or manipulated.

Key functions for creating metafiles include:

  • CreateEnhMetaFile: Creates an enhanced metafile.
  • CreateMetaFile: Creates a classic Windows metafile.

To stop recording and retrieve the metafile handle:

  • CloseEnhMetaFile: Closes an enhanced metafile device context.
  • CloseMetaFile: Closes a Windows metafile device context.

Playing Metafiles

To display the contents of a metafile, you "play" it onto a target device context. This involves re-executing the recorded GDI commands. When playing, you can specify a target rectangle to scale or transform the metafile's contents.

Key functions for playing metafiles:

  • PlayEnhMetaFile: Plays an enhanced metafile.
  • PlayMetaFile: Plays a Windows metafile.

For more control over playback, including scaling and transformation:

  • EnumEnhMetaFile: Enumerates the records in an enhanced metafile.
  • GdiPlayDrawing (part of EMFPlus, related but distinct).

Manipulating Metafiles

Metafiles can be copied, deleted, or modified. You can also retrieve information about the metafile, such as its dimensions and the type of content it contains.

Key functions for manipulation:

  • CopyEnhMetaFile: Copies an enhanced metafile.
  • DeleteEnhMetaFile: Deletes an enhanced metafile.
  • GetEnhMetaFileDescription: Retrieves a text description of the metafile.
  • GetEnhMetaFileHeader: Retrieves the header information of an enhanced metafile.
  • GetEnhMetaFilePixelFormat: Retrieves the pixel format information for an enhanced metafile.

Other GDI functions that interact with or are relevant to metafiles:

Function Description
CreateEnhMetaFile Creates an enhanced metafile device context.
CloseEnhMetaFile Closes an enhanced metafile device context and returns a handle to the metafile.
PlayEnhMetaFile Displays the contents of an enhanced metafile on a specified device.
GdiSetEnhMetaRgn Sets the metafile's update region.

Key Structures

Metafiles utilize several important structures:

Structure Description
ENHMETAHEADER Contains information about an enhanced metafile.
METAHEADER Contains information about a Windows metafile.
ENHMETARECORD Represents a single record in an enhanced metafile.

Key Constants

Constants are used to define metafile types and playback options:

Constant Description
EMR_HEADER Identifier for the metafile header record.
MF_VALIDSIZE Indicates that the metafile's size is valid.