An unclear converted image. wmf to png

前端 未结 3 1025
梦毁少年i
梦毁少年i 2021-01-14 18:02

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.         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-14 18:28

    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);
                }
            }
    

提交回复
热议问题