【问题标题】:How to convert a stream to BitmapImage?如何将流转换为 BitmapImage?
【发布时间】:2017-08-30 01:05:49
【问题描述】:

我正在尝试使用以下代码将 MemoryStream 转换为 Image。

  Stream stream = new MemoryStream(bytes);
  BitmapImage bitmapImage = new BitmapImage();
  await bitmapImage.SetSourceAsync(stream.AsRandomAccessStream());

但它在SetSourceAsync方法中抛出异常,异常是

System.Exception 未被用户代码处理 H结果=-2003292336 Message=找不到组件。 (HRESULT 的例外情况: 0x88982F50) 源=mscorlib 堆栈跟踪: 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotificat 离子(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter.GetResult() 在 ImageEditor_UWP.MainPage.d__1.MoveNext() InnerException:

如何将流转换为图像?

【问题讨论】:

标签: c# uwp bitmapimage


【解决方案1】:

到图片:

public async static System.Threading.Tasks.Task<BitmapImage> ImageFromBytes(byte[] bytes)
{
    var image = new BitmapImage();

    try
    {
        var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
        await stream.WriteAsync(bytes.AsBuffer());
        stream.Seek(0);
        await image.SetSourceAsync(stream);
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.Message);
    }

    return image;
}

【讨论】:

  • BitmapImage 位图 = new BitmapImage();等待 bitmap.SetSourceAsync(stream); image.Source = 位图;这也对我有用。
【解决方案2】:

试试这个

var img = Bitmap.FromStream(stream);

【讨论】:

  • 目前 Bitmap 类在 UWP windows 10 中不存在
  • 这是 WinForms,而问题是关于 UWP。
猜你喜欢
  • 2011-07-17
  • 2012-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
相关资源
最近更新 更多