gpt4 book ai didi

c# - Windows Phone 8.1 创建 WriteableBiitmap 时出现异常

转载 作者:太空宇宙 更新时间:2023-11-03 13:12:29 24 4
gpt4 key购买 nike

我正在制作一个 Windows Phone 8.1 应用程序(Windows 运行时,而不是 Silverlight 8.1),我需要从流创建一个 WriteableBitmap,但是当我尝试这样做时,我得到了这个异常:“类型异常'System.Exception' 发生在 mscorlib.ni.dll 中,但未在用户代码中处理

Additional information: The component cannot be found. (Exception from HRESULT: 0x88982F50)"

我已经尝试了很多东西,但仍然没有运气。我的代码如下:

private async void Button_Click(object sender, RoutedEventArgs e)
{
var files = await KnownFolders.CameraRoll.GetFilesAsync();
for(int i=0; i<files.Count; i++)
{
var fileStream = await files[i].OpenReadAsync();
if(fileStream != null)
{
WriteableBitmap writeableBmp = await BitmapFactory.New(1, 1).FromStream(fileStream);
}
}
}

但是如果我替换了就不会发生这种情况

var fileStream = await files[i].OpenReadAsync();

例如,

var fileStream = await files[0].OpenReadAsync();

有什么想法吗?谢谢。

最佳答案

这很奇怪。我认为您已经检查了 appxmanifest 文件中的“图片库”功能。

这段代码对我有用:

public static async Task<WriteableBitmap> GetWritableBitmapFromStream(Stream stream)
{
var decoder = await BitmapDecoder.CreateAsync(stream.AsRandomAccessStream());
var pixelData = await decoder.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, new BitmapTransform(), ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage);
var pixels = pixelData.DetachPixelData();

var bmp = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
using (var bmpStream = bmp.PixelBuffer.AsStream())
{
bmpStream.Seek(0, SeekOrigin.Begin);
await bmpStream.WriteAsync(pixels, 0, (int)bmpStream.Length);
}
return bmp;
}

private async void Button_Click(object sender, RoutedEventArgs e)
{
var files = await KnownFolders.CameraRoll.GetFilesAsync();
foreach (var file in files)
{
// need "using System.IO" for this extension method
using (var fileStream = await file.OpenStreamForReadAsync())
{
WriteableBitmap writeableBmp = await GetWritableBitmapFromStream(fileStream);
// do something with writableBmp
}
}
}

关于c# - Windows Phone 8.1 创建 WriteableBiitmap 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27887502/

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