1: /// <summary>
2: /// Adjusts the brightness
3: /// </summary>
4: /// <param name="Image">Image to change</param>
5: /// <param name="Value">-255 to 255</param>
6: /// <returns>A bitmap object</returns>
7: public static Bitmap AdjustBrightness(Bitmap Image, int Value)
8: {
9: float FinalValue = (float)Value / 255.0f;
10: ColorMatrix TempMatrix = new ColorMatrix();
11: TempMatrix.Matrix = new float[][]{
12: new float[] {1, 0, 0, 0, 0},
13: new float[] {0, 1, 0, 0, 0},
14: new float[] {0, 0, 1, 0, 0},
15: new float[] {0, 0, 0, 1, 0},
16: new float[] {FinalValue, FinalValue, FinalValue, 1, 1}
17: };
18: return TempMatrix.Apply(Image);
19: }
20:
21:
22: /*
23: Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
24:
25: Permission is hereby granted, free of charge, to any person obtaining a copy
26: of this software and associated documentation files (the "Software"), to deal
27: in the Software without restriction, including without limitation the rights
28: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
29: copies of the Software, and to permit persons to whom the Software is
30: furnished to do so, subject to the following conditions:
31:
32: The above copyright notice and this permission notice shall be included in
33: all copies or substantial portions of the Software.
34:
35: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
41: THE SOFTWARE.*/
42:
43: #region Usings
44: using System.Drawing;
45: using System.Drawing.Imaging;
46: #endregion
47:
48: namespace Utilities.Media.Image
49: {
50: /// <summary>
51: /// Helper class for setting up and applying a color matrix
52: /// </summary>
53: public class ColorMatrix
54: {
55: #region Constructor
56:
57: /// <summary>
58: /// Constructor
59: /// </summary>
60: public ColorMatrix()
61: {
62: }
63:
64: #endregion
65:
66: #region Properties
67:
68: /// <summary>
69: /// Matrix containing the values of the ColorMatrix
70: /// </summary>
71: public float[][] Matrix { get; set; }
72:
73: #endregion
74:
75: #region Public Functions
76:
77: /// <summary>
78: /// Applies the color matrix
79: /// </summary>
80: /// <param name="OriginalImage">Image sent in</param>
81: /// <returns>An image with the color matrix applied</returns>
82: public Bitmap Apply(Bitmap OriginalImage)
83: {
84: Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
85: using (Graphics NewGraphics = Graphics.FromImage(NewBitmap))
86: {
87: System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(Matrix);
88: using (ImageAttributes Attributes = new ImageAttributes())
89: {
90: Attributes.SetColorMatrix(NewColorMatrix);
91: NewGraphics.DrawImage(OriginalImage,
92: new System.Drawing.Rectangle(0, 0, OriginalImage.Width, OriginalImage.Height),
93: 0, 0, OriginalImage.Width, OriginalImage.Height,
94: GraphicsUnit.Pixel,
95: Attributes);
96: }
97: }
98: return NewBitmap;
99: }
100:
101: #endregion
102: }
103: }