From a2fb7bf3228df080fd277ba18f4996fc518b7835 Mon Sep 17 00:00:00 2001 From: RKrom Date: Thu, 7 Feb 2013 13:19:01 +0000 Subject: [PATCH] Added "Adjust" to the ImageHelper, this can change the brightness, contrast and gamma. git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2469 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4 --- GreenshotPlugin/Core/Effects.cs | 27 ++++++++++++++++++++++ GreenshotPlugin/Core/ImageHelper.cs | 36 +++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/GreenshotPlugin/Core/Effects.cs b/GreenshotPlugin/Core/Effects.cs index dcbaf9ff0..9f0224b03 100644 --- a/GreenshotPlugin/Core/Effects.cs +++ b/GreenshotPlugin/Core/Effects.cs @@ -111,6 +111,33 @@ namespace Greenshot.Core { } } + /// + /// AdjustEffect + /// + public class AdjustEffect : IEffect { + public AdjustEffect() : base() { + Contrast = 1f; + Brightness = 1f; + Gamma = 1f; + } + public float Contrast { + get; + set; + } + public float Brightness { + get; + set; + } + public float Gamma { + get; + set; + } + public Image Apply(Image sourceImage, out Point offsetChange) { + offsetChange = Point.Empty; + return ImageHelper.Adjust(sourceImage, Brightness, Contrast, Gamma); + } + } + /// /// InvertEffect /// diff --git a/GreenshotPlugin/Core/ImageHelper.cs b/GreenshotPlugin/Core/ImageHelper.cs index 0ac5739c5..033517b8f 100644 --- a/GreenshotPlugin/Core/ImageHelper.cs +++ b/GreenshotPlugin/Core/ImageHelper.cs @@ -1029,6 +1029,42 @@ namespace GreenshotPlugin.Core { return newImage; } + /// + /// Adjust the brightness, contract or gamma of an image. + /// Use the value "1.0f" for no changes. + /// + /// Original bitmap + /// Bitmap with grayscale + public static Image Adjust(Image sourceImage, float brightness, float contrast, float gamma) { + //create a blank bitmap the same size as original + // If using 8bpp than the following exception comes: A Graphics object cannot be created from an image that has an indexed pixel format. + Bitmap newBitmap = CreateEmpty(sourceImage.Width, sourceImage.Height, PixelFormat.Format24bppRgb, Color.Empty, sourceImage.HorizontalResolution, sourceImage.VerticalResolution); + float adjustedBrightness = brightness - 1.0f; + // get a graphics object from the new image + using (Graphics graphics = Graphics.FromImage(newBitmap)) { + // create the grayscale ColorMatrix + ColorMatrix colorMatrix = new ColorMatrix( + new float[][] { + new float[] {contrast, 0, 0, 0, 0}, // scale red + new float[] {0, contrast, 0, 0, 0}, // scale green + new float[] {0, 0, contrast, 0, 0}, // scale blue + new float[] {0, 0, 0, 1.0f, 0}, // don't scale alpha + new float[] {adjustedBrightness, adjustedBrightness, adjustedBrightness, 0, 1} + }); + + //create some image attributes + ImageAttributes attributes = new ImageAttributes(); + + //set the color matrix attribute + attributes.SetColorMatrix(colorMatrix); + + //draw the original image on the new image using the grayscale color matrix + graphics.DrawImage(sourceImage, new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), 0, 0, sourceImage.Width, sourceImage.Height, GraphicsUnit.Pixel, attributes); + } + + return newBitmap; + } + /// /// Create a new bitmap where the sourceBitmap is in grayscale ///