EMF+ Untangled

A common belief regarding the EMF format, at least as it appears by browsing technical blog posts, is that this format was never documented by Microsoft and that there are many unknown facts about it. This opinion is wrong, as the format has complete publicly available specifications posted on MSDN.

That being said, I do admit that the documentation for how EMF can be used is pretty scarce and, from a developer’s point of view, the expectations would be to have an SDK or something similar that would help parsing EMF and would create a bijection between the binary format and a class structure.

This post will not go as far as propose an SDK but will hopefully represent a first step into this direction.

First takeaway – listing EMF records and objects

The attached program (EMF Lister) will parse an EMF+ file and will list all the records from a binary perspective.

emflister

Achieving this is not really rocket-science, as we have a method from the Graphics class that enumerates the EMF records of a Graphics object.

Second takeaway – generating an EMF file

A very easy way demonstrated with this sample code is to create an EMF using the drawing methods for a Graphics object.

Let’s take the following record from the sample EMF file:

FillRects 49152(16 bytes) EE 82 EE FF 01 00 00 00 00 00 00 00 80 02 E0 01

This was generated from the C# method:

g.FillRectangle(Brushes.Violet, 0, 0, width, height);

(where width = 640, height = 480).

Checking the specifications for the FillRects record, we can see how the two link together:

EE 82 EE FF and the Flags: 49152 = define the Brush being used for filling rectangles.

01 00 00 00 = number of rectangles (one rectangle).

00 00, 00 00, 80 02/ 0x280, E0 01/ 0x1E0 = 0, 0, 640, 480 – the left, top, width and height of the rectangle.

For more details regarding how brushes and pens are saved, please check the EMF+ object specifications.

========================

Using the EMF Lister:

Usage: EMFLister.exe <EMF input> [TXT output]

for parsing EMF input

Or:

EMFLister.exe

for generating the sample.EMF file in the current folder

 

Additional information:

There is a very useful Codeplex project that (among other features) converts XAML to EMF: https://xamltoys.codeplex.com/

EMFLister.zip