-1

I am trying to convert an image to bytes to save it in a database and retrieve from database.

How can I do that?

Nauman Khan
  • 509
  • 7
  • 20

1 Answers1

2

You can create a MemoryStream, save the image to it, and use the ToArray method to get the bytes:

This code assumes you have an Image named image:

byte[] bytes;
using (MemoryStream ms = new MemoryStream())
{
    image.Save(ms, ImageFormat.Png);
    bytes = ms.ToArray();
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325