Click here to Skip to main content
14,635,966 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello everybody,

I needed a function to convert image data stored in byte array to a format, that can be displayed using some WPF controls. The byte array contains image data as is stored on harddisk, so there're also the header data. I've already had the code for System.Drawing.Image, which worked fine and I tried to write a WPF analogue to it.

Currently, I have this:
public static BitmapImage ToBitmapImage(this byte[] data)
        {
            using (MemoryStream ms = new MemoryStream(data))
            {

                BitmapImage img = new BitmapImage();
                img.CacheOption = BitmapCacheOption.OnLoad;
                img.BeginInit();
                img.StreamSource = ms;
                img.EndInit();

                if (img.CanFreeze)
                {
                    img.Freeze();
                }


                return img;
            }
        }


BitmapImage bd = myimage.ToBitmapImage();
imgCtrl.Source = bd;//imgCtrl is WPF's Image control


The problem is,that the image is not displayed. I've tried different CacheOptions, but it didn't help. Maybe it's just some little thing, but I've no idea what is that. Thank you in advance :).

Solution
In my case, only a little tweak was needed and the code worked. CacheOption should be set after BeginInit(), not before calling it.
public static BitmapImage ToBitmapImage(this byte[] data)
        {
            using (MemoryStream ms = new MemoryStream(data))
            {

                BitmapImage img = new BitmapImage();
                img.BeginInit();
                img.CacheOption = BitmapCacheOption.OnLoad;//CacheOption must be set after BeginInit()
                img.StreamSource = ms;
                img.EndInit();

                if (img.CanFreeze)
                {
                    img.Freeze();
                }


                return img;
            }
        }
Posted
Updated 25-Aug-15 10:50am
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

I've used a bit different approach. First saving the bitmap into the memory stream and then converting to image.

Have a look at the last code block in How To Extract Images From An Office Document[^]
   
Comments
macika123 25-Aug-15 16:50pm
   
Thank you for your response. It's weird, that for you worked reading from MemoryStream, even without explicit definition of CacheOption property.
Meanwhile, I found out that all what I needed was moving 'img.CacheOption = BitmapCacheOption.OnLoad;' line after BeginInit() and the code worked.
For me it seems, that we're doing the same thing, reading from the beginning of some MemoryStream, but somehow it's different. Nevertheless, I have working code :). Also, thank you for your nice article.
I've added my solution to the question, but I also accept yours for reassuring. :)
Wendelius 25-Aug-15 23:02pm
   
Glad you got it working :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100