-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
  • 489
  • 6
  • 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
  • 148,824
  • 21
  • 237
  • 306