Search code examples
c#winformspointbrushpen

Can a Pen or Brush paint a Point?


I know that I can draw a filled circle and I can draw many simple and complicated things with a Graphics. But I couldn't get it to draw a single point (not a single pixel).

I'm playing with a Paint program and the user can draw fine but not plot a dot. I can add a dummy point really close or can draw a filled cirlce but sometimes I miss the obvious.

So is there a way to draw a single point with a given Brush or Pen?

And no, of course I don't mean to draw a single Pixel. I want to use the Properties like color and width. Like a DrawLine with only one Point or with the same Point twice. But that renders nothing.


Solution

  •     public void DrawPoint(Graphics G, Pen pen, Point point)
        {
            // add more LineCaps as needed..
            int pw2 = (int ) Math.Max(1, pen.Width / 2);
            using(var brush = new SolidBrush(pen.Color))
            {
                if (pen.EndCap == LineCap.Square)
                    G.FillRectangle(brush, point.X - pw2, point.Y - pw2, pen.Width, pen.Width);
                else
                    G.FillEllipse(brush, point.X - pw2, point.Y - pw2, pen.Width, pen.Width);
            }
        }