Export (0) Print
Expand All

Bitmap.SetPixel Method

Sets the color of the specified pixel in this Bitmap object.

[Visual Basic]
Public Sub SetPixel( _
   ByVal x As Integer, _
   ByVal y As Integer, _
   ByVal color As Color _
)
[C#]
public void SetPixel(
 int x,
 int y,
 Color color
);
[C++]
public: void SetPixel(
 int x,
 int y,
 Color color
);
[JScript]
public function SetPixel(
   x : int,
 y : int,
 color : Color
);

Parameters

x
The x-coordinate of the pixel to set.
y
The y-coordinate of the pixel to set.
color
A Color structure that represents the color to assign to the specified pixel.

Return Value

This method does not return a value.

Example

[Visual Basic, C#] The following example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates a Bitmap object.
  • Sets the color of each pixel in the bitmap to black.
  • Draws the bitmap.
[Visual Basic] 
Public Sub SetPixel_Example(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
        
[C#] 
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);
}
        

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Bitmap Class | Bitmap Members | System.Drawing Namespace

Show:
© 2014 Microsoft