Exemplo n.º 1
0
        /// <summary>
        ///     Apply the effect to the bitmap
        /// </summary>
        /// <param name="sourceBitmap">Bitmap</param>
        /// <param name="effect">IEffect</param>
        /// <param name="matrix">Matrix</param>
        /// <returns>Bitmap</returns>
        public static Bitmap ApplyEffect(this Bitmap sourceBitmap, IEffect effect, Matrix matrix)
        {
            var effects = new List <IEffect> {
                effect
            };

            return(sourceBitmap.ApplyEffects(effects, matrix));
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Create an image from a surface with the settings from the output settings applied
        /// </summary>
        /// <param name="surface"></param>
        /// <param name="outputSettings"></param>
        /// <param name="bitmapToSave"></param>
        /// <returns>true if the image must be disposed</returns>
        public static bool CreateBitmapFromSurface(ISurface surface, SurfaceOutputSettings outputSettings, out Bitmap bitmapToSave)
        {
            var disposeImage = false;

            if (outputSettings.Format == OutputFormats.greenshot || outputSettings.SaveBackgroundOnly)
            {
                // We save the image of the surface, this should not be disposed
                bitmapToSave = surface.Screenshot;
            }
            else
            {
                // We create the export image of the surface to save
                bitmapToSave = surface.GetBitmapForExport();
                disposeImage = true;
            }

            // The following block of modifications should be skipped when saving the greenshot format, no effects or otherwise!
            if (outputSettings.Format == OutputFormats.greenshot)
            {
                return(disposeImage);
            }
            Bitmap tmpBitmap;

            if (outputSettings.Effects != null && outputSettings.Effects.Count > 0)
            {
                // apply effects, if there are any
                using (var matrix = new Matrix())
                {
                    tmpBitmap = bitmapToSave.ApplyEffects(outputSettings.Effects, matrix);
                }
                if (tmpBitmap != null)
                {
                    if (disposeImage)
                    {
                        bitmapToSave.Dispose();
                    }
                    bitmapToSave = tmpBitmap;
                    disposeImage = true;
                }
            }

            // check for color reduction, forced or automatically, only when the DisableReduceColors is false
            if (outputSettings.DisableReduceColors || !CoreConfig.OutputFileAutoReduceColors && !outputSettings.ReduceColors)
            {
                return(disposeImage);
            }
            var isAlpha = Image.IsAlphaPixelFormat(bitmapToSave.PixelFormat);

            if (outputSettings.ReduceColors || !isAlpha && CoreConfig.OutputFileAutoReduceColors)
            {
                using (var quantizer = new WuQuantizer(bitmapToSave))
                {
                    var colorCount = quantizer.GetColorCount();
                    Log.Info().WriteLine("Image with format {0} has {1} colors", bitmapToSave.PixelFormat, colorCount);
                    if (!outputSettings.ReduceColors && colorCount >= 256)
                    {
                        return(disposeImage);
                    }
                    try
                    {
                        Log.Info().WriteLine("Reducing colors on bitmap to 256.");
                        tmpBitmap = quantizer.GetQuantizedImage(CoreConfig.OutputFileReduceColorsTo);
                        if (disposeImage)
                        {
                            bitmapToSave.Dispose();
                        }
                        bitmapToSave = tmpBitmap;
                        // Make sure the "new" image is disposed
                        disposeImage = true;
                    }
                    catch (Exception e)
                    {
                        Log.Warn().WriteLine(e, "Error occurred while Quantizing the image, ignoring and using original. Error: ");
                    }
                }
            }
            else if (isAlpha && !outputSettings.ReduceColors)
            {
                Log.Info().WriteLine("Skipping 'optional' color reduction as the image has alpha");
            }
            return(disposeImage);
        }