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
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기