使用 WPF 中的 ScaleTransform 进行水平图像缩放
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15460578/
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
Horizontal image scaling with ScaleTransform in WPF
提问by Hozuki
I have an image in a WPF window with the default Stretchsetting, Uniform, and am making an attempt to make it fill the screen horizontally. I do not wish to use a different Stretchsetting as this is supposed to a learning experience. The image dimensions being loaded are 420x800. This is the XAML for the window..
我在 WPF 窗口中有一个图像,默认Stretch设置为“统一”,我正在尝试使其水平填充屏幕。我不希望使用不同的Stretch设置,因为这应该是一种学习体验。正在加载的图像尺寸是420x800. 这是窗口的 XAML ..
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="Red" Height="1200" Width="840">
<Image Name="Image" Source="{Binding SourceUri}">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform x:Name="Scale" />
<TranslateTransform x:Name="Translate" />
</TransformGroup>
</Image.RenderTransform>
</Image>
</Window>
On the code-behind, I am attempting to calculate the scaling to zoom the image to fill the horizontal screen and I am using translate transform the move it to the center of the screen. The following bit of code is obviously wrong...
在代码隐藏中,我试图计算缩放以缩放图像以填充水平屏幕,并且我正在使用平移变换将其移动到屏幕的中心。下面的代码显然是错误的......
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WpfApplication1 {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
DataContext = this;
double ImageWidth = 420;
Scale.ScaleX = Width / ImageWidth;
Translate.X = -(ImageWidth / 2);
}
public string SourceUri {
get {
return @"C:\Users\Roel\Desktop\test.png";
}
}
}
}
I am attempting to understand how stretching and transformations are working together but I am having difficulty with this. I would appreciate all insights, even references to detailed explanations, as I have trouble finding any informational source explaining clearly and concisely how the transformations are applied.
我试图了解拉伸和转换是如何协同工作的,但我对此有困难。我将不胜感激所有的见解,甚至是对详细解释的引用,因为我很难找到任何信息来源,以清楚而简洁地解释如何应用转换。
回答by Clemens
You would usually just do this:
你通常会这样做:
<Image Name="Image" Source="{Binding SourceUri}" Stretch="Fill"/>
In case you really need to calculate the stretch transformation manually, you would only need a ScaleTransform, no TranslateTransform, and you would put that into the LayoutTransformof the Image control. Moreover, the Image control would have to be placed into a Grid, which provides the size of the Windows's "client area". You can't calculate anything based on the Window's Width (or ActualWidth) as that includes the width of the Window's borders.
如果您确实需要手动计算拉伸变换,则只需要 a ScaleTransform、 no TranslateTransform,然后将其放入LayoutTransformImage 控件的 。此外,Image 控件必须放置在一个网格中,该网格提供了 Windows 的“客户区”的大小。您无法根据窗口的宽度(或实际宽度)计算任何内容,因为其中包括窗口边框的宽度。
<Grid SizeChanged="Grid_SizeChanged">
<Image Name="image" Source="{Binding SourceUri}">
<Image.LayoutTransform>
<ScaleTransform x:Name="scale"/>
</Image.LayoutTransform>
</Image>
</Grid>
In the Grid's SizeChanged handler you would calculate the scaling as shown below.
在 Grid 的 SizeChanged 处理程序中,您将按如下所示计算缩放比例。
private void Grid_SizeChanged(object sender, SizeChangedEventArgs e)
{
scale.ScaleX = e.NewSize.Width / image.Source.Width;
scale.ScaleY = e.NewSize.Height / image.Source.Height;
}

