SetColorMatrix Method (ColorMatrix)
Collapse the table of content
Expand the table of content

ImageAttributes.SetColorMatrix Method (ColorMatrix)

.NET Framework 1.1

Sets the color-adjustment matrix for the default category.

[Visual Basic]
Overloads Public Sub SetColorMatrix( _
   ByVal newColorMatrix As ColorMatrix _
public void SetColorMatrix(
 ColorMatrix newColorMatrix
public: void SetColorMatrix(
 ColorMatrix* newColorMatrix
public function SetColorMatrix(
   newColorMatrix : ColorMatrix


The color-adjustment matrix.

Return Value

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- and grayscale-adjustment settings apply to all categories that don't have adjustment settings of their own. For example, if you never specify any adjustment settings for the pen category, then the default settings apply to the pen category.

The call myImageAttributes.SetColorMatrix(cm) is equivalent to the call myImageAttributes.SetColorMatrix(cm, ColorMatrixFlag.Default). ColorMatrixFlag.Default specifies that all colors (including grays) are adjusted by the color-adjustment matrix.


[Visual Basic, C#] The following 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:

  • 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.
  • Creates a ColorMatrix object and sets its Matrix00 location to 1.75, which emphasizes the red component of the image.
  • Creates an ImageAttributes object and calls the SetColorMatrix method.
  • Draws the image (a second rectangle) to the screen using the ColorMatrix object just set in the ImageAttributes object.

[Visual Basic, C#] Note that the second rectangle has the color red emphasized.

[Visual Basic] 
Public Sub SetColorMatrixExample(e As PaintEventArgs)
' Create a rectangle image with all colors set to 128 (medium
' gray).
Dim myBitmap As New Bitmap(50, 50, PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(myBitmap)
g.FillRectangle(New SolidBrush(Color.FromArgb(255, 128, 128, _
128)), New Rectangle(0, 0, 50, 50))
' Open an Image file and draw it to the screen.
Dim myImage As Image = Image.FromFile("Rectangle1.jpg")
e.Graphics.DrawImage(myImage, 20, 20)
' Initialize the color matrix.
Dim myColorMatrix As New ColorMatrix()
myColorMatrix.Matrix00 = 1.75F ' Red
myColorMatrix.Matrix11 = 1F ' Green
myColorMatrix.Matrix22 = 1F ' Blue
myColorMatrix.Matrix33 = 1F ' alpha
myColorMatrix.Matrix44 = 1F ' w
' Create an ImageAttributes object and set the color matrix.
Dim imageAttr As New ImageAttributes()
' Draw the image using the color matrix.
Dim rect As New Rectangle(100, 20, 200, 200)
e.Graphics.DrawImage(myImage, rect, 0, 0, 200, 200, _
GraphicsUnit.Pixel, imageAttr) ' Image
End Sub 'SetColorMatrixExample
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));
// 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();
myColorMatrix.Matrix00 = 1.75f; // Red
myColorMatrix.Matrix11 = 1.00f; // Green
myColorMatrix.Matrix22 = 1.00f; // Blue
myColorMatrix.Matrix33 = 1.00f; // alpha
myColorMatrix.Matrix44 = 1.00f; // w
// Create an ImageAttributes object and set the color matrix.
ImageAttributes imageAttr = new ImageAttributes();
// Draw the image using the color matrix.
Rectangle rect = new Rectangle(100, 20, 200, 200);
e.Graphics.DrawImage(myImage,         // Image
rect,            // Dest. rect.
0,               // srcX
0,               // srcY
200,             // srcWidth
200,             // srcHeight
GraphicsUnit.Pixel, // srcUnit
imageAttr);      // ImageAttributes

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

ImageAttributes Class | ImageAttributes Members | System.Drawing.Imaging Namespace | ImageAttributes.SetColorMatrix Overload List

© 2016 Microsoft