Skip to content

Instantly share code, notes, and snippets.

@morbidcamel101
Last active August 29, 2015 14:09
Show Gist options
  • Save morbidcamel101/b68eb1e31d4c4f44caf5 to your computer and use it in GitHub Desktop.
Save morbidcamel101/b68eb1e31d4c4f44caf5 to your computer and use it in GitHub Desktop.
Drawing utilities. How to convert an image to a byte array
public static class DrawingUtils
{
// Extension method to convert an image into a byte array
public static byte[] ToBytes(this Bitmap image, ImageFormat imageFormat = null)
{
if (imageFormat != null)
{
using (MemoryStream stream = new MemoryStream())
{
image.Save(stream, imageFormat);
stream.Flush();
return Image.GetInstance(stream.ToArray());
}
}
ImageConverter converter = new ImageConverter();
return Image.GetInstance((byte[])converter.ConvertTo(image, typeof(byte[])));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment