Take the 2-minute tour ×

I'm stuck trying to turn on a single pixel on a Windows Form.

graphics.DrawLine(Pens.Black, 50, 50, 51, 50); // draws two pixels

graphics.DrawLine(Pens.Black, 50, 50, 50, 50); // draws no pixels

The API really should have a method to set the color of one pixel, but I don't see one.

I am using C#.

share|improve this question
I made a control which plots some very nice scientific graphs (with added capabilities I need not available in any commercial controls). I can plot a data point with and X or a + or a small box. But for unimportant data, I just want a single pixel. –  Mark T Apr 17 '09 at 16:10
add comment

4 Answers

up vote 42 down vote accepted

This will set a single pixel:

e.Graphics.FillRectangle(aBrush, x, y, 1, 1);
share|improve this answer
Who would have guessed that DrawRectangle draws a 2x2 but FillRectange draws a 1x1 with the same arguments? Thanks. –  Mark T Apr 17 '09 at 16:20
@Mark: It does make a sort of sense... DrawRectangle says "draw an outline starting at x and y of this width and height," so it draws both the start and end points (x,y; x+1,y;x,y+1; x+1,y+1) and lines between them. FillRectangle says "draw a solid surface starting at x,y of this width and height." Incidentally, FillRectangle takes a Brush, not a Pen. –  Powerlord Apr 17 '09 at 16:42
@R. Bemrose: Actually, it depends on the Pen object you pass to DrawRectangle(). If it has Pen.Alignment = PenAlignment.Outset it will draw Pen.Width pixels thick outline round the given rectangle. But if you specify Pen.Alignment = PenAlignment.Inset it will draw Pen.Width pixels thick "outline" inside the given rectangle. –  Cipi Jul 25 '11 at 13:36
This is a very unsatisfactory answer. Although this sets a single pixel, the requirement is not always to set a literal pixel. All the times I've needed this, which is often, I've needed the dot to be drawn by the same pen that lines are drawn with, not a brush. In other words, we need a line that is shorter that 2 pixels. I suspect that is what the OP needed. –  RobertT Feb 18 '12 at 13:39
I don't like this answer. This way is highly inefficient. –  user626528 Feb 22 '13 at 10:51
show 1 more comment

The Graphics object doesn't have this, since it's an abstraction and could be used to cover a vector graphics format. In that context, setting a single pixel wouldn't make sense. The Bitmap image format does have GetPixel() and SetPixel(), but not a graphics object built on one. For your scenario, your option really seems like the only one because there's no one-size-fits-all way to set a single pixel for a general graphics object (and you don't know EXACTLY what it is, as your control/form could be double-buffered, etc.)

Why do you need to set a single pixel?

share|improve this answer
add comment

Where I'm drawing lots of single pixels (for various customised data displays), I tend to draw them to a bitmap and then blit that onto the screen.

The Bitmap GetPixel and SetPixel operations are not particularly fast because they do an awful lot of boundschecking, but it's quite easy to make a 'fast bitmap' class which has quick access to a bitmap.

share|improve this answer
add comment

Apparently DrawLine draws a line that is one pixel short of the actual specified length. There doesn't seem to be a DrawPoint/DrawPixel/whatnot, but instead you can use DrawRectangle with width and height set to 1 to draw a single pixel.

share|improve this answer
Nice try, but with size 1 it draws 2x2 pixels. (Draw it next to a line, and you can see it is twice as wide.) And with size zero, of course, it draws nothing. –  Mark T Apr 17 '09 at 16:16
Right, my mistake. :) Good thing FillRectangle does the trick. –  Rytmis Apr 17 '09 at 16:24
But DrawLine draws short. –  Joshua Sep 28 '11 at 17:40
add comment

Your Answer


