Search code examples
c#winformssystem.drawing

C# Graphics class want to make signature panel : input from drawing tablet


i am trying to make signature panel in c# windowsform application where input is from drawing tablet

my code as below this code working for line drawing not dot created.

So please suggest how dot and line both are create.

    {
        Graphics graphics;
        Boolean cusorMoving = false;
        Pen cursorPen;
        int cursorX = -1;
        int cursorY = -1;
       
        public SignPad()
        {
            InitializeComponent();
            graphics = panel2.CreateGraphics();
            cursorPen = new Pen(Color.Black, 2);
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            cursorPen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
            cursorPen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
        }

       
           

Mouse Down event

        private void panel2_MouseDown(object sender, MouseEventArgs e)
        {
            cusorMoving = true;
            cursorX = e.X;
            cursorY = e.Y;
        }

        private void panel2_MouseUp(object sender, MouseEventArgs e)
        {
            cusorMoving = false;
           
            cursorX = -1;
            cursorY = -1;

        }

Mouse Move event


        private void panel2_MouseMove(object sender, MouseEventArgs e)
        {
            if (cursorX != -1 && cursorY != -1 && cusorMoving == true)
            {
                graphics.DrawLine(cursorPen, new Point(cursorX, cursorY), e.Location);
                cursorX = e.X;
                cursorY = e.Y;
            }            
                         
        }                
    

Solution

  • You need to store individual points in a collection and draw them separately in the Paint handler. Every time you add a point to the collection, you also need to tell the panel to draw the area where the new segment was added. Something like this:

    using System.Collections.Generic;
    using System.Drawing;
    
    namespace Lines
    {
        public partial class SignPad : Form
        {
            Pen cursorPen = SystemPens.ControlText;
            List<Point> points = new List<Point>();
            bool cursorMoving = false;
    
            public SignPad()
            {
                InitializeComponent();
    
                cursorPen = new Pen(Color.Black, 2);
                cursorPen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
                cursorPen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
            }
    
            private void panel2_Paint(object? sender, PaintEventArgs e)
            {
                var g = e.Graphics;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    
                for (int i = 1; i < points.Count; ++i)
                    g.DrawLine(cursorPen, points[i - 1], points[i]);
            }
    
            private void panel2_MouseDown(object? sender, MouseEventArgs e)
            {
                if (!cursorMoving)
                {
                    cursorMoving = true;
                    points.Clear();
                    points.Add(e.Location);
                    panel2.Invalidate();
                }
            }
    
            private void panel2_MouseMove(object? sender, MouseEventArgs e)
            {
                if (cursorMoving && points.Count > 0)
                {
                    var p = e.Location;
                    var q = points[points.Count - 1];
                    var r = Rectangle.FromLTRB(Math.Min(p.X, q.X), Math.Min(p.Y, q.Y), Math.Max(p.X, q.X), Math.Max(p.Y, q.Y));
                    r = Rectangle.Inflate(r, (int)cursorPen.Width, (int)cursorPen.Width);
    
                    points.Add(p);
                    panel2.Invalidate(r);
                }
            }
    
            private void panel2_MouseUp(object? sender, MouseEventArgs e)
            {
                cursorMoving = false;
            }
        }
    }
    

    Don't forget to add the Paint handler the same way you added MouseMove, MouseDown and MouseUp handlers - in the Designer.