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 フォームで使用するように設計されており、イベント ハンドラーのPaintパラメーターである が必要PaintEventArgseです。 コードは、次のアクションを実行します。

  1. すべての色の値が 128 に設定された四角形イメージを作成し、単色の中灰色で塗りつぶされた四角形を生成します。 次に、この四角形の画像を画面に描画します。

  2. ColorMatrix 作成し、その Matrix 位置を 1.75 に設定します。これは、イメージの赤いコンポーネントを強調します。

  3. オブジェクトを ImageAttributes 作成し、 メソッドを SetColorMatrix 呼び出します。

  4. オブジェクトに設定した だけを使用して、画像 (2 番目の四角形) を ColorMatrix 画面に ImageAttributes 描画します。

2 番目の四角形の色が赤で強調されていることに注意してください。

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 、既定、ビットマップ、ブラシ、ペン、テキストの 5 つの調整カテゴリの色とグレースケールの設定を保持します。 たとえば、既定のカテゴリの色調整マトリックス、ビットマップ カテゴリに別の色調整マトリックス、ペン カテゴリに別の色調整マトリックスを指定できます。

既定の色調整とグレースケール調整の設定は、独自の調整設定を持たないすべてのカテゴリに適用されます。 たとえば、ペン カテゴリの調整設定を指定しない場合、既定の設定はペン カテゴリに適用されます。

メソッドのImageAttributes.SetColorMatrix(ColorMatrix)呼び出しは、 メソッドを呼び出して パラメーターをImageAttributes.SetColorMatrix(ColorMatrix, ColorMatrixFlag)渡すことColorMatrixFlag.Defaultflagsと同じです。 ColorMatrixFlag.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 、既定、ビットマップ、ブラシ、ペン、テキストの 5 つの調整カテゴリの色とグレースケールの設定を保持します。 たとえば、既定のカテゴリの色調整マトリックス、ビットマップ カテゴリに別の色調整マトリックス、ペン カテゴリに別の色調整マトリックスを指定できます。

既定の色調整とグレースケール調整の設定は、独自の調整設定を持たないすべてのカテゴリに適用されます。 たとえば、ペン カテゴリの調整設定を指定しない場合、既定の設定はペン カテゴリに適用されます。

こちらもご覧ください

適用対象

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 フォームに貼り付け、フォームのPaintイベント処理メソッドから を呼び出RotateColorsし、 を としてPaintEventArgs渡しますe

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 、既定、ビットマップ、ブラシ、ペン、テキストの 5 つの調整カテゴリの色とグレースケールの設定を保持します。 たとえば、既定のカテゴリの色調整マトリックス、ビットマップ カテゴリに別の色調整マトリックス、ペン カテゴリに別の色調整マトリックスを指定できます。

既定の色調整とグレースケール調整の設定は、独自の調整設定を持たないすべてのカテゴリに適用されます。 たとえば、ペン カテゴリの調整設定を指定しない場合、既定の設定はペン カテゴリに適用されます。

特定のカテゴリに対して色調整またはグレースケール調整設定を指定するとすぐに、既定の調整設定がそのカテゴリに適用されなくなります。 たとえば、既定のカテゴリの調整設定のコレクションを指定するとします。 メソッドに渡 Pen してペン カテゴリの色調整マトリックスを SetColorMatrix 設定した場合、既定の調整設定はペンに適用されなくなります。

適用対象