One of our early blogs was about extracting images from databases, websites and showing them on the screen.
The blog(see it here) dealt with
a) Retrieving images from the www
b) Retrieving images from the database when retrieved as a binary.
We realized there are some more scenarios to take care of here
c) Save Image to the database as a byte[]
public Byte[] BufferFromImage(BitmapImage imageSource)
{
MemoryStream memStream = new MemoryStream();
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(imageSource));
encoder.Save(memStream);
return memStream.GetBuffer();
}
d) Retrieve Image from byte[]( When you use Entity Framework with the Image database type you would get it back as a byte[])
public BitmapImage ConvertToImage(byte[] rawImageData)
{
MemoryStream stream = new MemoryStream(rawImageData);
stream.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = stream;
bi.EndInit();
return bi;
}
If you see any more scenarios do add comments and lets complete this list…
Cennest!