Exemplo n.º 1
0
        private static void GetImageStream( ImageFormat outputFormat, Bitmap bitmap, MemoryStream ms )
        {
            var imageEncoders = ImageCodecInfo.GetImageEncoders ();
            var encoderParameters = new EncoderParameters (1);
            encoderParameters.Param[0] = new EncoderParameter (Encoder.Quality, 100L);

            if (ImageFormat.Jpeg.Equals (outputFormat))
            {
                bitmap.Save (ms, imageEncoders[1], encoderParameters);
            }
            else if (ImageFormat.Png.Equals (outputFormat))
            {
                bitmap.Save (ms, imageEncoders[4], encoderParameters);
            }
            else if (ImageFormat.Gif.Equals (outputFormat))
            {
                var quantizer = new OctreeQuantizer (255, 8);
                using (var quantized = quantizer.Quantize (bitmap))
                {
                    quantized.Save (ms, imageEncoders[2], encoderParameters);
                }
            }
            else if (ImageFormat.Bmp.Equals (outputFormat))
            {
                bitmap.Save (ms, imageEncoders[0], encoderParameters);
            }
            else
            {
                bitmap.Save (ms, outputFormat);
            }
        }