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

查看:41
本文介绍了在 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="true"
  2. 在图像上设置 ScaleTransform 以对其进行缩放,以便一个设备像素 = 一个位图像素

要计算所需的 ScaleTransform,请像这样计算屏幕的 DPI:

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

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

然后对于位图,执行:

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 屏幕上拉伸图像但不使用抗锯齿(例如双倍所有像素),只需使用您喜欢的任何算法在您自己的代码中拉伸位图,然后将上述算法与拉伸位图一起使用.

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天全站免登陆