【问题标题】:How to get the width and height of a BitmapImage in metro (UWP)?如何在 Metro (UWP) 中获取 BitmapImage 的宽度和高度?
【发布时间】:2012-07-19 06:12:49
【问题描述】:

我正在从事地铁应用程序。

我使用的是BitmapImage,自从构造函数以来我设置了Uri,但我意识到PixelWidthPixelHeight一直返回0(我也验证了@ 987654326@是正确的)。

我找不到另一个返回这些属性的类。我在想是否有办法通过字节数组或类似的方式获取这些数据。你怎么看?

【问题讨论】:

  • 假设您是从本地文件系统/资源加载,尝试等到下一个主线程循环加载位图。如果它来自网络/互联网源,您可能需要等待下载的时间长一点。

标签: c# xaml uwp microsoft-metro bitmapimage


【解决方案1】:

检查 CacheOption 属性并设置为 BitmapCacheOption.None。您也可以尝试转换为 WriteableBitmap。 如果我说的不是真的,对不起,因为我无法测试。 Xaml(仅限 wpf)xD

【讨论】:

  • 我已将 CacheOption 设置为 BitmapCacheOption.None 但它不起作用
  • 请记住,在将图像传递给源之前,您必须更改 BitmapCache。那么然后尝试投射一个 WriteableBitmap....
【解决方案2】:

BitmapImagePixelWidthPixelHeight 返回0(如果未加载图像)。图像仅在实际使用时加载,例如分配给Image.Source 并显示。那是……有问题的。使用BitmapCacheOption.None 没有帮助,它只适用于 WP7。

正确的解决方案是使用BitmapDecoder:

var stream = await storageFile.OpenReadAsync();
var decoder = await BitmapDecoder.CreateAsync(stream);
var size = new BitmapSize { Width = decoder.PixelWidth, Height = decoder.PixelHeight };

找到this solution on MSDN forums

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-12
    • 2014-05-01
    • 2011-08-13
    • 1970-01-01
    • 2011-08-06
    • 2019-12-26
    • 2011-09-25
    • 1970-01-01
    相关资源
    最近更新 更多