Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
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;
            }
        }


C#
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.
C#
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

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[^]
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900