Exception creating BitmapDecoder from WriteableBitmap for Windows Phone UWP

3

In my UWP Windows 10 Mobile app, I am attempting to access and manipulate the transparency of individual pixels in the PixelBuffer for a given WriteableBitmap. The issue I'm having is that BitmapDecoder.CreateAsync() is throwing

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

I have spent WAY too much time searching, refactoring and debugging this to no avail; any hints, direction or help of any kind would be much appreciated.

        // 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();  

UPDATE: In case this helps spark some ideas, I found that if I use the overloaded CreateAsync constructor that supplies a Codec with the stream, it throws a different exception:

Specified cast is not valid.

            Guid BitmapEncoderGuid = BitmapEncoder.PngEncoderId;
            decoder =  await BitmapDecoder.CreateAsync(BitmapEncoderGuid, stream);  

It gives the same exception no matter which codec I supply (e.g. Png, Jpeg, GIF, Tiff, Bmp, JpegXR )

c#
mobile
uwp
asked on Stack Overflow Jan 12, 2016 by Brandt Bridges • edited Jan 12, 2016 by Brandt Bridges

1 Answer

2

I don't understand, why you want to use a BitmapDecoder at all. The pixel data in WriteableBitmap is not encoded in any way. You would need a BitmapDecoder if you were loading the image from a file stream in a particular compression format - this would require you to use the correct codec.

You can just read the pixel data directly from the stream:

byte[] pixels;
using (var stream = img.PixelBuffer.AsStream())
{
    pixels = new byte[(uint)stream.Length];
    await stream.ReadAsync(pixels, 0, pixels.Length);
}

This will give you a byte array containing 4 bytes for each pixel, corresponding to their R, G, B and A components.

answered on Stack Overflow Jan 12, 2016 by Damir Arh

User contributions licensed under CC BY-SA 3.0