Skip to main content
 首页 » 编程设计

c#之WPF 中的图像变得模糊

2024年02月06日12傻小

我正在尝试在 WPF 中制作一些东西,但我遇到了一个问题。我在窗口中放置了一些元素,因此在编辑器中它看起来是这样的:

在构建中它看起来是这样的:

如果我放置到元素->

RenderOptions.BitmapScalingMode="NearestNeighbor" RenderOptions.EdgeMode="Aliased" 

现在构建它:

我真的不知道怎么解决,所以我真的很感激有人能帮助我

请您参考如下方法:

这通常与未设置 SnapsToDevicePixels 有关。

Pixel Snapping in WPF Applications

在你的控件或窗口中,你可以这样设置

<MyControl SnapsToDevicePixels="True"> 

The WPF graphics system uses device-independent units to enable resolution and device independence. Each device independent pixel automatically scales with the system's dots per inch (dpi) setting. This provides WPF applications proper scaling for different dpi settings and makes the application automatically dpi-aware.

However, this dpi independence can create irregular edge rendering due to anti-aliasing. These artifacts, commonly seen as blurry, or semi-transparent, edges can occur when the location of an edge falls in the middle of a device pixel rather than between device pixels. To address this issue, WPF provides a way for object edges in a visual tree to snap, or become fixed, to device pixels through pixel snapping, eliminating the semi-transparent edges produced by anti-aliasing.

Pixel snapping is a means to suppress these visual artifacts by applying small offsets to the geometry of the visual to align the geometry to device pixels.