5

Must be a simple question, yet I can't find or figure out myself solution to it.

How to draw a single point in wpf?

In winforms I did something like

graphics.DrawLine(pen, x, y, x+1, y+1);

But in wpf x and y become double (or well, actually I never used float in winforms), then there is a Pen and device dependend/independent units, so adding +1 doesn't looks good anymore. How would you draw single point in wpf?

Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • 1
    The 'solution' for WinForms doesn't really look good, it is more of an oval. The way to do it, since a DrawPoint is lacking is to draw a filled circle with the width of the pen. See my own [question here!](http://stackoverflow.com/questions/22763643/can-a-pen-or-brush-paint-a-point) – TaW Aug 19 '14 at 11:16
  • 1
    @Taw, `DrawEllipse(brush, null, x, y, d / 2, d / 2)` (d - diameter or `Pen` thickness) produces good results, thanks. – Sinatr Aug 19 '14 at 11:26

1 Answers1

8

You can draw a point in a number of ways in WPF. One way is using the Ellipse class... try this XAML:

<Ellipse Width="1" Height="1" Stroke="Black" HorizontalAlignment="Center" 
    VerticalAlignment="Center" />

Alternatively, you could try using the DrawingContext.DrawEllipse Method to draw your point:

YourDrawingContext.DrawEllipse(YourBrush, YourPen, YourPointPosition, radiusX, radius);
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Using DrawEllipse for drawing a point from a pen (color and thickness) you should have Brush=null and radius=0. Alternatively Brush=pen.Brush and Radius=pen.Thickness/2. Both works in my application, but I haven't tested alpha blending for which I think the former may have issues, depending on how the internals are implemented. – Andreas Apr 17 '23 at 06:32