Bitmap.SetPixel(Int32, Int32, Color) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
设置此 Bitmap中指定像素的颜色。
public:
void SetPixel(int x, int y, System::Drawing::Color color);
public void SetPixel (int x, int y, System.Drawing.Color color);
member this.SetPixel : int * int * System.Drawing.Color -> unit
Public Sub SetPixel (x As Integer, y As Integer, color As Color)
参数
- x
- Int32
要设置的像素的 x 坐标。
- y
- Int32
要设置的像素的 y 坐标。
例外
操作失败。
示例
下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse
,这是 Paint 事件处理程序的参数。 该代码执行以下操作:
创建 Bitmap。
将位图中每个像素的颜色设置为黑色。
绘制位图。
private:
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 );
}
private 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);
}
Private 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
注解
使用 SetPixel 方法以编程方式设置图像中单个像素的颜色。 还可以使用 LockBits 方法以编程方式更改映像。 通常,对于大规模更改,LockBits 方法可提供更好的性能。