I've got a method that i use to convert a byte array into a System.Drawing.Image. The code looks like this:
The byte array is loaded from an SQL database. The byte array that was populated into the database was created by the following method:
When i run the first method and try to recreate the image i get the following exception:
The image that is being loaded is a JPEG.
I'm really not sure where to go next with this problem so any suggestions would be as welcome as actual solutions.
Thanks
Dafydd
Code:
private Image ConvertByteArrayToImage(byte[] bytes)
{
Image image = new Image();
using (MemoryStream ms = new MemoryStream(bytes, true))
{
ms.Write(bytes, 0, bytes.Length);
image = Image.FromStream(ms);
}
return image;
}
The byte array is loaded from an SQL database. The byte array that was populated into the database was created by the following method:
Code:
private byte[] ConvertImageToByteArray(Image image)
{
//Extract the inner image.
byte[] bytes = null;
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, image.RawFormat);
bytes = new byte[ms.Length];
ms.Read(bytes, 0, (int)ms.Length);
}
return bytes;
}
When i run the first method and try to recreate the image i get the following exception:
Code:
System.ArgumentException: Invalid parameter used.
at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement)
at System.Drawing.Image.FromStream(Stream stream)
at VehicleImageDALC.ConvertByteArrayToImage(Byte[] bytes) in c:\sourcesafe databases\vehicleimagedalc.cs:line 244
The image that is being loaded is a JPEG.
I'm really not sure where to go next with this problem so any suggestions would be as welcome as actual solutions.
Thanks
Dafydd