Microsoft Windows Metafile

Also Known As: Windows Metafile, WMF


Type Metafile
Colors 24-bit maximum
Compression NA
Maximum Image Size NA
Multiple Images Per File No
Numerical Format Little-endian
Originator Microsoft Corporation
Platform Microsoft Windows
Supporting Applications Numerous Microsoft Windows-based graphics applications
See Also Encapsulated PostScript, Microsoft Windows Bitmap

Usage
Used for file interchange, device support.

Comments
A widely used format associated with Microsoft Windows, although applications on other platforms may provide support.

Vendor specifications are available for this format.


Microsoft Windows Metafile (WMF) files are used to store vector and bitmap-format image data in memory or in disk files for later playback to an output device. Although Windows Metafile is specific to Microsoft Windows, many non-Windows-based applications support this format as a method for interchanging data with Windows applications. Because of the widespread popularity of the Microsoft Windows GUI, the Windows Metafile format has become a staple format for graphical applications and is supported on all platforms. Encapsulated PostScript (EPSF) supports the use of an included Windows Metafile when required to store vector-based data. The logical unit of measurement used in Windows Metafiles is the twip. A twip (meaning "twentieth of a point") is equal to 1/1440 of an inch. Thus 720 twips equal 1/2 inch, while 32,768 twips is 22.75 inches.

Contents:
File Organization
File Details
For Further Information

File Organization

Windows Metafile format files contain a header, followed by one or more records of data. The header contains a description of the record data stored in the metafile. Each record is a binary-encoded Microsoft Windows Graphics Device Interface (GDI) function call. The GDI is used by Windows to perform all output to a screen window or other output device. When the metafile data is rendered (or played back, in Microsoft terminology), the data from each record is used to perform the appropriate function call to render each object in the image. The last record in the file contains information indicating that the end of the record data has been reached.

File Details

The header is 18 bytes in length and is structured as follows:

typedef struct _WindowsMetaHeader
{
  WORD  FileType;       /* Type of metafile (1=memory, 2=disk) */
  WORD  HeaderSize;     /* Size of header in WORDS (always 9) */
  WORD  Version;        /* Version of Microsoft Windows used */
  DWORD FileSize;       /* Total size of the metafi+le in WORDs */
  WORD  NumOfObjects;   /* Number of objects in the file */
  DWORD MaxRecordSize;  /* The size of largest record in WORDs */
  WORD  NoParameters;   /* Not Used (always 0) */
} WMFHEAD;

FileType contains a value which indicates the location of the metafile data. A value of 1 indicates that the metafile is stored in memory, while a 2 indicates that it is stored on disk.

HeaderSize contains the size of the metafile header in WORDs.

Version stores the version number of Microsoft Windows that created the metafile. This value is always read in hexadecimal format. For example, in a metafile created by Windows 3.0, this item would have the value 300h.

FileSize specifies the total size of the metafile in 16-bit WORDs.

NumOfObjects specifies the number of objects that are in the metafile.

MaxRecordSize specifies the size of the largest record in the metafile in WORDs.

NumOfParams is not used and is set to a value of 0.

Following the header is a series of data records. The basic format of each record is shown below:

typedef struct _WindowsMetaRecord
{
DWORD Size;          /* Total size of the record in WORDs */
WORD  Function;      /* Function number (defined in WINDOWS.H) */
WORD  Parmeters[];   /* Parameter values passed to function */
} WMFRECORD;

Size is the total size of the records in WORDs, including the Size field itself. The minimum possible size for a record is 3.

Function is the GDI number of the function.

Parameters is an array of the parameters used by the function. The parameters are stored in the reverse order in which they are passed to the function.

When a Windows Metafile format file is played back, each record is read and the function call it contains is executed in the sequence in which it is read. The last record in every metafile always has a function number of zero and is used to indicate the end of the record data.

There are several important considerations that must be observed when reading WMF record data.

First, not all of the records in a Windows Metafile have the above format, although most do. The GDI function calls that do follow the basic record format are the following:

Arc RealizePalette SetPolyFillMode
Chord Rectangle SetROP2
Ellipse ResizePalette SetStretchBltMode
ExcludeClipRect RestoreDC SetTextAlign
FloodFill RoundRect SetTextCharExtra
IntersectClipRect SaveDC SetTextColor
LineTo ScaleViewportExt SetTextJustification
MoveTo ScaleWindowExt SetViewportExt
OffsetClipRgn SetBkColor SetViewportOrg
OffsetViewportOrg SetBkMode SetWindowExt
OffsetWindowOrg SetMapMode SetWindowOrg
PatBlt SetMapperFlags
Pie SetPixel

Second, several record formats deviate from this basic record format by containing a data structure, rather than a data array, in the Parameters field. These are:

AnimatePalette CreatePatternBrush Escape
BitBlt CreatePenIndirect ExtTextOut
CreateBrushIndirect CreateRegion Polygon
CreateFontIndirect DeleteObject PolyPolygon
CreatePalette DrawText Polyline

Consult the Microsoft Windows Programmer's Reference Library for the internal structure of each of these special records.

Third, several GDI function calls were added or had their parameters changed with the release of Microsoft Windows 3.0. GDI function calls in this category include:

AnimatePalette Record DeleteObject
BitBlt CreatePatternBrush RealizePalette
CreatePalette Record ResizePalette

