Click to See Complete Forum and Search --> : Convert String to image


idrisgani
March 24th, 2009, 11:02 AM
Hi , i want to save the image from the database to an physical location like C:\one.JPG. the datatype of the column in the table is Image. i tried with working with MemoryStream. but i am not suceed. i am getting "Parameter not valid" when converting the stream to image "Image.fromStream(ms,true)".

DataView dv = new DataView(ds.Tables[0], null, null, DataViewRowState.CurrentRows);

byte[] imageData =(Bytes) dv[0][3];

Image newImage;
//Read image data into a memory stream
using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
{
ms.Write(imageData, 0, imageData.Length);

//Set image variable value using memory stream.
newImage = Image.FromStream(ms, true);
}

nelo
March 24th, 2009, 11:08 AM
Hi,

Are you sure that you have the image data in "imageData"? Have you tried stepping through the application with the debugger?

boudino
March 25th, 2009, 03:05 AM
I would try to call ms.Seek(0) before constructing the image. And are you sure that the raw data is in proper format? I think (I'm not able to prove it now) that it must be a BMP.

Also, keep in mind this statement from MSDN: "You must keep the stream open for the lifetime of the Image."