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
alt text

drawing with circles
alt text

  • one
    Can you upload a screenshot? - Specter
  • Already laid out - MikroFF

3 answers 3

Here is another option for not showing breaks: memorize points in MouseMove and draw them again.

  private void mainPictureBox_MouseDown(object sender, MouseEventArgs e) { _points = new HashSet<Point> {e.Location}; } private void mainPictureBox_MouseMove(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) //РИСУЕМ ЛЕВОЙ КЛАВИШЕЙ МЫШИ { return; } _points.Add(e.Location); using (var drawPen = new Pen(penColorGlobal, penSizeTrackBar.Value)) using (var g = Graphics.FromImage(mainPictureBox.Image)) { g.DrawLines(drawPen, _points.ToArray()); } mainPictureBox.Invalidate(); } 

    All the same, he did everything, of course he suffered a little, but brought it to mind:

      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.DrawLine(drawPen, startPoint, e.Location); startPoint = e.Location; drawPen.Dispose(); g.Dispose(); mainPictureBox.Invalidate(); } 

      Generally speaking, without blurring, an oblique line with respect to the location of the pixels will always be drawn by a ladder. If you look closely at any Microsoft Word, you will see that there is the same thing. If this is the problem, look at System.Drawing.Drawing2D for blurring.

      • After adding my post, I thought of it and put a line in the code: g.SmoothingMode = SmoothingMode.AntiAlias; I will tell you the effect, but not as much as needed. - MikroFF