gpt4 book ai didi

c# - 如何将流转换为 BitmapImage?

转载 作者:太空宇宙 更新时间:2023-11-03 16:59:36 27 4
gpt4 key购买 nike

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

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

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

System.Exception was unhandled by user code HResult=-2003292336 Message=The component cannot be found. (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:

如何将流转换为图像?

最佳答案

图片:

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

关于c# - 如何将流转换为 BitmapImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45931723/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com