mirror of
https://github.com/greenshot/greenshot
synced 2025-08-19 13:10:00 -07:00
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
This commit is contained in:
parent
7573373a5d
commit
a2fb7bf322
2 changed files with 63 additions and 0 deletions
|
@ -111,6 +111,33 @@ namespace Greenshot.Core {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// AdjustEffect
|
||||||
|
/// </summary>
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// InvertEffect
|
/// InvertEffect
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -1029,6 +1029,42 @@ namespace GreenshotPlugin.Core {
|
||||||
return newImage;
|
return newImage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adjust the brightness, contract or gamma of an image.
|
||||||
|
/// Use the value "1.0f" for no changes.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sourceImage">Original bitmap</param>
|
||||||
|
/// <returns>Bitmap with grayscale</returns>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a new bitmap where the sourceBitmap is in grayscale
|
/// Create a new bitmap where the sourceBitmap is in grayscale
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue