Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to draw a single point on canvas

I am looking for a way to draw a single point (with a color) on C# canvas. In android I would do something like

paint.Color = Color.Rgb (10, 10, 10);
canvas.DrawPoint (x, y, paint);

So I thought that I would be able to find it in the Shape class, but it was not there. Am I missing something or there is no way to draw a single point?

In the second case, what is a recommended way of drawing a point? In HTML5 canvas there is a similar problem and people are drawing points using rectangles/circles.

P.S. a question with similar title Add Point to Canvas is not answering it and moving into "how to draw a shape".

like image 700
Salvador Dali Avatar asked Nov 08 '13 02:11

Salvador Dali


2 Answers

I just ran in the same question for UWP, I finally decided to use an Ellipse:

int dotSize = 10;

Ellipse currentDot = new Ellipse();
currentDot.Stroke = new SolidColorBrush(Colors.Green);
currentDot.StrokeThickness = 3;
Canvas.SetZIndex(currentDot, 3);
currentDot.Height = dotSize;
currentDot.Width = dotSize;
currentDot.Fill = new SolidColorBrush(Colors.Green);
currentDot.Margin = new Thickness(100, 200, 0, 0); // Sets the position.
myGrid.Children.Add(currentDot);
like image 60
Romano Avatar answered Oct 30 '22 21:10

Romano


What about a Polyline?

xaml:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <Canvas x:Name="canvas" Background="#00FFFFFF" MouseMove="Canvas_MouseMove">
        <Polyline x:Name="polyline" Stroke="DarkGreen" StrokeThickness="3"/>
    </Canvas>
</Grid>

c#:

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
    polyline.Points.Add(new Point(0,0));
    polyline.Points.Add(new Point(0, 1));
    polyline.Points.Add(new Point(1, 0));
    polyline.Points.Add(new Point(1, 1));
}
like image 37
Manuel Avatar answered Oct 30 '22 23:10

Manuel