ImageAttributes.SetColorMatrix 方法

定義

設定彩色調整矩陣。

多載

SetColorMatrix(ColorMatrix)

設定預設分類的彩色調整矩陣。

SetColorMatrix(ColorMatrix, ColorMatrixFlag)

設定預設分類的彩色調整矩陣。

SetColorMatrix(ColorMatrix, ColorMatrixFlag, ColorAdjustType)

設定指定分類的彩色調整矩陣。

SetColorMatrix(ColorMatrix)

來源:
ImageAttributes.cs
來源:
ImageAttributes.cs
來源:
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 -> unit
Public Sub SetColorMatrix (newColorMatrix As ColorMatrix)

參數

newColorMatrix
ColorMatrix

彩色調整矩陣。

範例

下列程式碼範例是設計來搭配Windows Forms使用,而且需要 PaintEventArgse ,這是事件處理常式的參數 Paint 。 此程式碼會執行下列動作:

  1. 建立一個矩形影像,其中所有色彩值都設定為 128,產生填滿純色中灰色的矩形。 程式碼接著會將這個矩形影像繪製到畫面。

  2. 建立 , ColorMatrix 並將其 Matrix 位置設定為 1.75,強調影像的紅色元件。

  3. ImageAttributes建立 物件並呼叫 SetColorMatrix 方法。

  4. 使用 物件中 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) 方法並傳遞 ColorMatrixFlag.Default 參數 flagsColorMatrixFlag.Default 會指定色彩調整矩陣會調整所有色彩 (包括灰色)

另請參閱

適用於

SetColorMatrix(ColorMatrix, ColorMatrixFlag)

來源:
ImageAttributes.cs
來源:
ImageAttributes.cs
來源:
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 -> unit
Public Sub SetColorMatrix (newColorMatrix As ColorMatrix, flags As ColorMatrixFlag)

參數

newColorMatrix
ColorMatrix

彩色調整矩陣。

flags
ColorMatrixFlag

ColorMatrixFlag 的元素,指定將會受到彩色調整矩陣影響之影像和色彩的類型。

範例

如需程式碼範例,請參閱 SetColorMatrix(ColorMatrix) 方法。

備註

物件會維護五個 ImageAttributes 調整類別的色彩和灰階設定:預設、點陣圖、筆刷、畫筆和文字。 例如,您可以指定預設類別的色彩調整矩陣、點陣圖類別的不同色彩調整矩陣,以及畫筆類別的色彩調整矩陣。

預設色彩調整和灰階調整設定會套用至沒有自己調整設定的所有類別。 例如,如果您從未為畫筆類別指定任何調整設定,則預設設定會套用至畫筆類別。

另請參閱

適用於

SetColorMatrix(ColorMatrix, ColorMatrixFlag, ColorAdjustType)

來源:
ImageAttributes.cs
來源:
ImageAttributes.cs
來源:
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 -> unit
Public Sub SetColorMatrix (newColorMatrix As ColorMatrix, mode As ColorMatrixFlag, type As ColorAdjustType)

參數

newColorMatrix
ColorMatrix

彩色調整矩陣。

mode
ColorMatrixFlag

ColorMatrixFlag 的元素,指定將會受到彩色調整矩陣影響之影像和色彩的類型。

type
ColorAdjustType

ColorAdjustType 的元素,指定已經設定彩色調整矩陣的分類。

範例

下列程式碼範例會示範如何使用 SetColorMatrix 方法。 若要執行此範例,請將程式碼貼到 Windows Form 中,然後從表單的事件 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 調整類別的色彩和灰階設定:預設、點陣圖、筆刷、畫筆和文字。 例如,您可以指定預設類別的色彩調整矩陣、點陣圖類別的不同色彩調整矩陣,以及畫筆類別的色彩調整矩陣。

預設色彩調整和灰階調整設定會套用至沒有自己調整設定的所有類別。 例如,如果您從未為畫筆類別指定任何調整設定,則預設設定會套用至畫筆類別。

一旦您為特定類別指定色彩調整或灰階調整設定,預設調整設定就不再套用至該類別。 例如,假設您指定預設類別的調整設定集合。 如果您藉由傳遞 PenSetColorMatrix 方法來設定畫筆類別的色彩調整矩陣,則不會將任何預設調整設定套用至畫筆。

適用於