ImageAttributes.SetColorMatrix Método

Definição

Define a matriz de ajuste de cores.

Sobrecargas

SetColorMatrix(ColorMatrix)

Define a matriz de ajuste de cores para a categoria padrão.

SetColorMatrix(ColorMatrix, ColorMatrixFlag)

Define a matriz de ajuste de cores para a categoria padrão.

SetColorMatrix(ColorMatrix, ColorMatrixFlag, ColorAdjustType)

Define a matriz de ajuste de cores de uma categoria especificada.

SetColorMatrix(ColorMatrix)

Origem:
ImageAttributes.cs
Origem:
ImageAttributes.cs
Origem:
ImageAttributes.cs

Define a matriz de ajuste de cores para a categoria padrão.

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)

Parâmetros

newColorMatrix
ColorMatrix

A matriz de ajuste de cores.

Exemplos

O exemplo de código a seguir foi projetado para uso com Windows Forms e requer PaintEventArgse, que é um parâmetro do Paint manipulador de eventos. O código executa as seguintes ações:

  1. Cria uma imagem de retângulo que tem todos os valores de cor definidos como 128, produzindo um retângulo que é preenchido com uma cor cinza médio sólida. Em seguida, o código desenha essa imagem de retângulo na tela.

  2. Cria um ColorMatrix e define sua Matrix localização como 1,75, o que enfatiza o componente vermelho da imagem.

  3. Cria um ImageAttributes objeto e chama o SetColorMatrix método .

  4. Desenha a imagem (um segundo retângulo) na tela usando o ColorMatrix apenas definido no ImageAttributes objeto .

Observe que o segundo retângulo tem a cor vermelha enfatizada.

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

Comentários

Um ImageAttributes objeto mantém as configurações de cor e escala de cinza para cinco categorias de ajuste: padrão, bitmap, pincel, caneta e texto. Por exemplo, você pode especificar uma matriz de ajuste de cor para a categoria padrão, uma matriz de ajuste de cor diferente para a categoria de bitmap e ainda uma matriz de ajuste de cor diferente para a categoria de caneta.

As configurações padrão de ajuste de cor e ajuste de escala de cinza se aplicam a todas as categorias que não têm configurações de ajuste próprias. Por exemplo, se você nunca especificar nenhuma configuração de ajuste para a categoria de caneta, as configurações padrão se aplicarão à categoria de caneta.

Chamar o ImageAttributes.SetColorMatrix(ColorMatrix) método é equivalente a chamar o ImageAttributes.SetColorMatrix(ColorMatrix, ColorMatrixFlag) método e passar ColorMatrixFlag.Default para o flags parâmetro . ColorMatrixFlag.Default especifica que todas as cores (incluindo cinzas) são ajustadas pela matriz de ajuste de cor

Confira também

Aplica-se a

SetColorMatrix(ColorMatrix, ColorMatrixFlag)

Origem:
ImageAttributes.cs
Origem:
ImageAttributes.cs
Origem:
ImageAttributes.cs

Define a matriz de ajuste de cores para a categoria padrão.

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)

Parâmetros

newColorMatrix
ColorMatrix

A matriz de ajuste de cores.

flags
ColorMatrixFlag

Um elemento de ColorMatrixFlag que especifica o tipo de imagem e a cor que serão afetados pela matriz de ajuste de cores.

Exemplos

Para obter um exemplo de código, consulte o SetColorMatrix(ColorMatrix) método .

Comentários

Um ImageAttributes objeto mantém as configurações de cor e escala de cinza para cinco categorias de ajuste: padrão, bitmap, pincel, caneta e texto. Por exemplo, você pode especificar uma matriz de ajuste de cor para a categoria padrão, uma matriz de ajuste de cor diferente para a categoria de bitmap e ainda uma matriz de ajuste de cor diferente para a categoria de caneta.

As configurações padrão de ajuste de cor e ajuste de escala de cinza se aplicam a todas as categorias que não têm configurações de ajuste próprias. Por exemplo, se você nunca especificar nenhuma configuração de ajuste para a categoria de caneta, as configurações padrão se aplicarão à categoria de caneta.

Confira também

Aplica-se a

SetColorMatrix(ColorMatrix, ColorMatrixFlag, ColorAdjustType)

Origem:
ImageAttributes.cs
Origem:
ImageAttributes.cs
Origem:
ImageAttributes.cs

Define a matriz de ajuste de cores de uma categoria especificada.

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)

Parâmetros

newColorMatrix
ColorMatrix

A matriz de ajuste de cores.

mode
ColorMatrixFlag

Um elemento de ColorMatrixFlag que especifica o tipo de imagem e a cor que serão afetados pela matriz de ajuste de cores.

type
ColorAdjustType

Um elemento de ColorAdjustType que especifica a categoria com a qual a matriz de ajuste de cores é definida.

Exemplos

O exemplo de código a seguir demonstra como usar o SetColorMatrix método . Para executar este exemplo, cole o código em um Windows Form e chame RotateColors do método de manipulação de eventos do Paint formulário, passando e como 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

Comentários

Um ImageAttributes objeto mantém as configurações de cor e escala de cinza para cinco categorias de ajuste: padrão, bitmap, pincel, caneta e texto. Por exemplo, você pode especificar uma matriz de ajuste de cor para a categoria padrão, uma matriz de ajuste de cor diferente para a categoria de bitmap e ainda uma matriz de ajuste de cor diferente para a categoria de caneta.

As configurações padrão de ajuste de cor e ajuste de escala de cinza se aplicam a todas as categorias que não têm configurações de ajuste próprias. Por exemplo, se você nunca especificar nenhuma configuração de ajuste para a categoria de caneta, as configurações padrão se aplicarão à categoria de caneta.

Assim que você especificar uma configuração de ajuste de cor ou ajuste de escala de cinza para uma determinada categoria, as configurações de ajuste padrão não se aplicam mais a essa categoria. Por exemplo, suponha que você especifique uma coleção de configurações de ajuste para a categoria padrão. Se você definir a matriz de ajuste de cor para a categoria de caneta passando Pen para o SetColorMatrix método , nenhuma das configurações de ajuste padrão será aplicada a canetas.

Aplica-se a