0

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.

Julián Urbano
  • 8,378
  • 1
  • 30
  • 52
TaW
  • 53,122
  • 8
  • 69
  • 111
  • Can you post a sreenshot of what you mean? – BlueM Mar 31 '14 at 14:04
  • That should not be necessary. Imagine a thick line, now make it shorter and shorter. Shorter, not thinner. When it is reduced to a single Point it will look like..well, it should look like an endpoint, not even necessarily a cirlce. – TaW Mar 31 '14 at 14:44
  • What's the difference between a single point and a single pixel in your world? – LarsTech Mar 31 '14 at 15:21
  • A point is a geometrical construct, like a line or a circle. When you draw it you use a tool like a pen or a brush of a certain width and color and you add color to your drawing surface. Here the surface consists of pixels. If the width of my pen is 1 then the point would indeed be just one pixel wide but, as I have written in my OP, I want to use a given Pen with its given width. – TaW Mar 31 '14 at 15:29
  • You can't get around the fact that WinForms is pixel based. Make sure you have the smoothmode antialiased. Do provide some code or a picture that demonstrates what problem you are having. – LarsTech Mar 31 '14 at 15:35
  • A single pixel is a 1x1 rectangle. Use Graphics.FillRectangle() – Hans Passant Mar 31 '14 at 15:39
  • 1
    Sigh. I must have a really bad day expressing myself. I was missing a Graphics.DrawPoint(Pen, Point) method. But hey, I can write one myself, thank you very much.. – TaW Mar 31 '14 at 15:42

1 Answers1

1
    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);
        }
    }
John Alexiou
  • 28,472
  • 11
  • 77
  • 133
TaW
  • 53,122
  • 8
  • 69
  • 111