Draw a single pixel dot

Once again GDI+ resolution independence jumps up to bite you in the soft and tender parts. There is no PlotPixel function in GDI+ that allows you to draw directly on the GDI Graphics surface as we used to be able to in GDI..

To draw a dot of a specific colour on your screen you must resort to the following shenanigans.

  • Create a 1x1 bitmap image
  • Set its only pixel to the colour you desire using Bitmap.SetPixel
  • use Graphics.DrawImageUnscaled(...) to draw the dot in the correct place

The following example assume that they are in a Paint event handler.

[C#]

Bitmap bm=new Bitmap(1,1);

bm.SetPixel(0,0,Color.Red);

e.Graphics.DrawImageUnscaled(bm,<xPosition>,<yPosition>);

[VB]

Dim bm as new Bitmap(1,1)

bm.SetPixel(0,0,Color.Red)

e.Graphics.DrawImageUnscaled(bm,<xPosition>,<yPosition>)

Remember that you don't have to create the bitmap every time you draw it. You can store a 1 pixel bitmap for the lifetime of the application and just copy it over and over.

Back to the GDI+ FAQ

Copyright Robert W. Powell 2003, All rights reserved