C# Convert Image to byte array (byte[]) or C# convert byte array (byte[]) to Image
Convert Image to byte[] method:
public byte[] imageToByteArray(Image imageIn)
{
using (var ms = new System.IO.MemoryStream())
{
System.Drawing.Imaging.ImageFormat format;
switch (imageIn.MimeType())
{
case "image/png":
format = ImageFormat.Png;
break;
case "image/gif":
format = ImageFormat.Gif;
break;
default:
format = ImageFormat.Jpeg;
break;
}
imageIn.Save(ms, format);
return ms.ToArray();
}
}
Convert byte[] to Image method:
public Image byteArrayToImage(byte[] byteArrayIn)
{
using (var ms = new System.IO.MemoryStream(byteArrayIn))
{
Image returnImage = Image.FromStream(ms);
return returnImage;
}
}
No comments:
Post a Comment