0

I have a byte buffer (byte[]) of an image. I have to convert byte[] to BitmapSource (System.Windows.Media.Imaging.BitmapSource).

private void WriteAsJpeg(string fileName, BitmapSource bmp)
            {
                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                BitmapFrame outputFrame = BitmapFrame.Create(bmp);
                encoder.Frames.Add(outputFrame);
                encoder.QualityLevel = 100;
                using (FileStream file = File.OpenWrite(fileName))
                {
                    encoder.Save(file);
                }
            }

How can i convert byte[] to BitmapSource in order to call the above specified function.

  • have you seen that:https://stackoverflow.com/questions/25626770/is-it-possible-to-convert-a-byte-to-a-bitmapsource – Frenchy Oct 18 '19 at 11:01

1 Answers1

0
//array is a byte[]
 using (var memoryStream = new System.IO.MemoryStream(array))
    {
    var image = new BitmapImage();
    image.BeginInit();
    image.CacheOption = BitmapCacheOption.OnLoad; 
    image.StreamSource = memoryStream ;
    image.EndInit();

   }
Marwen Jaffel
  • 703
  • 1
  • 6
  • 14