Click here to Skip to main content
6,820,238 members and growing! (19,888 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Graphics     Intermediate

Multiple Matrices With ColorMatrix in C#

By dcoolidge

Using multiple matrices to manipulate an image.
C#, Windows, .NET1.0, .NET1.1VS.NET2003, Dev
Posted:29 Jul 2004
Views:37,877
Bookmarked:17 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
8 votes for this article.
Popularity: 3.43 Rating: 3.80 out of 5
2 votes, 25.0%
1

2
1 vote, 12.5%
3
2 votes, 25.0%
4
3 votes, 37.5%
5

Introduction

In my current job, I was creating a control that manipulated the brightness and contrast of an image. I didn't want to call Graphics.DrawImage more than once. The solution was to do a matrix multiply on the two matrices (brightness and contrast) before drawing the image. Just for fun, I included a picture of my daughter in the project...

The main part of the code I wanted to share was the matrices and the matrix multiplication.

private void NewMatrices()
{
  // Initialize the matices;

  bm = new float[5][];
  cm = new float[5][];
  for (int i = 0; i < 5; i++)
  {
    bm[i] = new float[5];
    cm[i] = new float[5];
  }
  // Set the values of the brightness matrix

  float brightness = 0.2f;
  bm[0][0] = 1; bm[0][1] = 0; bm[0][2] = 0; bm[0][3] = 0; bm[0][4] = 0;
  bm[1][0] = 0; bm[1][1] = 1; bm[1][2] = 0; bm[1][3] = 0; bm[1][4] = 0;
  bm[2][0] = 0; bm[2][1] = 0; bm[2][2] = 1; bm[2][3] = 0; bm[2][4] = 0;
  bm[3][0] = 0; bm[3][1] = 0; bm[3][2] = 0; bm[3][3] = 1; bm[3][4] = 0;
  bm[4][0] = brightness; bm[4][1] = brightness;
  bm[4][2] = brightness; bm[4][3] = 1; bm[4][4] = 1;
  // Set the values of contrast matrix

  float contrast = 20f;
  float T = 0.5f * (1f - contrast);
  cm[0][0] = contrast; cm[0][1] = 0; cm[0][2] = 0; cm[0][3] = 0; cm[0][4] = 0;
  cm[1][0] = 0; cm[1][1] = contrast; cm[1][2] = 0; cm[1][3] = 0; cm[1][4] = 0;
  cm[2][0] = 0; cm[2][1] = 0; cm[2][2] = contrast; cm[2][3] = 0; cm[2][4] = 0;
  cm[3][0] = 0; cm[3][1] = 0; cm[3][2] = 0; cm[3][3] = 1; cm[3][4] = 0;
  cm[4][0] = T; cm[4][1] = T; cm[4][2] = T; cm[4][3] = 1; cm[4][4] = 1;
}

private float[][] Multiply(float[][] f1, float[][] f2)
{
  float[][] X = new float[5][];
  for (int d = 0; d < 5; d++)
    X[d] = new float[5];
  int size = 5;
  float[] column = new float[5];
  for (int j = 0; j < 5; j++)
  {
    for (int k = 0; k < 5; k++)
    {
      column[k] = f1[k][j];
    }
    for (int i = 0; i < 5; i++)
    {
      float[] row = f2[i];
      float s = 0;
      for (int k = 0; k < size; k++)
      {
        s += row[k] * column[k];
      }
      X[i][j] = s;
    } 
  }
  return X;
}

Another interesting part of the code is the DrawImage function. It takes in a matrix (i.e., float[][]) and draws an image with that matrix.

private void DrawImage(float[][] Matrix)
{
  ColorMatrix m = new ColorMatrix(Matrix);
  ImageAttributes ia = new ImageAttributes();
  ia.SetColorMatrix(m);
  Rectangle rMy = new Rectangle(0,0,i.Width, i.Height);
  Bitmap bm = new Bitmap(i.Width, i.Height);
  Graphics g = Graphics.FromImage((Image)bm);
  g.Clear(Color.Black);
  g.DrawImage(i, rMy, 0, 0, i.Width, i.Height, 
                               GraphicsUnit.Pixel, ia);
  pictureBox1.Image = (Image)bm;
}

So now, we can call DrawImage with one of our matrices we created above, or use Multiply to combine them.

DrawImage(cm);
DrawImage(bm);
DrawImage(Multiply(cm,bm);

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

About the Author

dcoolidge


Member
.Net wannabe
Occupation: Web Developer
Location: United States United States

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Barcode Image Generation Library
    This library was designed to give an easy class for developers to use when they need to generate barcode images from a string of data.
  • ImageStone
    An article on a library for image manipulation.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralMost of this code can be optimised out PinmemberMikeDC17:18 19 Sep '06  
GeneralRe: Most of this code can be optimised out Pinmemberdcoolidge8:01 20 Sep '06  
GeneralDrawImage Performace issue Pinmemberms44cn23:04 5 Sep '06  
GeneralRe: DrawImage Performace issue Pinmemberdcoolidge12:07 7 Sep '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 29 Jul 2004
Editor: Smitha Vijayan
Copyright 2004 by dcoolidge
Everything else Copyright © CodeProject, 1999-2010
Web19 | Advertise on the Code Project