ImageAttributes.SetColorMatrix 方法    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
设置颜色调整矩阵。
重载
| SetColorMatrix(ColorMatrix) | 设置默认类别的颜色调整矩阵。 | 
| SetColorMatrix(ColorMatrix, ColorMatrixFlag) | 设置默认类别的颜色调整矩阵。 | 
| SetColorMatrix(ColorMatrix, ColorMatrixFlag, ColorAdjustType) | 设置指定类别的颜色调整矩阵。 | 
SetColorMatrix(ColorMatrix)
- Source:
- ImageAttributes.cs
- Source:
- ImageAttributes.cs
- Source:
- ImageAttributes.cs
- Source:
- ImageAttributes.cs
- Source:
- ImageAttributes.cs
设置默认类别的颜色调整矩阵。
public:
 void SetColorMatrix(System::Drawing::Imaging::ColorMatrix ^ newColorMatrix);public void SetColorMatrix (System.Drawing.Imaging.ColorMatrix newColorMatrix);member this.SetColorMatrix : System.Drawing.Imaging.ColorMatrix -> unitPublic Sub SetColorMatrix (newColorMatrix As ColorMatrix)参数
- newColorMatrix
- ColorMatrix
颜色调整矩阵。
示例
下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:
- 创建一个矩形图像,该图像的所有颜色值都设置为 128,生成填充有纯色中灰色的矩形。 然后,该代码将此矩形图像绘制到屏幕。 
- 创建 ColorMatrix 并将其 Matrix 位置设置为 1.75,该位置强调图像的红色组件。 
- 创建 ImageAttributes 对象并调用 SetColorMatrix 方法。 
- 使用刚刚在 ImageAttributes 对象中设置的 ColorMatrix 将图像(第二个矩形)绘制到屏幕。 
请注意,第二个矩形强调红色。
private:
   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 );
   }
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);      
}
Public Sub SetColorMatrixExample(ByVal 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))
    myBitmap.Save("Rectangle1.jpg")
    ' 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 = 1.0F
    ' Green
    myColorMatrix.Matrix22 = 1.0F
    ' Blue
    myColorMatrix.Matrix33 = 1.0F
    ' alpha
    myColorMatrix.Matrix44 = 1.0F
    ' w
    ' Create an ImageAttributes object and set the color matrix.
    Dim imageAttr As New ImageAttributes
    imageAttr.SetColorMatrix(myColorMatrix)
    ' 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
注解
ImageAttributes 对象维护五个调整类别的颜色和灰度设置:默认、位图、画笔、笔和文本。 例如,可以为默认类别指定颜色调整矩阵、位图类别的不同颜色调整矩阵,以及笔类别的颜色调整矩阵。
默认颜色调整和灰度调整设置适用于没有自己调整设置的所有类别。 例如,如果从未为笔类别指定任何调整设置,则默认设置将应用于笔类别。
调用 ImageAttributes.SetColorMatrix(ColorMatrix) 方法等效于调用 ImageAttributes.SetColorMatrix(ColorMatrix, ColorMatrixFlag) 方法,并为 flags 参数传递 ColorMatrixFlag.Default。 
              ColorMatrixFlag.Default 指定颜色调整矩阵调整所有颜色(包括灰色)
另请参阅
适用于
SetColorMatrix(ColorMatrix, ColorMatrixFlag)
- Source:
- ImageAttributes.cs
- Source:
- ImageAttributes.cs
- Source:
- ImageAttributes.cs
- Source:
- ImageAttributes.cs
- Source:
- ImageAttributes.cs
设置默认类别的颜色调整矩阵。
public:
 void SetColorMatrix(System::Drawing::Imaging::ColorMatrix ^ newColorMatrix, System::Drawing::Imaging::ColorMatrixFlag flags);public void SetColorMatrix (System.Drawing.Imaging.ColorMatrix newColorMatrix, System.Drawing.Imaging.ColorMatrixFlag flags);member this.SetColorMatrix : System.Drawing.Imaging.ColorMatrix * System.Drawing.Imaging.ColorMatrixFlag -> unitPublic Sub SetColorMatrix (newColorMatrix As ColorMatrix, flags As ColorMatrixFlag)参数
- newColorMatrix
- ColorMatrix
颜色调整矩阵。
- flags
- ColorMatrixFlag
一个 ColorMatrixFlag 元素,指定将受颜色调整矩阵影响的图像和颜色的类型。
示例
有关代码示例,请参阅 SetColorMatrix(ColorMatrix) 方法。
注解
ImageAttributes 对象维护五个调整类别的颜色和灰度设置:默认、位图、画笔、笔和文本。 例如,可以为默认类别指定颜色调整矩阵、位图类别的不同颜色调整矩阵,以及笔类别的颜色调整矩阵。
默认颜色调整和灰度调整设置适用于没有自己调整设置的所有类别。 例如,如果从未为笔类别指定任何调整设置,则默认设置将应用于笔类别。
另请参阅
适用于
SetColorMatrix(ColorMatrix, ColorMatrixFlag, ColorAdjustType)
- Source:
- ImageAttributes.cs
- Source:
- ImageAttributes.cs
- Source:
- ImageAttributes.cs
- Source:
- ImageAttributes.cs
- Source:
- ImageAttributes.cs
设置指定类别的颜色调整矩阵。
public:
 void SetColorMatrix(System::Drawing::Imaging::ColorMatrix ^ newColorMatrix, System::Drawing::Imaging::ColorMatrixFlag mode, System::Drawing::Imaging::ColorAdjustType type);public void SetColorMatrix (System.Drawing.Imaging.ColorMatrix newColorMatrix, System.Drawing.Imaging.ColorMatrixFlag mode, System.Drawing.Imaging.ColorAdjustType type);member this.SetColorMatrix : System.Drawing.Imaging.ColorMatrix * System.Drawing.Imaging.ColorMatrixFlag * System.Drawing.Imaging.ColorAdjustType -> unitPublic Sub SetColorMatrix (newColorMatrix As ColorMatrix, mode As ColorMatrixFlag, type As ColorAdjustType)参数
