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 Forms 使用而設計,而且需要 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 方法可提供更佳的效能。