Bitmap.GetPixel(Int32, Int32) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得這個 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
參數
- x
- Int32
要擷取之圖元的 X 座標。
- y
- Int32
要擷取之圖元的 Y 座標。
傳回
Color 結構,表示指定圖元的色彩。
例外狀況
作業失敗。
範例
下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse
,這是 Paint 事件處理程式的參數。 程序代碼會取得位圖中像素的色彩,然後使用該色彩填滿矩形。
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