How to modify wmf using this library?

Jan 21, 2013 at 8:03 AM

I wirte below code to add text to wmf file:

var wmf = new Oxage.Wmf.WmfDocument();                       


wmf.Records.RemoveAt(wmf.Records.Count -1);

wmf.AddText("hello world!",100,100);



But the result is not expected. Please deliver a showcase about modification of wmf. thx a lot!

Jan 27, 2013 at 10:17 AM

Please explain more details about the result so we can analyze the problem:

  • can the output WMF file be open in an image viewer? (before or after the change?)
  • if part of image is displayed - does text not render or visually appear on the surface?
  • is the output WMF file significantly smaller (file size)


Possible issues:

  • no record to define font appearance (color, style) prior the AddText method call
  • the WMF library cannot save WMF file completely (usually due to yet unimplemented records)
  • bug in the WMF library


It would be easier to analyze the problem if you could attach the original WMF file. Alternatively you can create a "dump" file and attach it to this discussion:

var wmf = new Oxage.Wmf.WmfDocument();
string dump = wmf.Dump();
System.IO.File.WriteAllText(@"D:\sdf_dump.txt", dump);


Jan 29, 2013 at 5:18 AM

It seems that I have found the problem.


if msg's length is odd, result wmf file's format is wrong and can't be displayed totally.

I use notepad++ to open result file and analyze the hex code according to wmf's format guide from Microsoft website. maybe such issue is caused by memory alignment;

Please check your library code and fix this bug; Thx a lot.


following code is used to create a wmf file;

                var wmf = new Oxage.Wmf.WmfDocument();
                wmf.Width = 700;
                wmf.Height = 1400;
                wmf.Format.Unit = 288;
                //wmf.AddCreateBrushIndirect(Color.Blue, Oxage.Wmf.BrushStyle.BS_SOLID);
                wmf.AddCreatePenIndirect(Color.Black, Oxage.Wmf.PenStyle.PS_SOLID, 1);
                wmf.AddRectangle(100, 100, 500, 2000);
                wmf.AddCircle(500, 1200, 100);
                wmf.AddCreateFontIndirect("arial", 60);
                wmf.AddText("1000", new Point(400, 1100));
                wmf.AddText("wikywhen", new Point(100, 1100));
                //wmf.AddCircle(50, 50, 200);              
then sdf.wmf file is created, you can open this file again and draw a text whose length is odd on it. then problem is recreated.

Jan 29, 2013 at 7:05 PM

Thank you for finding a bug and suggesting a solution.

As you've already figured out the problem was with odd-length strings that broke the whole WMF document. Problem has been solved by adding a dummy 0x00 byte after a odd-length string. So the META_TEXTOUT record aligns to the 16-bit boundary.

The bug has been fixed and committed to the SVN repository.