Bitmap.GetPixel(Int32, Int32) Metodo

Definizione

Ottiene il colore del pixel specificato in questo oggetto 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

Parametri

x
Int32

Coordinata x del pixel da recuperare.

y
Int32

Coordinata y del pixel da recuperare.

Restituisce

Struttura Color che rappresenta il colore del pixel specificato.

Eccezioni

x è minore di 0 oppure maggiore o uguale all'oggetto Width.

-oppure-

y è minore di 0 oppure maggiore o uguale all'oggetto Height.

Operazione non riuscita.

Esempio

L'esempio di codice seguente è progettato per l'uso con Windows Forms e richiede PaintEventArgse, che è un parametro del Paint gestore eventi. Il codice ottiene il colore di un pixel in una bitmap e quindi riempie un rettangolo con tale colore.

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

Si applica a