Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WMF / EMF File Format conversion C#

Tags:

c#

image

gdi

wmf

In my program, I have a requirement to "playback" or "parse" windows metafiles (WMF and EMF). I have dug through MSDN and Google, and the closest I have come is the Graphics. EnumerateMetafile method. I can get it to work, in that my EnumerateMetafileProc callback is called, and I can then call PlayRecord. What is missing, is how to get usefull data out of that callback.

Example I looked at: http://msdn.microsoft.com/en-us/library/ms142060.aspx

The callback has a recordType parameter, which is an ENUM. Well this looks useful, except their seems to be no way to cast the data into any useful type.

My goal is to play back the creation of the WMF/EMF, so that I can make function calls to a specialized graphics class which has methods like (DrawLine, DrawPoint, DrawArc). In a sense, I am re-creating the WMF/EMF drawing in a completely different format (Converting).

Any help in this is extremely appreciated.

like image 748
FlaCracker Avatar asked Nov 19 '09 13:11

FlaCracker


1 Answers

Graphics.EnumerateMetafile, is unfortunately a very, very thin layer around the Win32 APIs. For each record in the EMF, you get an unmanaged memory address for the record-specific struct. No documentation about what it might contain - but then, there isn't much to go on in the Win32 world either.

So you need to consult the ancient texts!

This book came with a code sample CD-ROM that includes sample programs to use the EMF playback API.

This leaves you with two problems.

  1. The book is apparently out of print, so make sure that any used copy you buy includes a working CD-ROM.

  2. The sample code is all in C/C++ and it will be a non-trivial task to declare the necessary interop for the 100 or so structures that are used to describe all the EMF records.

So another approach might be to declare a huge interface in .NET that has a method for every GDI call you need to intercept, and then use C++/CLI to adapt the example C++ code so it calls onto your huge interface for each record.

Update

Although the MSDN documentation for EMF playback doesn't include details of specific EMF records, Microsoft has separately published the complete specification as of 2006:

MS-EMF: Enhanced Metafile Format

Also the author of the book I linked to above has made the source code of the CDROM samples available to download:

Source code for Windows Graphics Programming

like image 85
Daniel Earwicker Avatar answered Sep 21 '22 08:09

Daniel Earwicker