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