-1

I'm trying to use the method SetSource in my converter to convert a byte array to BitmapImage, but unfortunately the method looks gone.

I checked the documentation and the method is still in use. Link: https://learn.microsoft.com/en-us/previous-versions/cc189859(v=vs.95)

Any Ideas ?

public static BitmapImage ConvertByteArrayToBitMapImage(byte[] imageByteArray)
    {
        BitmapImage img = new BitmapImage();
        using (MemoryStream memStream = new MemoryStream(imageByteArray))
        {
            img.SetSource(memStream);
        }
        return img;
    }
Clemens
  • 123,504
  • 12
  • 155
  • 268
Boufar Tarek
  • 5
  • 1
  • 2
  • 1
    This method is from Silverlight and is indeed dead. Instead, look at the following question: [Creating WPF BitmapImage from MemoryStream png, gif](https://stackoverflow.com/questions/2097152/creating-wpf-bitmapimage-from-memorystream-png-gif) – Etienne de Martel Feb 03 '22 at 02:20
  • There is still a SetSource method in the UWP/WinUI BitmapImage, but you would prefer SetSourceAsync there. – Clemens Feb 03 '22 at 07:32

1 Answers1

0

I have to signal the start and end of the initialization of the BitmapImage, and also set BitmapCacheOption.OnLoad when the stream is closed after EndInit.

using (MemoryStream memStream = new MemoryStream(imageByteArray))
{ 
    img.BeginInit();
    img.StreamSource = memStream;
    img.CacheOption = BitmapCacheOption.OnLoad;
    img.EndInit();
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
Boufar Tarek
  • 5
  • 1
  • 2