Last active
August 29, 2015 14:09
-
-
Save morbidcamel101/b68eb1e31d4c4f44caf5 to your computer and use it in GitHub Desktop.
Drawing utilities. How to convert an image to a byte array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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