在WPF图像禁用抗锯齿 [英] Disabling antialiasing on a WPF image

查看:839
本文介绍了在WPF图像禁用抗锯齿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个小的登录对话框,并嵌入了一个横幅审美的原因对话框的顶部。一切都很顺利,但在默认情况下,WPF反别名整个图像,使得被包含在该文本是令人沮丧的模糊。

I'm writing a small Login dialog, and have embedded a banner at the top of the dialog for aesthetic reasons. All went well, except that by default, WPF anti aliases the entire image, making the text that was contained within it frustrating blurry.

在进行位的搜索,结果的前几页显示,这是共同的信念,抗混叠不能禁用WPF。可以在任何确认,或以其他方式否认这一点?

After a bit of searching, the first few pages of results showed that it's common belief that anti aliasing cannot be disable in WPF. Can any confirm, or otherwise deny this?

这对我来说是一个小问题 - 我要的文字出来的形象,而是叠加与背景图像来达到同样的效果(尽管我必须承认上面相同的文字标签,这是一个有点恼人)。

It's a minor issue for me - I'll take the text out of the image and instead superimpose a label with the same text on top of the background image to achieve the same effect (though I must admit, it's a bit annoying).

谢谢, 罗布

推荐答案

据我所知,WPF总是缩放位图时,确实抗锯齿。然而,你应该能够避免的位图​​缩放来完成自己的目标。

As far as I know, WPF always does anti-aliasing when scaling a bitmap. However you should be able to accomplish your goal by avoiding the bitmap scaling.

有两个步骤:

  1. 设置 SnapsToDevicePixels =真正的在图像
  2. 设置在图像上ScaleTransform扩展它,这样一台设备像素=一个位图像素
  1. Set SnapsToDevicePixels="true" on your image
  2. Set a ScaleTransform on your image to scale it so that one device pixel = one bitmap pixel

要计算所需的ScaleTransform,计算你的屏幕的DPI是这样的:

To compute the needed ScaleTransform, compute your screen's DPI like this:

var DPI = Win32Functions.GetSystemMetrics(SM_CYICON) / SystemParameters.IconHeight * 96;

,然后为位图,做的:

and then for the bitmap, do:

var scale = bitmapDPI / DPI;
var transform = new ScaleTransform(scale, scale);

这将导致您的位图的像素正好与设备像素相匹配。 WPF不会延长位图,所以应该没有抗锯齿。

This will cause your bitmap's pixels to exactly match with the device pixels. WPF will not stretch the bitmap, so there should be no anti-aliasing.

如果你想伸展你的图像上高DPI屏幕,但这样做没有抗锯齿(如双所有像素),就可以使用任意的算法,你喜欢伸展位在自己的code和使用以上拉伸位图。

If you do want to stretch your image on high DPI screens but do so without anti-aliasing (eg double all pixels), just stretch the bitmap in your own code using whichever algorithm you like and use the above with the stretched bitmap.

这篇关于在WPF图像禁用抗锯齿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