【问题标题】:How to Resize an image on an Image Control?如何调整图像控件上的图像大小?
【发布时间】:2015-09-16 19:33:49
【问题描述】:

我正在尝试使用 UWP Image control (XAML) 调整图像的大小

ScaleTransform t = (ScaleTransform)image.RenderTransform;

但我得到一个错误:

无法转换类型为“Windows.UI.Xaml.Media.MatrixTransform”的对象 键入“Windows.UI.Xaml.Media.ScaleTransform”。

那么如何调整它的大小(不使用Stretch 属性)?

【问题讨论】:

  • 什么是“通用 Windows 映像”?
  • @DaveInCaz 我的意思是显示在 UWP 图像控件上的图像。现已编辑并包含文档链接。

标签: c# .net xaml win-universal-app


【解决方案1】:

现有的 RenderTransform 是 MatrixTransform 类型,不能转换为 ScaleTransform。

您可以用新的 ScaleTransform 替换现有的 MatrixTransform:

image.RenderTransform = new ScaleTransform(2, 2);

或者您可以使用所需的比例更新现有的 MatrixTransform:

(image.RenderTransform as MatrixTransform).Matrix = new MatrixTransform(2, 0, 0, 2, 0, 0);

【讨论】:

  • 谢谢。我现在更好地理解了这一点。
【解决方案2】:

将新的 ScaleTransform 分配给 RenderTransform 属性一次:

image.RenderTransform = new ScaleTransform();

现在您以后可以通过

安全地访问它
var t = (ScaleTransform)image.RenderTransform

【讨论】:

  • 谢谢!有没有办法获取图像的大小(不是控件的大小)?
  • 不确定你的意思。您正在将 Image 控件的 Source 属性设置为某个值,例如具有 Width 和 Height 属性的 BitmapImage。
  • 我将Source 属性设置为文件的路径。我假设该文件被读取并存储在某个位图中的某个位置。有没有办法在代码中获取它?
  • 在后面的代码中获取 Source 属性。它应该是一个 BitmapImage(因此您可以将其转换为 BitmapImage 类型),它具有 PixelWidth 和 PixelHeight 属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 1970-01-01
  • 1970-01-01
  • 2020-10-02
  • 1970-01-01
相关资源
最近更新 更多