How to convert RenderTargetBitmap to CanvasBitmap?

SmilingMoon 981 Reputation points
2020-12-02T19:55:06.38+00:00

I got RenderTargetBitmap object. I like to manipulate image and Bind to Image control.
So, I created the following method to do imgCtrl.Source = await CropRedrawImage(renderTargetSourceImg);

However, SoftwareBitmap.CreateCopyFromBuffer or/and CanvasBitmap.CreateFromSoftwareBitmap throws exception.
CreateFromSoftwareBitmap throws "'The bitmap pixel format is unsupported."
CreateCopyFromBuffer throws 'Insufficient memory for response' for Rgba16 type.

How to avoid the exception and get the method worked?

async Task<BitmapImage> CropRedrawImage(RenderTargetBitmap sourceImg)
        {
            float dpi = 96;
            using (CanvasDevice device = CanvasDevice.GetSharedDevice())
            {
                using (var renderTarget = new CanvasRenderTarget(device,
                                                         sourceImg.PixelWidth, sourceImg.PixelHeight, dpi))// dpi); if it use dpi other than 96, it resizes.
                {
                    using (var ds = renderTarget.CreateDrawingSession())
                    {
                        Windows.Storage.Streams.IBuffer buffer = await sourceImg.GetPixelsAsync();
                        SoftwareBitmap bitmap = SoftwareBitmap.CreateCopyFromBuffer(buffer, BitmapPixelFormat.Bgra8, sourceImg.PixelWidth, sourceImg.PixelHeight);
                        //BitmapPixelFormat.Rgba8, Bgra8 -> NOT supported format by CanvasBitmap.CreateFromSoftwareBitmap, Rgba16 -> Throw exception, 
                        // Rgba16 -> Throw 'Insufficient memory for response. (Exception from HRESULT: 0xC00D7170)'
                        CanvasBitmap image = CanvasBitmap.CreateFromSoftwareBitmap(device, bitmap);
                        //=> Throws System.Exception: 'The bitmap pixel format is unsupported. (Exception from HRESULT: 0x88982F80)'


                        ds.Clear(Colors.Transparent);
                        Rect dest = new Rect(0, 0, 100, 100); //Test.
                        Rect src = new Rect(0, 0, 100, 100);
                        ds.DrawImage(image, dest, src);
                    }

                    //convert CanvasRenderTarget to BitmapImage which is bound to Image control's ImageSource.
                    BitmapImage bmpImg = new BitmapImage();
                    using (var stream = new InMemoryRandomAccessStream())
                    {
                        await renderTarget.SaveAsync(stream, CanvasBitmapFileFormat.Png);


                        stream.Seek(0);
                        await bmpImg.SetSourceAsync(stream);
                    }
                    return bmpImg;
                }
            }

        }
Developer technologies | Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2020-12-03T02:59:35.187+00:00

    Hello @SkyKim-0611 ,

    Welcome to Microsoft Q&A!

    The unexpected behavior is caused by the alpha mode of the SoftwareBitmap. After testing, when you create a SoftwareBitmap object, you could set the BitmapAlphaMode to Ignore.

    Like the following code.

                        SoftwareBitmap bitmap = SoftwareBitmap.CreateCopyFromBuffer(buffer, BitmapPixelFormat.Bgra8, renderTargetBitmap.PixelWidth, renderTargetBitmap.PixelHeight, BitmapAlphaMode.Ignore);  
    

    Then there should be no exception in your code.

    Thank you.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.