繁体   English   中英

从WriteableBitmap为Windows Phone UWP创建BitmapDecoder的异常

[英]Exception creating BitmapDecoder from WriteableBitmap for Windows Phone UWP

在我的UWP Windows 10移动应用程序中,我试图访问和操纵PixelBuffer中给定WriteableBitmap的单个像素的透明度。 我遇到的问题是BitmapDecoder.CreateAsync()正在抛出

“无法找到该组件。(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构造函数为流提供Codec,它会抛出一个不同的异常:

指定演员表无效。

            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组件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM