代码之家  ›  专栏  ›  技术社区  ›  Santhiya

如何将流转换为位图图像?

  •  2
  • Santhiya  · 技术社区  · 7 年前

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

    但它在 SetSourceAsync 方法,例外情况是

    系统用户代码未处理异常 HResult=-2003292336 源=mscorlib at系统。运行时。编译器服务。任务等待者。ThrowForNonSuccess(任务 任务) 离子(任务) at系统。运行时。编译器服务。任务等待者。GetResult()

    如何将流转换为图像?

    2 回复  |  直到 7 年前
        1
  •  0
  •   Clemens    7 年前

    到图像:

    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;
    }
    
        2
  •  -2
  •   Krish    7 年前

    var img = Bitmap.FromStream(stream);