0

I have a class like this:

class person
{
    public string Name{get;set;}
    public byte[] PersonImage{get;set;}
}

When I Load my Person From DataBase I want to show PersonImage in a Image control, so I want to create BitmapImage from my byte[]:

var bitmapImage = new BitmapImage();

bitmapImage.BeginInit();
var filestream = new MemoryStream(PersonImage);
bitmapImage.StreamSource = filestream;

bitmapImage.EndInit();// I have Exception in this line 

My Exception is:

No imaging component suitable to complete this operation was found.

--edite my Inner Exception is :

Inner Exception:Exception from HRESULT:0*88982F50

M.Azad
  • 3,673
  • 8
  • 47
  • 77
  • Have you looked at - http://stackoverflow.com/questions/3886849/error-in-my-byte-to-wpf-bitmapimage-conversion - it may help (how the questioner does their byte to image stream conversion) – kaj Mar 15 '12 at 17:24
  • 1
    possible duplicate of [Create new FileStream out of a byte array](http://stackoverflow.com/questions/6076192/create-new-filestream-out-of-a-byte-array) – D'Arcy Rittich Mar 15 '12 at 17:24
  • 1
    You'd better take a look at the code that wrote the data into the dbase as well. This question isn't otherwise answerable. – Hans Passant Mar 15 '12 at 17:28

2 Answers2

1

assuming PersonImage is really a valid byte[] representing an image try

var bitmapImage = new BitmapImage();

bitmapImage.BeginInit();
var somestream = new MemoryStream(PersonImage);
somestream.Position = 0; // "rewind" stream to start...
bitmapImage.StreamSource = somestream;

bitmapImage.EndInit();
Yahia
  • 69,653
  • 9
  • 115
  • 144
  • @mtaboy that means that `PersonImage` very probably does not contain a valid image... you need to check what `PersonImage` contains – Yahia Mar 15 '12 at 17:31
0

Your image is probably not a supported format. Try the following method:

public BitmapImage GetBitmapImage(byte[] imageData)
{
    BitmapImage bitmapImage = new BitmapImage();

    using (MemoryStream imageStream = new MemoryStream(imageData))
    using (Image image = Image.FromStream(imageStream))
    using (MemoryStream convertedImageStream = new MemoryStream())
    {
        bitmapImage.BeginInit();

        image.Save(convertedImageStream, ImageFormat.Png);

        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapImage.StreamSouce = convertedImageStream;

        bitMapImage.EndInit();
    }

    return bitmapImage;
}

Make sure you include the following using statements at the top of your file:

using System.Drawing;
using System.Drawing.Imaging;
JamieSee
  • 12,696
  • 2
  • 31
  • 47