Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Image (colour) manipulation with ColorMatrix

0.00/5 (No votes)
4 May 2010 1  
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 code of zero.

public void GreyScale()
{
    ApplySaturation(0.0f);
}

This will alter the saturation of an image. Use a code 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here