c# - 从 Windows Phone UWP 的 WriteableBitmap 创建 BitmapDecoder 的异常

标签 c# mobile uwp

在我的 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),它都会给出相同的异常

最佳答案

我不明白,你为什么要使用 BitmapDecoderWriteableBitmap 中的像素数据未以任何方式编码。如果您以特定压缩格式从文件流中加载图像,则需要 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/

相关文章:

javascript - 使鼠标悬停时的 img src 更改适应触摸屏设备

javascript - 创建Parse安装对象UWP winjs

c# - 引用和读取 .sqlite 数据库文件 (C#/UWP)

c#如何让线程等待

c# - 使用 CaSTLe Windsor 进行依赖注入(inject)的多态性

c# - 类型定义应以 '{' 开头,期望序列化类型 'ErrorResponse' ,得到以 : MOCK FOR URL NOT FOUND 开头的字符串

c# - 登录错误 : codes specific to reason why there was an error

android - 测试平板电脑移动网络应用

首次访问移动版 Chrome 时,有时会忽略 HTML 视口(viewport)标记

windows-10 - 如何在通用 Windows 平台应用程序中级联样式?