I am trying to convert an image to bytes to save it in a database and retrieve from database.
How can I do that?
I am trying to convert an image to bytes to save it in a database and retrieve from database.
How can I do that?
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();
}