Note that not all GDI function calls can appear in a metafile. The only calls that are valid are those that take a handle to a device context as their first parameter. A complete list of all of the GDI function calls is documented in Microsoft Windows Programmer's Reference. They are also found in the WINDOWS.H header file. These GDI function calls are the directives that begin with the characters META. There are more than 70 different GDI function calls defined for Windows 3.0.

Porting WMF Files Between Applications

Most Microsoft Windows applications that create metafiles prepend a 22-byte header to the file. This header contains information not found in the metafile header, but which is needed to move the metafile information between applications. The structure of this header is as follows:

typedef struct _WmfSpecialHeader
{
  DWORD Key;           /* Magic number (always 9AC6CDD7h) */
  WORD  Handle;        /* Metafile HANDLE number (always 0) */
  SHORT Left;          /* Left coordinate in metafile units */
  SHORT Top;           /* Top coordinate in metafile units */
  SHORT Right;         /* Right coordinate in metafile units */
  SHORT Bottom;        /* Bottom coordinate in metafile units */
  WORD  Inch;          /* Number of metafile units per inch */
  DWORD Reserved;      /* Reserved (always 0) */
  WORD  Checksum;      /* Checksum value for previous 10 WORDs */
} WMFSPECIAL;

Key contains a special identification value that indicates the presence of a special header and is always 9AC6CDD7h.

Handle is not used and always contains the value 0.

Left, Top, Right, and Bottom contain the coordinates of the upper-left and lower-right corners of the image on the output device. These are measured in twips. These four fields also correspond to the RECT structure used in Microsoft Windows and found in the file WINDOWS.H.

Inch contains the number of twips per inch used to represent the image. Normally, there are 1440 twips per inch; however, this number may be changed to scale the image. A value of 720 indicates that the image is double its normal size, or scaled to a factor of 2:1. A value of 360 indicates a scale of 4:1, while a value of 2880 indicates that the image is scaled down in size by a factor of two. A value of 1440 indicates a 1:1 scale ratio.

Reserved is not used and is always set to 0.

Checksum contains a checksum value for the previous 10 WORDs in the header, calculated by XORing each WORD value to 0:

WMFSPECIAL wmfspecial; wmfspecial.
Checksum = 0;
wmfspecial.Checksum ^= (wmfspecial.Key & 0x0000FFFFL);
wmfspecial.Checksum ^= ((wmfspecial.Key & 0xFFFF0000L) >> 16);
wmfspecial.Checksum ^= wmfspecial.Handle; wmfspecial.Checksum ^=
wmfspecial.Left;
wmfspecial.Checksum ^= wmfspecial.Top; wmfspecial.Checksum ^= 
wmfspecial.Right;
wmfspecial.Checksum ^= wmfspecial.Bottom; wmfspecial.Checksum ^=
wmfspecial.Inch;
wmfspecial.Checksum ^= (wmfspecial.Reserved & 0x0000FFFFL);
wmfspecial.Checksum ^= ((wmfspecial.Reserved & 0xFFFF0000L) >> 16);

An alternative way to step through the header structure one WORD at a time is to use a pointer as shown below:

WMFSPECIAL *wmfspecial;
WORD *ptr;
wmfspecial->Checksum = 0;
for(ptr = (WORD *) wmfspecial; 
    ptr < (WORD *)wmfspecial->Checksum;
    ptr++)
	wmfspecial->Checksum ^= *ptr;

Storing Bitmaps in a WMF File

The BitBlt function (GDI function number 940h) is used to store device-independent bitmaps in a Windows Metafile. This record was modified for Windows 3.0, so metafiles created under earlier versions of Windows may not be suitable for playback on all graphics output devices.

For Further Information

For further information about the Microsoft Windows Metafile format, see the specification included on the CD-ROM. You may also obtain information by contacting Microsoft at:

Microsoft Corporation
One Microsoft Way
Redmond, WA 98052-6399
Voice: 206-882-8080
FAX: 206-936-7329
BBS: 206-637-9009
WWW: http://www.microsoft.com/

Additional information about the Windows Metafile Format and the Microsoft Windows Graphics Device Interface can also be found in the following references:

Petzold, Charles, Programming Windows: The Microsoft Guide to Writing Applications for Windows 3, Second Edition, Microsoft Press, Redmond, WA, 1990.

Microsoft Corporation, Microsoft Windows: A Guide to Programming, Microsoft Windows Programmer's Reference Library, Microsoft Press, Redmond, WA, 1990.

Microsoft Corporation, Microsoft Windows: Programmer's Reference, Microsoft Windows Programmer's Reference Library, Microsoft Press, Redmond, WA, 1990.

Microsoft Corporation, Microsoft Windows: Programming Tools, Microsoft Windows Programmer's Reference Library, Microsoft Press, Redmond, WA, 1990.

The Microsoft Windows Programmer's Reference Library is the master reference for programmers working with all aspects of Microsoft Windows. The books in this library are supplied with the Microsoft Windows Software Development Kit (SDK). The manuals supplied with the Microsoft C 7.0 Professional Development Systems are also very helpful. You can get information about obtaining these products from:

Microsoft Information Center
Voice: 800-426-9400

You may also be able to get information via FTP through the Developer Relations Group at:

ftp://ftp.microsoft.com/developer/drg/


[Previous] [Next] [Up] [Index]
Contents | Glossary | Main | Formats | Software | Internet | Book

Copyright © 1996, 1994 O'Reilly & Associates, Inc. All Rights Reserved.