c# - 如何避免 WPF 中的抗锯齿?

标签 c# wpf wpf-controls

在目前正在开发的 WPF 应用程序中,用户可以在 Canvas 上画线,当然在保存后,他可以看到几种不同的文件格式(作为输出,例如 JPEG 和专有文件格式)。这工作正常,但 JPEG 版本应该被裁剪,当裁剪发生时,对象的顶部会被一些额外的像素覆盖。

在代码中我们使用了抗锯齿设置:

EdgeMode.Aliased; 

基于System.Windows.Media.EdgeMode

见下文。现在,当这被注释掉时,一切都很好,因此裁剪没有问题,但是线条会呈现出更锐利的边缘,这是 Not Acceptable 。

有人接触过这个问题吗?如果是这样,这个问题的解决方案或变通方法是什么?

#region Assembly PresentationCore.dll, v4.0.0.0
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\PresentationCore.dll
#endregion

using System;

namespace System.Windows.Media
{
    // Summary:
    //     Determines how the edges of non-text drawing primitives are rendered.
    public enum EdgeMode
    {
        // Summary:
        //     No edge mode is specified. Do not alter the current edge mode of non-text
        //     drawing primitives. This is the default value.
        Unspecified = 0,
        //
        // Summary:
        //     Render the edges of non-text drawing primitives as aliased edges.
        Aliased = 1,
    }
}

谢谢

enter image description here

最佳答案

尝试设置 SnapsToDevicePixels在您的图像上为 True 或者更确切地说,根元素可能在您的窗口上,以防您希望所有 UIElement 自动继承该属性。

来自 MSDN -

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.

您也可以查看此链接 here在 SO 中详细描述了更多其他选项,例如 -

  • 将图像上的 UseLayoutRendering 设置为 true
  • RenderOptions.BitmapScalingMode 设置为 NearestNeighbor

关于c# - 如何避免 WPF 中的抗锯齿?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19842520/

相关文章:

WPF 无法使 ItemContainerGenerator.ContainerFromItem 工作

c# - 组合空检查和模式匹配的 'if' 语句时出错

c# - 在 C# 中模拟 Postman Post - RestSharp

c# - MVVM Light RaisePropertyChanged

c# - 如何将自定义控件派生的 TabItem 添加到 WPF 中的 TabControl?

c# - 无法加载文件或程序集 'CopyModules.dll' 或其依赖项之一。指定的模块无法找到

C# 规范文件名

c# - 有没有办法工厂注入(inject)自定义 DbContext?

wpf - 选项卡控件上选项卡之间的键盘快捷方式

c# - 在 TreeView 中使用 HierarchicalDataTemplates