Convert back a byte array to imagebrush

Pierre Saintoyant 21 Reputation points
2021-02-11T17:54:00.53+00:00

I am updating an image. I create a byte[] from that picture no problem. I update (change some pixels). I want to display it back in a Canvas (context c#, UWP) I have tried the following code:

 private async void DisplayBytes(byte[] array)
        {
            var writeableBitmapSource = new WriteableBitmap((int)c_OriginalWidth, (int)c_OriginalHeight);
            using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
            {
                await stream.WriteAsync(array.AsBuffer());

                var a = stream.Size;
                var b = stream.Position;
                writeableBitmapSource.SetSource(stream);
            }
            ImageBrush localbrush = new ImageBrush
            {
                ImageSource = writeableBitmapSource,
                Stretch = Stretch.Uniform
            };
            ImagePanelTarget.Background = localbrush;
        }

I just get blank page; what am I doing wrong?

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

Accepted answer
  1. Yan Gu - MSFT 2,676 Reputation points
    2021-02-12T02:38:34.773+00:00

    Hello,

    Welcome to Microsoft Q&A.

    Please try the following code as an approach:

    private async void DisplayBytes(byte[] array)  
    {  
        var writeableBitmapSource = new WriteableBitmap((int)c_OriginalWidth, (int)c_OriginalHeight);  
        using (Stream stream = writeableBitmapSource.PixelBuffer.AsStream())  
        {  
            await stream.WriteAsync(array, 0, array.Length);  
        }  
        ImageBrush localbrush = new ImageBrush  
        {  
            ImageSource = writeableBitmapSource,  
            Stretch = Stretch.Uniform  
        };  
        ImagePanelTarget.Background = localbrush;  
    }  
    

    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.


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.