Bitmap.GetPixel 方法

获取此 Bitmap 中指定像素的颜色。

**命名空间:**System.Drawing
**程序集:**System.Drawing(在 system.drawing.dll 中)

语法

声明
Public Function GetPixel ( _
    x As Integer, _
    y As Integer _
) As Color
用法
Dim instance As Bitmap
Dim x As Integer
Dim y As Integer
Dim returnValue As Color

returnValue = instance.GetPixel(x, y)
public Color GetPixel (
    int x,
    int y
)
public:
Color GetPixel (
    int x, 
    int y
)
public Color GetPixel (
    int x, 
    int y
)
public function GetPixel (
    x : int, 
    y : int
) : Color

参数

  • x
    要检索的像素的 x 坐标。
  • y
    要检索的像素的 y 坐标。

返回值

Color 结构,它表示指定像素的颜色。

异常

异常类型 条件

ArgumentOutOfRangeException

x 小于 0,或者大于或等于 Width

y 小于 0,或者大于或等于 Height

Exception

操作失败。

示例

下面的代码示例设计为与 Windows 窗体一起使用,它需要 PaintEventArgse,即 Paint 事件处理程序的一个参数。此代码获取位图中像素的颜色,然后用该颜色填充一个矩形。

Public 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
public 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);
}
public:
   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 );
   }

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Bitmap 类
Bitmap 成员
System.Drawing 命名空间