How do initialize a WPF Image from a bytearray (byte[]) in C# code behind?


I'm building a small app using C#/WPF.

The application receives (from an unmanaged C++ library) a byte array (byte[]) from a bitmap source

In my WPF window, I have an ( image which I will use
to display the bitmap.

In the code behind (C#) I need to able to take that byte array, create BitmapSource /ImageSource
and assign the source for my image control.

// byte array source from unmanaged librariy
byte[] imageData; 

// Image Control Definition
System.Windows.Controls.Image image = new Image() {width = 100, height = 100 };

// Assign the Image Source
image.Source = ConvertByteArrayToImageSource(imageData);

private BitmapSource ConvertByteArrayToImagesource(byte[] imageData)

Open in new window

I've been working on this for a bit here and haven't been able to figure this out.
I've tried several solutions that I've found by goolging around.
To date, I haven't been able to figure this out.

I've tried:

1) Creating a BitmapSource
var stride = ((width * PixelFormats.Bgr24 +31) ?32) *4);
var imageSrc = BitmapSource.Create(width, height, 96d, 96d, PixelFormats.Bgr24, null, imageData, stride);

Open in new window

That through a runtime exception saying buffer was too small
Buffer size is not sufficient

2) I tried using a memory stream:
BitmapImage bitmapImage = new BitmapImage();
using (var mem = new MemoryStream(imageData))
   bitmapImage.CrateOptions = BitmapCreateOptions.PreservePixelFormat;
   bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
   bitmapImage.StreamSource = mem;
   return bitmapImage;

Open in new window

This code through an exception on the EndInit() call.
"No imaging component suitableto complete this operation was found."

I've spent a couple of days on this one and am clearly stuck.
Any help/ideas/direction would be greatly appreciated.

jxbmaSoftware ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aaron JabamaniTechnical ArchitectCommented:
Hope below helps

private static BitmapImageFromByte LoadImage(byte[] imageData)
        if (imageData == null || imageData.Length == 0) return null;
        var image = new BitmapImage();
        using (var mem = new MemoryStream(imageData))
            mem.Position = 0;
            image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
            image.CacheOption = BitmapCacheOption.OnLoad;
            image.UriSource = null;
            image.StreamSource = mem;
        return image;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jxbmaSoftware ConsultantAuthor Commented:
::apeter>  I tried this already, please see above:

This code through an exception on the EndInit() call.
 "No imaging component suitable to complete this operation was found."

The byte array originated from unmanaged library, so it was marshaled to a managed byte array.

What am I missing here?
Is there a way to determine whether the byte array is indeed valid?

jxbmaSoftware ConsultantAuthor Commented:
::>aPeter   I was able to get the BitmapSource.Create() solution to work. The reason I was getting the original error there was because I wasn't including the number of channels when calculating the buffer size for the raw image.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.