Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw a single pixel on Windows Forms

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#.

like image 695
Mark T Avatar asked Apr 17 '09 15:04

Mark T


3 Answers

This will set a single pixel:

e.Graphics.FillRectangle(aBrush, x, y, 1, 1);
like image 125
Henk Holterman Avatar answered Nov 16 '22 07:11

Henk Holterman


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?

like image 21
Adam Robinson Avatar answered Nov 16 '22 08:11

Adam Robinson


Just to show complete code for Henk Holterman answer:

Brush aBrush = (Brush)Brushes.Black;
Graphics g = this.CreateGraphics();

g.FillRectangle(aBrush, x, y, 1, 1);
like image 18
WoodyDRN Avatar answered Nov 16 '22 08:11

WoodyDRN