本文介绍了转换后的图像不清楚. wmf到png的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用c#.net将wmf图像文件转换为png格式.

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

但是,保存的图像不清楚.

But, saved image is unclear.

我的代码:

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

我怎么弄清楚?

推荐答案

.wmf文件的DpiX/Y值非常大.您需要重新缩放图像以使其更适合显示器的分辨率.这段代码产生了一个看起来不错的图元文件版本.您可能需要调整缩放比例以适合您的需求,或者之后再重新缩放位图:

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

这篇关于转换后的图像不清楚. wmf到png的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 18:09