ExtensionMethod.NET Home of 818 C#, Visual Basic, F# and Javascript extension methods

ToBytes

Convert image to byte array

Source

public static byte[] ToBytes(this Image image, ImageFormat format)
{
	if(image == null)
		throw new ArgumentNullException("image");
	if(format == null)
		throw new ArgumentNullException("format");

	using(MemoryStream stream = new MemoryStream())
	{
		image.Save(stream, format);
		return stream.ToArray();
	}
}

Example

Image image = ...;
byte[] imageBytes = image.ToBytes(ImageFormat.Png);

Author: Lucas

Submitted on: 7 mrt 2008

Language: C#

Type: System.Image

Views: 3164