c#如何wmf图片转换成png图片,转换后的图像不清楚。 wmf转png

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?

解决方案

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值