SetColorMatrix Method (ColorMatrix)

ImageAttributes::SetColorMatrix Method (ColorMatrix^)


Sets the color-adjustment matrix for the default category.

Namespace:   System.Drawing.Imaging
Assembly:  System.Drawing (in System.Drawing.dll)

void SetColorMatrix(
	ColorMatrix^ newColorMatrix


Type: System.Drawing.Imaging::ColorMatrix^

The color-adjustment matrix.

This method does not return a value.

An ImageAttributes object maintains color and grayscale settings for five adjustment categories: default, bitmap, brush, pen, and text. For example, you can specify a color-adjustment matrix for the default category, a different color-adjustment matrix for the bitmap category, and still a different color-adjustment matrix for the pen category.

The default color-adjustment and grayscale-adjustment settings apply to all categories that do not have adjustment settings of their own. For example, if you never specify any adjustment settings for the pen category, the default settings apply to the pen category.

Calling the ImageAttributes::SetColorMatrix(ColorMatrix^) method is equivalent to calling the ImageAttributes::SetColorMatrix(ColorMatrix^, ColorMatrixFlag) method and passing ColorMatrixFlag::Default for the flags parameter. ColorMatrixFlag::Default specifies that all colors (including grays) are adjusted by the color-adjustment matrix

The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. The code performs the following actions:

  1. Creates a rectangle image that has all the color values set to 128, producing a rectangle that is filled with a solid medium-gray color. The code then draws this rectangle image to the screen.

  2. Creates a ColorMatrix and sets its Matrix location to 1.75, which emphasizes the red component of the image.

  3. Creates an ImageAttributes object and calls the SetColorMatrix method.

  4. Draws the image (a second rectangle) to the screen using the ColorMatrix just set in the ImageAttributes object.

Note that the second rectangle has the color red emphasized.

   void SetColorMatrixExample( PaintEventArgs^ e )
      // Create a rectangle image with all colors set to 128 (medium
      // gray).
      Bitmap^ myBitmap = gcnew Bitmap( 50,50,PixelFormat::Format32bppArgb );
      Graphics^ g = Graphics::FromImage( myBitmap );
      g->FillRectangle( gcnew SolidBrush( Color::FromArgb( 255, 128, 128, 128 ) ), Rectangle(0,0,50,50) );
      myBitmap->Save( "Rectangle1.jpg" );

      // Open an Image file and draw it to the screen.
      Image^ myImage = Image::FromFile( "Rectangle1.jpg" );
      e->Graphics->DrawImage( myImage, 20, 20 );

      // Initialize the color matrix.
      ColorMatrix^ myColorMatrix = gcnew ColorMatrix;

      // Red
      myColorMatrix->Matrix00 = 1.75f;

      // Green
      myColorMatrix->Matrix11 = 1.00f;

      // Blue
      myColorMatrix->Matrix22 = 1.00f;

      // alpha
      myColorMatrix->Matrix33 = 1.00f;

      // w
      myColorMatrix->Matrix44 = 1.00f;

      // Create an ImageAttributes object and set the color matrix.
      ImageAttributes^ imageAttr = gcnew ImageAttributes;
      imageAttr->SetColorMatrix( myColorMatrix );

      // Draw the image using the color matrix.
      Rectangle rect = Rectangle(100,20,200,200);
      e->Graphics->DrawImage( myImage, rect, 0, 0, 200, 200, GraphicsUnit::Pixel, imageAttr );

.NET Framework
Available since 1.1
Return to top
© 2015 Microsoft