how serialize a system.Drawing.Image into stream in c#?
I have an Image开发者_如何学JAVA object, img, and i need to save it into a memory stream.
Save method has this signature:
Image.Save (Stream, ImageCodecInfo, EncoderParameters)
but i don't know what i shoud provide in parameters 2 and 3.
I workaround problem using BinaryFormmater serialization, but i would like use the most appropriate method.
Thank you for any help!
There is a simpler overload: Image.Save(Stream, ImageFormat)
. The second parameter lets you pick the format you want to save the image on, like BMP, PNG, or JPEG.
image.Save(stream, ImageFormat.Bmp); // if you need to access the pixels, perhaps
image.Save(stream, ImageFormat.Png); // lossless compression (good for drawings)
image.Save(stream, ImageFormat.Jpeg); // lossy but powerful compression (good for photos)
This link has the information you need. On that page, they list this as the easiest way to get the ImageCodecInfo:
public static ImageCodecInfo FindEncoder(ImageFormat format) {
if (format == null)
throw new ArgumentNullException("format");
foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders()) {
if (codec.FormatID.Equals(format.Guid)) {
return codec;
}
}
return null;
}
精彩评论