Bitmap.SetPixel 方法

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

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

语法

声明
Public Sub SetPixel ( _
    x As Integer, _
    y As Integer, _
    color As Color _
)
用法
Dim instance As Bitmap
Dim x As Integer
Dim y As Integer
Dim color As Color

instance.SetPixel(x, y, color)
public void SetPixel (
    int x,
    int y,
    Color color
)
public:
void SetPixel (
    int x, 
    int y, 
    Color color
)
public void SetPixel (
    int x, 
    int y, 
    Color color
)
public function SetPixel (
    x : int, 
    y : int, 
    color : Color
)

参数

  • x
    要设置的像素的 x 坐标。
  • y
    要设置的像素的 y 坐标。
  • color
    Color 结构,它表示要分配给指定像素的颜色。

返回值

此方法不返回值。

异常

异常类型 条件

Exception

操作失败。

示例

下面的代码示例设计为与 Windows 窗体一起使用,它需要 PaintEventArgse,即 Paint 事件处理程序的一个参数。代码执行下列操作:

  • 创建一个 Bitmap

  • 将位图中每个像素的颜色设置为黑色。

  • 绘制该位图。

Public Sub SetPixel_Example(ByVal e As PaintEventArgs)

    ' Create a Bitmap object from a file.
    Dim myBitmap As New Bitmap("Grapes.jpg")

    ' Draw myBitmap to the screen.
    e.Graphics.DrawImage(myBitmap, 0, 0, myBitmap.Width, _
    myBitmap.Height)

    ' Set each pixel in myBitmap to black.
    Dim Xcount As Integer
    For Xcount = 0 To myBitmap.Width - 1
        Dim Ycount As Integer
        For Ycount = 0 To myBitmap.Height - 1
            myBitmap.SetPixel(Xcount, Ycount, Color.Black)
        Next Ycount
    Next Xcount

    ' Draw myBitmap to the screen again.
    e.Graphics.DrawImage(myBitmap, myBitmap.Width, 0, myBitmap.Width, _
        myBitmap.Height)
End Sub
public void SetPixel_Example(PaintEventArgs e)
{
             
    // Create a Bitmap object from a file.
    Bitmap myBitmap = new Bitmap("Grapes.jpg");
             
    // Draw myBitmap to the screen.
    e.Graphics.DrawImage(myBitmap, 0, 0, myBitmap.Width,
        myBitmap.Height);
             
    // Set each pixel in myBitmap to black.
    for (int Xcount = 0; Xcount < myBitmap.Width; Xcount++)
    {
        for (int Ycount = 0; Ycount < myBitmap.Height; Ycount++)
        {
            myBitmap.SetPixel(Xcount, Ycount, Color.Black);
        }
    }
             
    // Draw myBitmap to the screen again.
    e.Graphics.DrawImage(myBitmap, myBitmap.Width, 0,
        myBitmap.Width, myBitmap.Height);
}
public:
   void SetPixel_Example( PaintEventArgs^ e )
   {
      // Create a Bitmap object from a file.
      Bitmap^ myBitmap = gcnew Bitmap( "Grapes.jpg" );

      // Draw myBitmap to the screen.
      e->Graphics->DrawImage( myBitmap, 0, 0, myBitmap->Width, myBitmap->Height );

      // Set each pixel in myBitmap to black.
      for ( int Xcount = 0; Xcount < myBitmap->Width; Xcount++ )
      {
         for ( int Ycount = 0; Ycount < myBitmap->Height; Ycount++ )
         {
            myBitmap->SetPixel( Xcount, Ycount, Color::Black );
         }
      }

      // Draw myBitmap to the screen again.
      e->Graphics->DrawImage( myBitmap, myBitmap->Width, 0, myBitmap->Width, myBitmap->Height );
   }

平台

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 命名空间