Export (0) Print
Expand All

Bitmap.GetPixel Method

Gets the color of the specified pixel in this Bitmap.

[Visual Basic]
Public Function GetPixel( _
   ByVal x As Integer, _
   ByVal y As Integer _
) As Color
[C#]
public Color GetPixel(
 int x,
 int y
);
[C++]
public: Color GetPixel(
 int x,
 int y
);
[JScript]
public function GetPixel(
   x : int,
 y : int
) : Color;

Parameters

x
The x-coordinate of the pixel to retrieve.
y
The y-coordinate of the pixel to retrieve.

Return Value

A Color structure that represents the color of the specified pixel.

Example

[Visual Basic, C#] The following 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.

[Visual Basic] 
Public Sub GetPixel_Example(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
        
[C#] 
public void GetPixel_Example(PaintEventArgs e)
{
// Create a Bitmap object from an image file.
Bimap 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);
}
        

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Bitmap Class | Bitmap Members | System.Drawing Namespace

Show:
© 2014 Microsoft