How to turn an array of bytes into an image in WPF?

Asked

Viewed 117 times

2

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

I need to turn him into an image, only how I’m in one WPF it is not possible to simply play a bitmap within a PictureBox because that element simply does not exist in the 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;

Obs: m_picture is a <Image/>

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

What I need to do?

2 answers

1


My solution

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

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 needed 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 I play inside the host, and the same, into a StackPanelof my project WPF

But to actually arrive at an image from mine []byte m_Frame I created a Bitmap with some features of a API and played inside the PictureBox.

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

  • When running the img line = System.Drawing.Image.Fromstream(Imagedatastream) this error is returning: System.Argumentexception Parameter is not Valid. You know what it can be?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.