Exemplo n.º 1
0
        /// <summary>
        /// Saves image to stream with specified quality
        /// </summary>
        public static void SaveToStream(Image imageToSave, Stream stream, OutputFormat extension, int quality)
        {
            ImageFormat imfo         = null;
            bool        disposeImage = false;

            switch (extension)
            {
            case OutputFormat.bmp:
                imfo = ImageFormat.Bmp;
                break;

            case OutputFormat.gif:
                imfo = ImageFormat.Gif;
                break;

            case OutputFormat.jpg:
                imfo = ImageFormat.Jpeg;
                break;

            case OutputFormat.png:
                imfo = ImageFormat.Png;
                break;

            case OutputFormat.tiff:
                imfo = ImageFormat.Tiff;
                break;

            default:
                imfo = ImageFormat.Png;
                break;
            }
            // If Quantizing is enable, overwrite the image to save with a 256 - color version
            if (conf.OutputFileReduceColors)
            {
                try {
                    LOG.Debug("Reducing colors on bitmap.");
                    Quantizer quantizer = new OctreeQuantizer(255, 8);
                    imageToSave = quantizer.Quantize(imageToSave);
                    // Make sure the "new" image is disposed
                    disposeImage = true;
                } catch (Exception e) {
                    LOG.Warn("Error occurred while Quantizing the image, ignoring and using original. Error: ", e);
                }
            }

            try {
                // Create meta-data
                PropertyItem softwareUsedPropertyItem = CreatePropertyItem(PROPERTY_TAG_SOFTWARE_USED, "Greenshot");
                if (softwareUsedPropertyItem != null)
                {
                    try {
                        imageToSave.SetPropertyItem(softwareUsedPropertyItem);
                    } catch (ArgumentException) {
                        LOG.WarnFormat("Image of type {0} do not support property {1}", imfo, softwareUsedPropertyItem.Id);
                    }
                }
                LOG.DebugFormat("Saving image to stream with PixelFormat {0}", imageToSave.PixelFormat);
                if (imfo == ImageFormat.Jpeg)
                {
                    EncoderParameters parameters = new EncoderParameters(1);
                    parameters.Param[0] = new System.Drawing.Imaging.EncoderParameter(Encoder.Quality, quality);
                    ImageCodecInfo[] ies = ImageCodecInfo.GetImageEncoders();
                    imageToSave.Save(stream, ies[1], parameters);
                }
                else
                {
                    imageToSave.Save(stream, imfo);
                }
            } finally {
                // cleanup if needed
                if (disposeImage && imageToSave != null)
                {
                    imageToSave.Dispose();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Saves image to stream with specified quality
        /// </summary>
        public static void SaveToStream(Image imageToSave, Stream stream, OutputFormat extension, int quality)
        {
            ImageFormat imfo = null;
            bool disposeImage = false;

            switch (extension) {
                case OutputFormat.bmp:
                    imfo = ImageFormat.Bmp;
                    break;
                case OutputFormat.gif:
                    imfo = ImageFormat.Gif;
                    break;
                case OutputFormat.jpg:
                    imfo = ImageFormat.Jpeg;
                    break;
                case OutputFormat.png:
                    imfo = ImageFormat.Png;
                    break;
                case OutputFormat.tiff:
                    imfo = ImageFormat.Tiff;
                    break;
                default:
                    imfo = ImageFormat.Png;
                    break;
            }
            // If Quantizing is enable, overwrite the image to save with a 256 - color version
            if (conf.OutputFileReduceColors) {
                try {
                    LOG.Debug("Reducing colors on bitmap.");
                    Quantizer quantizer = new OctreeQuantizer(255,8);
                    imageToSave = quantizer.Quantize(imageToSave);
                    // Make sure the "new" image is disposed
                    disposeImage = true;
                } catch(Exception e) {
                    LOG.Warn("Error occurred while Quantizing the image, ignoring and using original. Error: ", e);
                }
            }

            try {
                // Create meta-data
                PropertyItem softwareUsedPropertyItem = CreatePropertyItem(PROPERTY_TAG_SOFTWARE_USED, "Greenshot");
                if (softwareUsedPropertyItem != null) {
                    try {
                        imageToSave.SetPropertyItem(softwareUsedPropertyItem);
                    } catch (ArgumentException) {
                        LOG.WarnFormat("Image of type {0} do not support property {1}", imfo, softwareUsedPropertyItem.Id);
                    }
                }
                LOG.DebugFormat("Saving image to stream with PixelFormat {0}", imageToSave.PixelFormat);
                if (imfo == ImageFormat.Jpeg) {
                    EncoderParameters parameters = new EncoderParameters(1);
                    parameters.Param[0] = new System.Drawing.Imaging.EncoderParameter(Encoder.Quality, quality);
                    ImageCodecInfo[] ies = ImageCodecInfo.GetImageEncoders();
                    imageToSave.Save(stream, ies[1], parameters);
                } else {
                    imageToSave.Save(stream, imfo);
                }
            } finally {
                // cleanup if needed
                if (disposeImage && imageToSave != null) {
                    imageToSave.Dispose();
                }
            }
        }