在 WPF 中平滑图像边缘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14490574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:11:58  来源:igfitidea点击:

Smoothing image edges in WPF

c#wpfimageantialiasing

提问by Wilson

I have a few 256x256 images I am using with Image controls in a WPF application. Although they are all 256x256, I changed some of the controls to 64x64, etc. When I resize it down (using fill as my stretch property), the edges become very rough. Even on something as simple as a triangle, it is painfully apparent:

我有一些 256x256 图像,我在 WPF 应用程序中与图像控件一起使用。虽然它们都是 256x256,但我将一些控件更改为 64x64 等。当我将其缩小(使用填充作为我的拉伸属性)时,边缘变得非常粗糙。即使是像三角形这样简单的东西,也很明显:

enter image description here

在此处输入图片说明

Is there a good fix for this, or do i need to resize the images in photoshop before putting them into the application?

是否有一个很好的解决方法,或者我是否需要在将它们放入应用程序之前在 photoshop 中调整图像的大小?

回答by d.moncada

You need to set the Image's render options.

您需要设置图像的渲染选项。

Try setting the style of the Image to:

尝试将图像的样式设置为:

<Window.Resources>
    <Style TargetType="Image">
        <Setter Property="Height" Value="64" />
        <Setter Property="Width" Value="64" />
        <Setter Property="RenderOptions.BitmapScalingMode" Value="HighQuality" />
    </Style>
</Window.Resources>

To use the image, simply call as before:

要使用图像,只需像以前一样调用:

<Image Source="/Images/MyImage.png" />

Or, to use the RenderOptionson a single image:

或者,RenderOptions在单个图像上使用:

<Image RenderOptions.BitmapScalingMode="HighQuality"
       Source="/Images/MyImage.png"
       Width="64"
       Height="64" />

For more info see:

有关更多信息,请参阅:

http://msdn.microsoft.com/en-us/library/system.windows.media.renderoptions.aspx

http://msdn.microsoft.com/en-us/library/system.windows.media.renderoptions.aspx