Bitmap.GetPixel(Int32, Int32) Metod

Definition

Hämtar färgen på den angivna pixeln i den här 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

Parametrar

x
Int32

X-koordinaten för pixeln som ska hämtas.

y
Int32

Y-koordinaten för pixeln som ska hämtas.

Returer

En Color struktur som representerar färgen på den angivna pixeln.

Undantag

x är mindre än 0 eller större än eller lika med Width.

-eller-

y är mindre än 0 eller större än eller lika med Height.

Åtgärden misslyckades.

Exempel

Följande kodexempel är utformat för användning med Windows Forms och kräver PaintEventArgse, vilket är en parameter för händelsehanteraren Paint. Koden hämtar färgen på en pixel i en bitmapp och fyller sedan en rektangel med den färgen.

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

Gäller för