2

Since yesterday, i am trying to load an image from a byte array. I get the byte array from the Employees table of NorthWind database. I read some articles saying that there is an OLE header of size 78 that should be removed before we convert from byte[] to ImageSource. But it can't get any image. here is my converter:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        byte[] data = value as byte[];           

        if (data != null)
        {
            MemoryStream ms = new MemoryStream();
            int offset = 78;               
            BitmapImage img = new BitmapImage();                
            ms.Write(data, offset, data.Length - offset);
            img.SetSource(ms);
            ms.Close();
            return img;
        }
        return null;

    }

here is my image definition in XAML

<Image Grid.Column="1" Height="147" HorizontalAlignment="Left" Margin="3,3,0,6" Name="photoImage" Source="{Binding Path=Photo, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource PhotoConverter1}}" Stretch="Fill" VerticalAlignment="Center" Width="137" DataContext="{Binding}" />

Could you help me to figure out how to make it work?

Shomron
  • 451
  • 4
  • 13

1 Answers1

0

http://en.wikipedia.org/wiki/BMP_file_format

You should be able to look at the array and see where the bitmap starts using the header information in the bitmap.

This code may also help you: http://blogs.msdn.com/b/pranab/archive/2008/07/15/removing-ole-header-from-images-stored-in-ms-access-db-as-ole-object.aspx

Alikar
  • 347
  • 2
  • 13
  • In windows the header always starts with BM or in ASCII "424D" It may also be "4D42" depending on big or little endian formatting of the data. – Alikar Feb 29 '12 at 19:52
  • thanks you for your answers. I went to your links and used the given code but the image still doesn't load (no image shows up into my image control.) the piece of code in your second link confirms that the offset is 78. – Shomron Feb 29 '12 at 20:32
  • Can you add the bitmap header to your question. Perhaps it is not formatted correctly. – Alikar Feb 29 '12 at 20:41
  • It is a silverlight application. I am using Silverlight 5 – Shomron Feb 29 '12 at 20:41
  • Sorry, I took out that question because I saw the tag in your initial question. – Alikar Feb 29 '12 at 21:11
  • So just to clarify you've checked that the images in the stream are in fact good bmps with correct headers and data? – Alikar Mar 05 '12 at 16:18
  • well, I notice that the byte array is wierd. According to {this}[http://en.wikipedia.org/wiki/BMP_file_format] my byte array should start with 424D (BM). but, when I looked at it in memory, I noticed that BM is at offset 86. – Shomron Mar 05 '12 at 17:55
  • Sounds like there is either another data structure wrapping the bitmap or the offset is not 78. Try the offset with 86 and see what happens. – Alikar Mar 05 '12 at 18:35