Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

ImageAttributes.SetColorMatrix Method (ColorMatrix)

Sets the color-adjustment matrix for the default category.

Namespace:  System.Drawing.Imaging
Assembly:  System.Drawing (in System.Drawing.dll)
public void SetColorMatrix(
	ColorMatrix newColorMatrix
)

Parameters

newColorMatrix
Type: System.Drawing.Imaging.ColorMatrix

The color-adjustment matrix.

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 PaintEventArgs e, 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.

private void SetColorMatrixExample(PaintEventArgs e)
{

    // Create a rectangle image with all colors set to 128 (medium 

    // gray).
    Bitmap myBitmap = new Bitmap(50, 50, PixelFormat.Format32bppArgb);
    Graphics g = Graphics.FromImage(myBitmap);
    g.FillRectangle(new SolidBrush(Color.FromArgb(255, 128, 128, 128)),
        new 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 = new 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 = new ImageAttributes();
    imageAttr.SetColorMatrix(myColorMatrix);

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

}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft. All rights reserved.