Bitmap.GetPixel(Int32, Int32) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets the color of the specified pixel in this 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
Parameters
- x
- Int32
The x-coordinate of the pixel to retrieve.
- y
- Int32
The y-coordinate of the pixel to retrieve.
Returns
A Color structure that represents the color of the specified pixel.
Exceptions
x
is less than 0, or greater than or equal to Width.
-or-
y
is less than 0, or greater than or equal to Height.
The operation failed.
Examples
The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e
, which is a parameter of the Paint event handler. The code gets the color of a pixel in a bitmap, and then fills a rectangle with that color.
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
Applies to
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應