Answer the question
In order to leave comments, you need to log in
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:
Then I tried lines instead of circles and everything is the same
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);
}
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;
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question