A
A
Alexander Prokopenko2022-01-27 07:02:10
WPF
Alexander Prokopenko, 2022-01-27 07:02:10

How to draw in C# wpf?

I'm trying to draw on Canvas'e C# wpf, but when I quickly move the mouse I get this:
61f21897e57f9682754845.png
Then I tried lines instead of circles and everything is the same
61f218acdebe1929318974.png

Here is my code:

public void DrawPen(Point startPoint, Point endPoint, Canvas canvas, Color color, bool isClear = false)
{
    SolidColorBrush brushColor = new SolidColorBrush(color);
    Ellipse newEllipse = new Ellipse()
    {
        Stroke = brushColor,
        Width = 5,
        Height = 5
    };

    newEllipse.SetValue(Canvas.LeftProperty, endPoint.X);
    newEllipse.SetValue(Canvas.TopProperty, endPoint.Y);

    canvas.Children.Add(newEllipse);
}


This function is triggered when the left mouse button is pressed and the mouse moves on the canvas
private void CanvasMouseMove(object sender, MouseEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed)
    {
        endPoint = e.GetPosition(Canvas);

        switch (currentShape)
        {
            case Shapes.Pen:
                d.DrawPen(startPoint, endPoint, Canvas, currentColor);
                break;
            case Shapes.Line:
                d.DrawLine(startPoint, endPoint, Canvas, currentColor);
                break;
            case Shapes.Rectangle:
                d.DrawRectangle(startPoint, endPoint, Canvas, currentColor);
                break;
            case Shapes.Ellipse:
                d.DrawEllipse(startPoint, endPoint, Canvas, currentColor);
                break;
        }
    }        
}


How can I draw quickly and normally?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
HemulGM, 2022-01-27
@AlexanderProkopenko

You need to set the old cursor position and draw this line while moving

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question