Bitmap.GetPixel(Int32, Int32) Méthode

Définition

Obtient la couleur du pixel spécifié dans ce Bitmap.

public:
 System::Drawing::Color GetPixel(int x, int y);
public System.Drawing.Color GetPixel (int x, int y);
member this.GetPixel : int * int -> System.Drawing.Color
Public Function GetPixel (x As Integer, y As Integer) As Color

Paramètres

x
Int32

Coordonnée x du pixel à récupérer.

y
Int32

Coordonnée y du pixel à récupérer.

Retours

Structure Color représentant la couleur du pixel spécifié.

Exceptions

x est inférieur à 0 ou supérieur ou égal à Width.

- ou -

y est inférieur à 0 ou supérieur ou égal à Height.

L'opération a échoué.

Exemples

L’exemple de code suivant est conçu pour être utilisé avec Windows Forms et nécessite PaintEventArgse, qui est un paramètre du Paint gestionnaire d’événements. Le code obtient la couleur d’un pixel dans une bitmap, puis remplit un rectangle avec cette couleur.

private:
   void GetPixel_Example( PaintEventArgs^ e )
   {
      // Create a Bitmap object from an image file.
      Bitmap^ myBitmap = gcnew Bitmap( "Grapes.jpg" );

      // Get the color of a pixel within myBitmap.
      Color pixelColor = myBitmap->GetPixel( 50, 50 );

      // Fill a rectangle with pixelColor.
      SolidBrush^ pixelBrush = gcnew SolidBrush( pixelColor );
      e->Graphics->FillRectangle( pixelBrush, 0, 0, 100, 100 );
   }
private void GetPixel_Example(PaintEventArgs e)
{

    // Create a Bitmap object from an image file.
    Bitmap myBitmap = new Bitmap("Grapes.jpg");

    // Get the color of a pixel within myBitmap.
    Color pixelColor = myBitmap.GetPixel(50, 50);

    // Fill a rectangle with pixelColor.
    SolidBrush pixelBrush = new SolidBrush(pixelColor);
    e.Graphics.FillRectangle(pixelBrush, 0, 0, 100, 100);
}
Private Sub GetPixel_Example(ByVal e As PaintEventArgs)

    ' Create a Bitmap object from an image file.
    Dim myBitmap As New Bitmap("Grapes.jpg")

    ' Get the color of a pixel within myBitmap.
    Dim pixelColor As Color = myBitmap.GetPixel(50, 50)

    ' Fill a rectangle with pixelColor.
    Dim pixelBrush As New SolidBrush(pixelColor)
    e.Graphics.FillRectangle(pixelBrush, 0, 0, 100, 100)
End Sub

S’applique à