I want to insert image in database and i have declared the binary(5000) datatype for it in the database.Please help me that how to convert image into bytes and how to store it in db ?
Asked
Active
Viewed 588 times
-2
-
possible duplicate of [How to convert image in byte array](http://stackoverflow.com/questions/3801275/how-to-convert-image-in-byte-array) – Alex Art. Mar 19 '15 at 06:05
1 Answers
1
you can convert your image to byte array like this
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
and convert to image from byte[] like this
public Image Convert_To_Image(object _ImageValue)
{
if (_ImageValue == null)
{
return null;
}
byte[] imgArray = (byte[])_ImageValue;
MemoryStream mem = new MemoryStream(imgArray);
return Image.FromStream(mem);
}
Assuming you have a picturebox and you want to show image in picture box tool
you can do something like this
yourpicturebox.Image = Convert_To_Image(_ImageValue);

Khurram Ali
- 1,659
- 4
- 20
- 37
-
i have inserted binary data into db and now i want to show image on form. how can i convert that binary data into image using linq to entities ? – Ahmad Raza Mar 19 '15 at 06:36
-
@AhmadRaza i have also add method `Convert_To_Image` in the answer which will convert binary to image ..this method will return you image then you can show your image into your form – Khurram Ali Mar 19 '15 at 07:49
-
bro this is my code i am call it in a handler...where you think i am wrong ? public static Stream ShowProductImage(int id) { using (CRMEntities ctx = new CRMEntities()) { var r = (from a in ctx.Products where a.id == id select a).First(); return new MemoryStream(r.image.ToArray()); } } – Ahmad Raza Mar 19 '15 at 10:02
-
Please tell me that where i should write connection string for http generic handler and in which form ? – Ahmad Raza Mar 19 '15 at 10:55
-