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

Tagged as

Go to top

Image (colour) manipulation with ColorMatrix

, 4 May 2010
Rate this:
Please Sign up or sign in to vote.
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...
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)

Share

About the Author

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

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 4 May 2010
Article Copyright 2010 by Henry Minute
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid