我正在寻找一种方法来防止 WPF 模糊我的图像。我希望我的应用程序在高 DPI 显示器上看起来不错,所以我创建了 96x96px 的图标以显示为 32x32 大小。
在 google 中有很多东西可以找到,甚至在 stackoverflow 上也有一些关于此的主题。总结他们说:启用该属性UseLayoutRounding
并设置RenderOptions.BitmapScalingMode
为HighQuality
. 不幸的是,这对我来说真的不起作用——图像看起来仍然很模糊。然后我在具有更高 DPI aaaand 的笔记本电脑上测试了我的应用程序 - 哇。图像非常清晰漂亮。
我唯一的解决方案是创建一个自定义控件,将 ImageSource 转换为 System.Drawing.Bitmap,在此处重新缩放以匹配图像尺寸并将其转换回 WPF ImageSource。显然不是完美的解决方案!
所以问题是:为什么我的图像在低 dpi 显示器上看起来很模糊?我不知道是什么原因造成的,希望有人有办法解决这个问题。
这是我发现的一些最有用的资源:
- http://blogs.msdn.com/b/dwayneneed/archive/2007/10/05/blurry-bitmaps.aspx
- http://www.nbdtech.com/Blog/archive/2008/11/20/blurred-images-in-wpf.aspx
- http://www.hanselman.com/blog/BeAwareOfDPIWithImagePNGsInWPFImagesScaleWeirdOrAreBlurry.aspx
- 在 WPF 图像上禁用抗锯齿
编辑:
这里要求的是转换 ImageSource 的代码。有几个方法调用不包括在内,但您可以通过 google 快速找到它们。
// Calculate the conversion factor.
float dpi = WpfImageHelper.GetCurrentDPI(this);
float factor = dpi / 96f;
int width = (int)Math.Round(this.image.ActualWidth * factor);
int height = (int)Math.Round(this.image.ActualHeight * factor);
// Create bitmaps.
Bitmap oldBitmap = WpfImageHelper.ToWinFormsBitmap2((BitmapSource)this.Source);
Bitmap newBitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
// Draw the new bitmap. Use high-quality interpolation mode.
using (Graphics g = Graphics.FromImage(newBitmap))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.Clear(System.Drawing.Color.Transparent);
g.DrawImage(oldBitmap, 0, 0, newBitmap.Width, newBitmap.Height);
}
// Set the image source to the resized bitmap.
this.Source = WpfImageHelper.ToWpfBitmap2(newBitmap);