- newColorMatrix
- ColorMatrix
颜色调整矩阵。
- mode
- ColorMatrixFlag
一个 ColorMatrixFlag 元素,指定将受颜色调整矩阵影响的图像和颜色的类型。
- type
- ColorAdjustType
一个 ColorAdjustType 元素,指定为其设置颜色调整矩阵的类别。
示例
下面的代码示例演示如何使用 SetColorMatrix 方法。 若要运行此示例,请将代码粘贴到 Windows 窗体中,并从窗体的 Paint 事件处理方法调用 RotateColors,并将 e 作为 PaintEventArgs传递。
private void RotateColors(PaintEventArgs e)
{
    Bitmap image = new Bitmap("RotationInput.bmp");
    ImageAttributes imageAttributes = new ImageAttributes();
    int width = image.Width;
    int height = image.Height;
    float degrees = 60f;
    double r = degrees * System.Math.PI / 180; // degrees to radians
    float[][] colorMatrixElements = { 
        new float[] {(float)System.Math.Cos(r),  (float)System.Math.Sin(r),  0,  0, 0},
        new float[] {(float)-System.Math.Sin(r),  (float)-System.Math.Cos(r),  0,  0, 0},
        new float[] {0,  0,  2,  0, 0},
        new float[] {0,  0,  0,  1, 0},
        new float[] {0, 0, 0, 0, 1}};
    ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
    imageAttributes.SetColorMatrix(
       colorMatrix,
       ColorMatrixFlag.Default,
       ColorAdjustType.Bitmap);
    e.Graphics.DrawImage(image, 10, 10, width, height);
    e.Graphics.DrawImage(
       image,
       new Rectangle(150, 10, width, height),  // destination rectangle 
        0, 0,        // upper-left corner of source rectangle 
        width,       // width of source rectangle
        height,      // height of source rectangle
        GraphicsUnit.Pixel,
       imageAttributes);
}
Private Sub RotateColors(ByVal e As PaintEventArgs)
    Dim image As Bitmap = New Bitmap("RotationInput.bmp")
    Dim imageAttributes As New ImageAttributes()
    Dim width As Integer = image.Width
    Dim height As Integer = image.Height
    Dim degrees As Single = 60.0F
    Dim r As Double = degrees * System.Math.PI / 180 ' degrees to radians
    Dim colorMatrixElements As Single()() = { _
       New Single() {CSng(System.Math.Cos(r)), _
                     CSng(System.Math.Sin(r)), 0, 0, 0}, _
       New Single() {CSng(-System.Math.Sin(r)), _
                     CSng(-System.Math.Cos(r)), 0, 0, 0}, _
       New Single() {0, 0, 2, 0, 0}, _
       New Single() {0, 0, 0, 1, 0}, _
       New Single() {0, 0, 0, 0, 1}}
    Dim colorMatrix As New ColorMatrix(colorMatrixElements)
    imageAttributes.SetColorMatrix( _
       colorMatrix, _
       ColorMatrixFlag.Default, _
       ColorAdjustType.Bitmap)
    e.Graphics.DrawImage(image, 10, 10, width, height)
    ' Pass in the destination rectangle (2nd argument), the upper-left corner 
    ' (3rd and 4th arguments), width (5th argument),  and height (6th 
    ' argument) of the source rectangle.
    e.Graphics.DrawImage( _
       image, _
       New Rectangle(150, 10, width, height), _
       0, 0, _
       width, _
       height, _
       GraphicsUnit.Pixel, _
       imageAttributes)
End Sub
注解
ImageAttributes 对象维护五个调整类别的颜色和灰度设置:默认、位图、画笔、笔和文本。 例如,可以为默认类别指定颜色调整矩阵、位图类别的不同颜色调整矩阵,以及笔类别的颜色调整矩阵。
默认颜色调整和灰度调整设置适用于没有自己调整设置的所有类别。 例如,如果从未为笔类别指定任何调整设置,则默认设置将应用于笔类别。
指定特定类别的颜色调整或灰度调整设置后,默认调整设置将不再应用于该类别。 例如,假设为默认类别指定调整设置的集合。 如果将 Pen 传递给 SetColorMatrix 方法来设置笔类别的颜色调整矩阵,则默认调整设置将不适用于笔。