private static void SaveImageAsPNGStream(Image img, Stream stream, PNGBitDepth bitDepth) { if (bitDepth == PNGBitDepth.Automatic) { if (ImageHelpers.IsImageTransparent((Bitmap)img)) { bitDepth = PNGBitDepth.Bit32; } else { bitDepth = PNGBitDepth.Bit24; } } if (bitDepth == PNGBitDepth.Bit32) { if (img.PixelFormat != PixelFormat.Format32bppArgb && img.PixelFormat != PixelFormat.Format32bppRgb) { using (Bitmap bmpNew = ((Bitmap)img).Clone(new Rectangle(0, 0, img.Width, img.Height), PixelFormat.Format32bppArgb)) { bmpNew.Save(stream, ImageFormat.Png); return; } } } else if (bitDepth == PNGBitDepth.Bit24) { if (img.PixelFormat != PixelFormat.Format24bppRgb) { using (Bitmap bmpNew = ((Bitmap)img).Clone(new Rectangle(0, 0, img.Width, img.Height), PixelFormat.Format24bppRgb)) { bmpNew.Save(stream, ImageFormat.Png); return; } } } img.Save(stream, ImageFormat.Png); }
public static MemoryStream SaveImageAsStream(Image img, EImageFormat imageFormat, PNGBitDepth pngBitDepth = PNGBitDepth.Automatic, int jpegQuality = 90, GIFQuality gifQuality = GIFQuality.Default) { MemoryStream stream = new MemoryStream(); switch (imageFormat) { case EImageFormat.PNG: SaveImageAsPNGStream(img, stream, pngBitDepth); if (Option.Settings.PNGStripColorSpaceInformation) { using (stream) { return(PNGStripColorSpaceInformation(stream)); } } break; case EImageFormat.JPEG: SaveImageAsJPEGStream(img, stream, jpegQuality); break; case EImageFormat.GIF: img.SaveGIF(stream, gifQuality); break; case EImageFormat.BMP: img.Save(stream, ImageFormat.Bmp); break; case EImageFormat.TIFF: img.Save(stream, ImageFormat.Tiff); break; } return(stream); }