在我的 UWP Windows 10 Mobile 应用程序中,我试图访问和操作 PixelBuffer 中给定 WriteableBitmap 的各个像素的透明度。我遇到的问题是 BitmapDecoder.CreateAsync()
正在抛出
"The component cannot be found. (Exception from HRESULT: 0x88982F50)".
我花了太多时间搜索、重构和调试它,但都无济于事;任何类型的提示、指导或帮助都将不胜感激。
// img is a WriteableBitmap that contains an image
var stream = img.PixelBuffer.AsStream().AsRandomAccessStream();
BitmapDecoder decoder = null;
try
{
decoder = await BitmapDecoder.CreateAsync(stream);
}
catch(Exception e)
{
// BOOM: The component cannot be found. (Exception from HRESULT: 0x88982F50)
}
// Scale image to appropriate size
BitmapTransform transform = new BitmapTransform()
{
ScaledWidth = Convert.ToUInt32(img.PixelWidth),
ScaledHeight = Convert.ToUInt32(img.PixelHeight)
};
PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
BitmapPixelFormat.Bgra8, // WriteableBitmap uses BGRA format
BitmapAlphaMode.Straight,
transform,
ExifOrientationMode.IgnoreExifOrientation, // This sample ignores Exif orientation
ColorManagementMode.DoNotColorManage
);
// An array containing the decoded image data, which could be modified before being displayed
byte[] pixels = pixelData.DetachPixelData();
更新:如果这有助于激发一些想法,我发现如果我使用为流提供编解码器的重载 CreateAsync 构造函数,它会引发不同的异常:
Specified cast is not valid.
Guid BitmapEncoderGuid = BitmapEncoder.PngEncoderId;
decoder = await BitmapDecoder.CreateAsync(BitmapEncoderGuid, stream);
无论我提供哪种编解码器(例如 Png、Jpeg、GIF、Tiff、Bmp、JpegXR),它都会给出相同的异常
最佳答案
我不明白,你为什么要使用 BitmapDecoder
。 WriteableBitmap
中的像素数据未以任何方式编码。如果您以特定压缩格式从文件流中加载图像,则需要 BitmapDecoder
- 这需要您使用正确的编解码器。
您可以直接从流中读取像素数据:
byte[] pixels;
using (var stream = img.PixelBuffer.AsStream())
{
pixels = new byte[(uint)stream.Length];
await stream.ReadAsync(pixels, 0, pixels.Length);
}
这将为您提供一个 byte
数组,每个像素包含 4 个字节,对应于它们的 R、G、B 和 A 分量。
关于c# - 从 Windows Phone UWP 的 WriteableBitmap 创建 BitmapDecoder 的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34747337/