Hello!
I am writing a small program that should allow the user to draw with the mouse. In principle, I did it all, but what worries me is how it draws a line, here is the method code:
private void mainPictureBox_MouseDown(object sender, MouseEventArgs e) { startPoint = e.Location; } private void mainPictureBox_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { drawPen = new Pen(penColorGlobal, penSizeTrackBar.Value); movePoint = e.Location; Graphics g = Graphics.FromImage(mainPictureBox.Image); g.DrawLine(drawPen, startPoint, movePoint); startPoint = movePoint; drawPen.Dispose(); g.Dispose(); mainPictureBox.Invalidate(); } } The code shows that when you lower the mouse on the picture ( MouseDown ), we startPoint starting coordinates of the startPoint object, that is, the coordinates from where we start to draw. In the MouseMove method itself, we indicate that we will draw a line from startPoint to movePoint (movePoint is the trajectory along which our mouse moves).
Everything works, but there is one thing ... If you look closely, you can see that the line drawn by us diagonally (say) will not consist of a solid line, but of some 0 0 crosses! This is especially noticeable with increasing line size.
Tell me, please, how can I fix this, or maybe I somewhere made a mistake in the code, if so correct it.
Thank you all in advance!
Fiddling around a bit, I got to the point that my MouseMove method took the following form:
private void mainPictureBox_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) //РИСУЕМ ЛЕВОЙ КЛАВИШЕЙ МЫШИ { drawPen = new Pen(penColorGlobal, 1); Brush fillBrush = new SolidBrush(penColorGlobal); Size size = new Size(penSizeTrackBar.Value, penSizeTrackBar.Value); Rectangle rectangle = new Rectangle(startPoint, size); Graphics g = Graphics.FromImage(mainPictureBox.Image); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //Сглаживание g.FillEllipse(fillBrush, rectangle); g.DrawEllipse(drawPen, rectangle); startPoint = e.Location; drawPen.Dispose(); g.Dispose(); mainPictureBox.Invalidate(); } This code draws already without crosses, and the view is much better. But now there is a completely different problem.
When we draw at a not very high speed, everything looks great, but when the mouse starts to accelerate (maybe it was pulled sharply or something else) the line goes not solid, but jerks, and since we draw in circles, this is bad, because how they are drawn separately.
Now help me deal with such a problem :)
Thanks again to everyone!
Screenshots:
drawing with lines
drawing with circles