简体   繁体   English

如何将流转换为BitmapImage?

[英]How to convert a stream to BitmapImage?

I am trying to convert MemoryStream to Image by using the following code. 我正在尝试通过使用以下代码将MemoryStream转换为Image。

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

but it throws an exception in the SetSourceAsync method and the exception is 但它在SetSourceAsync方法中引发异常,并且该异常是

System.Exception was unhandled by user code HResult=-2003292336 Message=The component cannot be found. 用户代码未处理System.Exception HResult = -2003292336消息=找不到组件。 (Exception from HRESULT: 0x88982F50) Source=mscorlib StackTrace: at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotificat ion(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at ImageEditor_UWP.MainPage.d__1.MoveNext() InnerException: (来自HRESULT的异常:0x88982F50)Source = mscorlib StackTrace:位于System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)位于System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务),位于System.Runtime.CompilerService ImageEditor_UWP.MainPage.d__1.MoveNext()上的GetResult()InnerException:

How can I convert a stream to an image? 如何将流转换为图像?

To image: 印象:

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;
}

尝试这个

var img = Bitmap.FromStream(stream);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM