[英]How to Resize an image on an Image Control?
I'm trying to resize an image on a UWP Image control (XAML) using我正在尝试使用以下方法调整UWP 图像控件(XAML) 上的图像大小
ScaleTransform t = (ScaleTransform)image.RenderTransform;
But am getting an error:但是我收到一个错误:
Unable to cast object of type 'Windows.UI.Xaml.Media.MatrixTransform' to type 'Windows.UI.Xaml.Media.ScaleTransform'.
无法将“Windows.UI.Xaml.Media.MatrixTransform”类型的对象转换为“Windows.UI.Xaml.Media.ScaleTransform”类型。
So how do I resize it (not using the Stretch
property)?那么,如何调整它的大小(不使用
Stretch
属性)?
The existing RenderTransform is of type MatrixTransform which cannot be cast to a ScaleTransform.现有的 RenderTransform 属于 MatrixTransform 类型,不能转换为 ScaleTransform。
You can either replace the existing MatrixTransform with a new ScaleTransform:您可以使用新的 ScaleTransform 替换现有的 MatrixTransform:
image.RenderTransform = new ScaleTransform(2, 2);
or you can update the existing MatrixTransform with the desired scale:或者您可以使用所需的比例更新现有的 MatrixTransform:
(image.RenderTransform as MatrixTransform).Matrix = new MatrixTransform(2, 0, 0, 2, 0, 0);
Assign a new ScaleTransform to the RenderTransform
property once:将新的 ScaleTransform 分配给
RenderTransform
属性一次:
image.RenderTransform = new ScaleTransform();
Now you can later safely access it by现在您以后可以通过以下方式安全地访问它
var t = (ScaleTransform)image.RenderTransform
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.