Convert an image to Byte[]

Sathya4260
Posted by Sathya4260 under C# category on | Points: 40 | Views : 3205
Mostly we would be saving the image in the database and using it, so if we need to insert an image type into an DB it should be done in the format of Byte[].

Here is the simple code to do that,

Image image1= Image.FromFile(Path.Combine(new DirectoryInfo(Application.StartupPath), @"image1.GIF"));
Byte[] imageb= new Byte[0];
using (MemoryStream stream1 = new MemoryStream())
{
image1.Save(stream1, ImageFormat.Gif);
stream1.Close();
imageb= stream1.ToArray();
}


Now we can use the imageb type to insert into the DB, Thus the image type is converted into an byte[] format

Comments or Responses

Login to post response