This documentation is archived and is not being maintained.

Bitmap.SetPixel Method

Sets the color of the specified pixel in this Bitmap.

Namespace: System.Drawing
Assembly: System.Drawing (in system.drawing.dll)

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
Not applicable.



The x-coordinate of the pixel to set.


The y-coordinate of the pixel to set.


A Color structure that represents the color to assign to the specified pixel.

Return Value

This method does not return a value.

Exception typeCondition


The operation failed.

The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates a Bitmap.

  • Sets the color of each pixel in the bitmap to black.

  • Draws the bitmap.

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,
    // 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 Server 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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0