How to transform a byte array into an image in WPF?

2

I have an array of bytes like this: private byte[] m_Frame

I need to turn it into an image, but since I'm in a WPF it's not possible to just play a bitmap within a PictureBox because that element simply does not exist in WPF .

For now, I have this development:

BitmapImage bmpi = new BitmapImage();
bmpi.BeginInit();
bmpi.StreamSource = new MemoryStream(m_Frame);
bmpi.EndInit();
m_picture.Source = bmpi;

Note: m_picture is <Image/>

But when I call the method that this excerpt is inserted the VS returns me a Exception:System.NotSupportedException: 'No imaging component suitable to complete this operation was found.

What do I need to do?

    
asked by anonymous 27.06.2018 / 19:42

2 answers

1

My solution

To solve the problem, I actually needed to create a PictureBox of WF within my WPF project.

To do this, I first added references to the following assemblies.

  
  • WindowsFormsIntegration
  •   
  • System.Windows.Forms
  •   

And within the logic of my program:

        System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost();

This creates the interaction required to create elements WF

PictureBox m_picture = new PictureBox();
host.Child = m_picture;
this.myStackPanel.Children.Add(host);

I create my PictureBox here and then play it inside host , and the same, into a StackPanel of my project WPF

But to actually get an image from my []byte m_Frame I created a Bitmap with some features of a API and played within PictureBox .

    
28.06.2018 / 22:23
0

Use a MemoryStream:

System.Drawing.Image img = null;

using (MemoryStream ImageDataStream = new MemoryStream())
{
    ImageDataStream.Write(bData, 0, bData.Length);
    ImageDataStream.Position = 0;
    bData = System.Text.UnicodeEncoding.Convert(Encoding.Unicode, Encoding.Default, bData);
    img = System.Drawing.Image.FromStream(ImageDataStream);
}
return img;

where bData is the byte array

    
27.06.2018 / 20:13