3

I'm trying to convert wmf image file into png format with c#.net.

But, saved image is unclear.

my code:

Metafile img = new Metafile(@"test.wmf");
MetafileHeader header = img.GetMetafileHeader();
Bitmap bitmap = new Bitmap((int)(img.Width / header.DpiX * 100), (int)(img.Height / header.DpiY * 100));
using(Graphics g = Graphics.FromImage(bitmap)){
    g.DrawImage(img, 0, 0);
}
bitmap.Save("test.png", ImageFormat.Png);

How could I get it clear?

Zoe
  • 27,060
  • 21
  • 118
  • 148
turutosiya
  • 1,051
  • 10
  • 17

3 Answers3

10

The .wmf file has extremely large values for DpiX/Y. You'll need to rescale the image to make it a better fit with the resolution of your monitor. This code produced a decent looking version of the metafile. You may want to tweak the scaling to fit your need or rescale the bitmap afterwards:

        using (Metafile img = new Metafile(@"c:\temp\test.wmf")) {
            MetafileHeader header = img.GetMetafileHeader();
            float scale = header.DpiX / 96f;
            using (Bitmap bitmap = new Bitmap((int)(scale * img.Width / header.DpiX * 100), (int)(scale * img.Height / header.DpiY * 100))) {
                using (Graphics g = Graphics.FromImage(bitmap)) {
                    g.Clear(Color.White);
                    g.ScaleTransform(scale, scale);
                    g.DrawImage(img, 0, 0);
                }
                bitmap.Save(@"c:\temp\test.png", ImageFormat.Png);
            }
        }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • You are tinkering with the wrong setting, it is Graphics.SmoothingMode. It is otherwise unclear what's in the .wmf. If it is a bitmap instead of a line drawing then you cannot improve what is already there. – Hans Passant Oct 31 '11 at 11:46
  • I've tired g.SmoothingMode = SmoothingMode.HighQuality, ,, still same. you can get the .wmf from here : http://goo.gl/CTJ40. – turutosiya Oct 31 '11 at 13:35
  • Adobe Illustrator can render that .wmf realy clear. you can see how it will be displayed with Illustrator here: http://yfrog.com/ochql9p – turutosiya Oct 31 '11 at 13:37
  • The file is strange, it has extremely large values for DpiX/Y. It seems to contain text that was flattened to curves. Post updated. – Hans Passant Oct 31 '11 at 14:45
  • The file originally from WordML file. That data is embeded in the WordML(.xml) in wmz format made by Microsoft Equation Editor. – turutosiya Nov 01 '11 at 00:39
  • Erm, no idea about those apps. You can actually see the a and b next to the X now, that has to be good. – Hans Passant Nov 01 '11 at 00:55
  • 1
    Instead of using a ScaleTransform it's also possible to create a bitmap with the size of `img.Width` and `img.Height` and call `bitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution);` – Dirk Vollmar Dec 11 '12 at 09:29
  • 1
    I had white areas at the right and bottom of the PNG file. Replacing `100` with `96` solved this issue – Breeze Dec 16 '16 at 15:14
0

EMF vectors can be very weird when converting to a PNG (especially when small formats are used). I ended up blowing it up 4x and than converting it to the size I want. The results are better when using this strategy.

GDI rendering
Source: https://keestalkstech.com/2016/06/rasterizing-emf-files-with-net-c/

Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203
0

Finally, I got an IDEA!!

That is to use Inkspace.
As you all know, Inkscape is an Vector Graphics Editor.
Today, I found out that INKSCAPE HAS COMMAND LINE OPTIONS!! http://goo.gl/Moksf .
So, what I should do is to call incape.exe with some options, such like:

Process.Start("inkscape.exe -f {wmf_filename} -e {png_filename_to_be_made}");

Here is converted png file : http://yfrog.com/gyu40ap
very clear png image. that is what I wanted to!

Thank you!

turutosiya
  • 1,051
  • 10
  • 17