Click here to Skip to main content
Click here to Skip to main content

Image (colour) manipulation with ColorMatrix

By , 4 May 2010
 
Below is a code snippet derived from a few of the ideas in the article at Matrix Operations for Image Manipulation[^].
 
Whilst this code is C# it should be easily convertible to VB or Managed C++.
 
These three simple Methods make use of the ColorMatrix to achieve variations in the colour saturation.
 
This is the Method that does all the work.
		private void ApplySaturation(float saturation)
		{
			float rWeight = 0.3086f;
			float gWeight = 0.6094f;
			float bWeight = 0.0820f;
 
			float a = (1.0f - saturation) * rWeight + saturation;
			float b = (1.0f - saturation) * rWeight;
			float c = (1.0f - saturation) * rWeight;
			float d = (1.0f - saturation) * gWeight;
			float e = (1.0f - saturation) * gWeight + saturation;
			float f = (1.0f - saturation) * gWeight;
			float g = (1.0f - saturation) * bWeight;
			float h = (1.0f - saturation) * bWeight;
			float i = (1.0f - saturation) * bWeight + saturation;
 
			// Create a Graphics
			using (Graphics gr = this.CreateGraphics())
			{
				gr.Clear(this.BackColor);
				// Create a Bitmap
				using (Bitmap curBitmap = new Bitmap("roses.jpg"))
				{
					// ColorMatrix elements
					float[][] ptsArray = {
											 new float[] {a,  b,  c,  0, 0},
											 new float[] {d,  e,  f,  0, 0},
											 new float[] {g,  h,  i,  0, 0},
											 new float[] {0,  0,  0,  1, 0},
											 new float[] {0, 0, 0, 0, 1}
										 };
					// Create ColorMatrix
					ColorMatrix clrMatrix = new ColorMatrix(ptsArray);
					// Create ImageAttributes
					ImageAttributes imgAttribs = new ImageAttributes();
					// Set color matrix
					imgAttribs.SetColorMatrix(clrMatrix,
						ColorMatrixFlag.Default,
						ColorAdjustType.Default);
					// Draw Image with no effects
					gr.DrawImage(curBitmap, 0, 0, 200, 200);
					// Draw Image with image attributes
					gr.DrawImage(curBitmap,
						new Rectangle(205, 0, 200, 200),
						0, 0, curBitmap.Width, curBitmap.Height,
						GraphicsUnit.Pixel, imgAttribs);
				}
			}
		}
 
This will convert an image to GreyScale. Use a saturation of zero.
    public void GreyScale()
    {
        ApplySaturation(0.0f);
    }
 
This will alter the saturation of an image. Use a saturation of 0.5f.
    public void Fade()
    {
        ApplySaturation(0.5f);
    }
 
This will give the complementary colours for an image. Use a saturation of -1.0f.
    public void Complement()
    {
        ApplySaturation(-1.0f);
    }
 
Obviously you should substitute one of your own images for "roses.jpg", the more colourful the better.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Henry Minute
Retired
United Kingdom United Kingdom
Member
Retired Systems Admin, Programmer, Dogsbody.
Mainly on Systems for Local Government, Health Authorities,
Insurance Industry - (COBOL eeeeeeeugh).
Inventor of Synchronized Shopping.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 4 May 2010
Article Copyright 2010 by Henry Minute
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid