ImageAttributes.SetColorMatrix 方法

定義

設定色彩調整矩陣。Sets the color-adjustment matrix.

多載

SetColorMatrix(ColorMatrix)

設定預設分類的彩色調整矩陣。Sets the color-adjustment matrix for the default category.

SetColorMatrix(ColorMatrix, ColorMatrixFlag)

設定預設分類的彩色調整矩陣。Sets the color-adjustment matrix for the default category.

SetColorMatrix(ColorMatrix, ColorMatrixFlag, ColorAdjustType)

設定指定分類的彩色調整矩陣。Sets the color-adjustment matrix for a specified category.

SetColorMatrix(ColorMatrix)

設定預設分類的彩色調整矩陣。Sets the color-adjustment matrix for the default category.

public:
 void SetColorMatrix(System::Drawing::Imaging::ColorMatrix ^ newColorMatrix);
public void SetColorMatrix (System.Drawing.Imaging.ColorMatrix newColorMatrix);
member this.SetColorMatrix : System.Drawing.Imaging.ColorMatrix -> unit
Public Sub SetColorMatrix (newColorMatrix As ColorMatrix)

參數

newColorMatrix
ColorMatrix

彩色調整矩陣。The color-adjustment matrix.

範例

下列程式碼範例是針對與 Windows Forms 搭配使用所設計, 而且PaintEventArgs它需要e, 這Paint是事件處理常式的參數。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. 建立矩形影像, 並將所有色彩值設為 128, 產生以純色中等灰色色彩填滿的矩形。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. 建立, 並將其Matrix位置設定為 1.75, 這會強調影像的紅色元件。 ColorMatrixCreates a ColorMatrix and sets its Matrix location to 1.75, which emphasizes the red component of the image.

  3. 建立物件, 並SetColorMatrix呼叫方法。 ImageAttributesCreates an ImageAttributes object and calls the SetColorMatrix method.

  4. 使用只ColorMatrix ImageAttributes在物件中設定的, 將影像 (第二個矩形) 繪製到螢幕上。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 = 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物件會維護五個調整類別的色彩和灰階設定: 預設、點陣圖、筆刷、畫筆和文字。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.

呼叫方法相當於ImageAttributes.SetColorMatrix(ColorMatrix, ColorMatrixFlag) ColorMatrixFlag.Default 呼叫flags方法, 並傳遞給參數。 ImageAttributes.SetColorMatrix(ColorMatrix)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指定彩色調整矩陣會調整所有色彩 (包括灰色)ColorMatrixFlag.Default specifies that all colors (including grays) are adjusted by the color-adjustment matrix

另請參閱

SetColorMatrix(ColorMatrix, ColorMatrixFlag)

設定預設分類的彩色調整矩陣。Sets the color-adjustment matrix for the default category.

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 -> unit
Public Sub SetColorMatrix (newColorMatrix As ColorMatrix, flags As ColorMatrixFlag)

參數

newColorMatrix
ColorMatrix

彩色調整矩陣。The color-adjustment matrix.

flags
ColorMatrixFlag

ColorMatrixFlag 的元素,指定將會受到彩色調整矩陣影響之影像和色彩的類型。An element of ColorMatrixFlag that specifies the type of image and color that will be affected by the color-adjustment matrix.

範例

如需程式碼範例, 請SetColorMatrix(ColorMatrix)參閱方法。For a code example, see the SetColorMatrix(ColorMatrix) method.

備註

ImageAttributes物件會維護五個調整類別的色彩和灰階設定: 預設、點陣圖、筆刷、畫筆和文字。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.

另請參閱

SetColorMatrix(ColorMatrix, ColorMatrixFlag, ColorAdjustType)

設定指定分類的彩色調整矩陣。Sets the color-adjustment matrix for a specified category.

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 -> unit
Public Sub SetColorMatrix (newColorMatrix As ColorMatrix, mode As ColorMatrixFlag, type As ColorAdjustType)

參數

newColorMatrix
ColorMatrix

彩色調整矩陣。The color-adjustment matrix.

mode
ColorMatrixFlag

ColorMatrixFlag 的元素,指定將會受到彩色調整矩陣影響之影像和色彩的類型。An element of ColorMatrixFlag that specifies the type of image and color that will be affected by the color-adjustment matrix.

type
ColorAdjustType

ColorAdjustType 的元素,指定已經設定彩色調整矩陣的分類。An element of ColorAdjustType that specifies the category for which the color-adjustment matrix is set.

範例

下列程式碼範例會示範如何使用 SetColorMatrix 方法。The following code example demonstrates how to use the SetColorMatrix method. 若要執行此範例, 請將程式碼貼入 Windows Form, RotateColors並從表單Paint的事件處理方法呼叫, e傳遞PaintEventArgs為。To run this example, paste the code into a Windows Form and call RotateColors from the form's Paint event-handling method, passing e as 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物件會維護五個調整類別的色彩和灰階設定: 預設、點陣圖、筆刷、畫筆和文字。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.

當您指定特定分類的彩色調整或灰階調整設定時, 預設調整設定就不再適用于該類別。As soon as you specify a color-adjustment or grayscale-adjustment setting for a certain category, the default adjustment settings no longer apply to that category. 例如, 假設您為預設分類指定調整設定的集合。For example, suppose you specify a collection of adjustment settings for the default category. 如果您藉由傳遞Pen SetColorMatrix至方法來設定 [畫筆] 分類的彩色調整矩陣, 則不會將任何預設的調整設定套用到畫筆。If you set the color-adjustment matrix for the pen category by passing Pen to the SetColorMatrix method, none of the default adjustment settings will apply to pens.

適用於