C# 在不损失质量的情况下调整 xaml 中的图像大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19302061/
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
Resize image in xaml without losing quality
提问by Naty Bizz
I have this image (original size: 256x256)
我有这张图片(原始尺寸:256x256)
I made this xaml definition to show the image in my application
我做了这个 xaml 定义来在我的应用程序中显示图像
<Image Grid.Row="1"
Source="/MyProject;component/Images/happy.png"
Stretch="Fill"
Width="64" Height="64"
VerticalAlignment="Top" Margin="0,0,0,0"
HorizontalAlignment="Center" />
And I get this result
我得到了这个结果
How can I made a more smooth resize?
我怎样才能更平滑地调整大小?
采纳答案by Daniel
Include RenderOptions.BitmapScalingMode="Fant"
on your Image, like so:
包括RenderOptions.BitmapScalingMode="Fant"
在您的图像中,如下所示:
<Image Grid.Row="1"
Source="/MyProject;component/Images/happy.png"
RenderOptions.BitmapScalingMode="Fant"
Stretch="Fill"
Width="64"
Height="64"
VerticalAlignment="Top"
Margin="0,0,0,0"
HorizontalAlignment="Center" />
回答by Farhad Jabiyev
Set RenderOptions.BitmapScalingMode
property for your Image
through .xaml
:
RenderOptions.BitmapScalingMode
为您的Image
through设置属性.xaml
:
<Image Grid.Row="1" RenderOptions.BitmapScalingMode="HighQuality" ... />
Additional info:
附加信息:
The RenderOptions.BitmapScalingMode
is a property that scales the images based on the quality.
WPF 4.0 defaults it to Unspecified
, which refers to LowQuality
image rendering.
的RenderOptions.BitmapScalingMode
是,它可以扩展基于质量的图像性能。WPF 4.0 默认为Unspecified
,指的是LowQuality
图像渲染。
But to ensure that the image remains good quality when the size increases, BitmapScalingMode
should be chosen as HighQuality
.
但是为了保证图像在尺寸增加时保持良好的质量,BitmapScalingMode 应该选择为HighQuality
。
Here is BitmapScalingModeEnumeration members with their description from msdn:
这是BitmapScalingMode枚举成员及其来自msdn的描述:
1.Fant- Use very high quality Fant bitmap scaling, which is slower than all other bitmap scaling modes, but produces higher quality output.
2.HighQuality- Use high quality bitmap scaling, which is slower than LowQuality mode, but produces higher quality output. The HighQuality mode is the same as the Fant mode.
3.Linear- Use linear bitmap scaling, which is faster than HighQuality mode, but produces lower quality output.
4.LowQuality- Use bilinear bitmap scaling, which is faster than HighQuality mode, but produces lower quality output. The LowQuality mode is the same as the Linear mode.
5.NearestNeighbor- Use nearest-neighbor bitmap scaling, which provides performance benefits over LowQuality mode when the software rasterizer is used. This mode is often used to magnify a bitmap.
6.Unspecified- Use the default bitmap scaling mode, which is Linear.
1. Fant- 使用非常高质量的 Fant 位图缩放,这比所有其他位图缩放模式慢,但产生更高质量的输出。
2. HighQuality- 使用高质量位图缩放,这比 LowQuality 模式慢,但产生更高质量的输出。HighQuality 模式与 Fant 模式相同。
3.线性- 使用线性位图缩放,这比 HighQuality 模式快,但输出质量较低。
4. LowQuality- 使用双线性位图缩放,这比 HighQuality 模式快,但输出质量较低。LowQuality 模式与线性模式相同。
5. NearestNeighbor- 使用最近邻位图缩放,当使用软件光栅化器时,它比 LowQuality 模式提供性能优势。此模式通常用于放大位图。
6.未指定- 使用默认位图缩放模式,即线性。
回答by Ben
As answered above, the setting RenderOptions.BitmapScalingMode="HighQuality"
activates the antialiasing. I'd like to provide an example for users who don't know what antialiasing is.
如上所述,该设置会RenderOptions.BitmapScalingMode="HighQuality"
激活抗锯齿。我想为不知道什么是抗锯齿的用户提供一个示例。
Without this setting :
没有这个设置:
<Image x:Name="InstrumentImage" />
With this setting :
使用此设置:
<Image x:Name="InstrumentImage" RenderOptions.BitmapScalingMode="HighQuality" />
See the different options here : https://docs.microsoft.com/en-us/dotnet/api/system.windows.media.bitmapscalingmode?view=netframework-4.8
在此处查看不同的选项:https: //docs.microsoft.com/en-us/dotnet/api/system.windows.media.bitmapscalingmode?view=netframework-4.8