At the beginning, I simply drew an ellipse with the current coordinate on onMouseMove, but I ran into the problem of not getting a continuous line.

I decided to connect the dots with lines, but another problem came out - on a large thickness when turning, the corners of the beginning and end of the lines seemed to crawl over the smear area and peeped out.

I decided to return to the first variant, but instead of drawing a single point, draw many points with an offset. But even here the problem is, due to the imposition, smoothing deteriorates, the smear becomes coarse, and even the transparency is leveled. Plus, either you add a lot of intermediate points and everything starts to slow down, or breaks at high speed anyway.

Example

The question is how it is implemented in various graphic editors? To rake a bunch of abstractions from some open source application broke.

    1 answer 1

    Try the following code:

    using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { //InitializeComponent(); DoubleBuffered = true; Size = new Size(500, 500); MouseMove += Form1_MouseMove; Paint += Form1_Paint; } List<Point> points = new List<Point>(); private void Form1_Paint(object sender, PaintEventArgs e) { if (points.Count > 1) { using (Pen pen = new Pen(Color.Red, 20)) { pen.StartCap = LineCap.Round; pen.EndCap = LineCap.Round; pen.LineJoin = LineJoin.Round; e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; e.Graphics.DrawLines(pen, points.ToArray()); } } } private void Form1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { points.Add(e.Location); this.Refresh(); } } } } 

    To avoid flickering when drawing, set the DoubleBufferd = true property of the control on which we draw (in this case, the form).

    To make the ends of the lines round, set the StartCap and EndCap properties to pen. To avoid uneven line connections, set the LineJoin property to LineJoin .

    And finally, so that the lines are not a hedgehog, we set the SmoothingMode graphic.

    • pen.LineJoin = LineJoin.Round; what I missed is - Oleksiy Logvinenko