我正在尝试从本地存储中读取BitmapImage
。该路径由FileOpenPicker
选择。但是当我尝试将其转换为Byte[]
时,它就会失败。似乎根本没有下载图像,因为image.PixelWidth
返回'0'。
StorageFile storageFile = args.Files[0];
//var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
//var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
//await bitmapImage.SetSourceAsync(stream);
var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
BitmapImage wb = new BitmapImage(new Uri(storageFile.Path));
byte[] pixeBuffer = null;
using (MemoryStream ms = new MemoryStream())
{
WriteableBitmap wbit = new WriteableBitmap(wb.PixelWidth,wb.PixelHeight);
Stream s1 = wb.PixelBuffer.AsStream();
s1.CopyTo(ms);
pixeBuffer = ms.ToArray();
}
程序在
崩溃 WriteableBitmap wbit = new WriteableBitmap(wb.PixelWidth,wb.PixelHeight);
我已检查storageFile.Path
指向正确的路径,我也尝试过来自互联网URL
,但结果相同。
我是C#的新手并且不知道如何触发Windows手机的异常处理,请指导我是否有所帮助。
答案 0 :(得分:0)
在调用解码器后,您应该执行以下操作:
try
{
PixelDataProvider provider = await decoder.GetPixelDataAsync(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, new BitmapTransform(), ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb);
byte[] pixelBuffer = provider.DetachPixelData();
}
catch(Exception ex)
{
Debug.WriteLine(ex);
}
try / catch块是您希望在代码中捕获异常的方式,因此我也将其包括在内。