禁用 WPF 图像上的抗锯齿功能

发布于 2024-08-11 09:24:06 字数 1468 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我恋#小黄人 2024-08-18 09:24:06

据我所知,WPF 在缩放位图时总是会进行抗锯齿处理。但是,您应该能够通过避免位图缩放来实现您的目标。

有两个步骤:

  1. 在图像上设置 SnapsToDevicePixels="true"
  2. 在图像上设置 ScaleTransform 来缩放图像,以便一个设备像素 = 一个位图像素

要计算所需的 ScaleTransform,请计算屏幕的 DPI,如下所示this:

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

然后对于位图,执行:

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

这将使位图的像素与设备像素完全匹配。 WPF 不会拉伸位图,因此不应该有抗锯齿。

如果您确实想在高 DPI 屏幕上拉伸图像,但不使用抗锯齿功能(例如,将所有像素加倍),只需使用您喜欢的算法在您自己的代码中拉伸位图,并将上述内容与拉伸的位图一起使用。

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.

There are two steps:

  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

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

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.

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.

旧时光的容颜 2024-08-18 09:24:06

它并不是真正的抗锯齿 - 导致问题的是子像素定位,我在我的博客上写了相关内容(以及解决问题的控件):

http://www.nbdtech.com/blog/archive/2008/11/20/blurred -wpf.aspx 中的图像

It's not really anti-aliasing - it's sub pixel positioning that causing the problem, I've written about it (and about a control that solves the problem) on my blog at:

http://www.nbdtech.com/blog/archive/2008/11/20/blurred-images-in-wpf.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文