Save and retrieve images using Entity Framework C#

18 Jan

If you have an image field in the table, you have to save the image as an array of bytes:

using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
public byte[] ImageToByteArray(Image imageIn)
 var ms = new MemoryStream();
 imageIn.Save(ms, ImageFormat.Png);
 return ms.ToArray();
public Image ByteArrayToImage(byte[] byteArrayIn)
 var ms = new MemoryStream(byteArrayIn);
 var returnImage = Image.FromStream(ms);
 return returnImage;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: