【问题标题】:converting Image raw data to bitmapimage in windows universal app在 Windows 通用应用程序中将图像原始数据转换为位图图像
【发布时间】:2015-07-24 16:41:35
【问题描述】:

我正在尝试实现一个 Windows 通用应用程序。我面临一个将图像原始数据(字节数组)转换为 BitmapImage 控件的问题。我不知道图像原始数据的类型。我使用了下面的代码,

private async Task<BitmapImage> ByteArrayToBitmapImage(byte[] byteArray)
{
    var bitmapImage = new BitmapImage();

    var stream = new InMemoryRandomAccessStream();
    await stream.WriteAsync(byteArray.AsBuffer());
    stream.Seek(0);

    bitmapImage.SetSource(stream);
    return bitmapImage;
}

图像不显示在窗口中。调试的时候发现bitmapImage对象的高宽都是0。

如果有人知道这个问题的解决方案,请帮助我

【问题讨论】:

    标签: win-universal-app


    【解决方案1】:

    当我使用下面的代码时,我的问题终于解决了,

        var bmp = new WriteableBitmap(320, 240);
        using (var stream = bmp.PixelBuffer.AsStream())
        {
            stream.Write(bytes, 0, bytes.Length);
            myImage.Source = bmp;
        }
    

    【讨论】:

      【解决方案2】:

      我正在使用相同的代码并为我工作,但我创建了 BitmapImage 并将其源设置在调度程序的线程上 - 也许这就是你问题的关键。

      【讨论】:

      • 我试过了。但没有希望.. .BitmapImage 对象的 pixelheight 和 pixelWidth 属性似乎为零。
      • 这里的图像原始数据不包含标题数据。这就是问题所在。
      猜你喜欢
      • 1970-01-01
      • 2019-08-05
      • 2022-01-09
      • 2021-02-03
      • 2014-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-07
      相关资源
      最近更新 更多