Marny A. López Marny A. López - 5 days ago 5
C# Question

C# Allow user to draw on winform with mouse/screen clicks

I am working on a personal project and I need to allow the client draw a "signature" on a new popup form, this by handling the events (maybe on-click and on-mouse over) events.

This signature has to be stored on an image object in order to save it to a varbinary(max) field on a database.

Googling around is not working, Any idea how this can be achieved?

Answer

I checked in my touch screen laptop, touchdown event can be handled through MouseDown event, touchup through MouseUp and touchmove through MouseMove event of the form.

Note: My machine has both touch and mouse support. I am not sure about touch only devices or machines.

Following code allows you to draw on the form through touch/mouse interactions.

public partial class Form1 : Form
{
    Image signature;
    bool clicked = false;
    Point previousPoint;

    public Form1()
    {
        this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.Paint += Form1_Paint;
        this.MouseDown += Form1_MouseDown;
        this.MouseUp += Form1_MouseUp;
        this.MouseMove += Form1_MouseMove;
        this.MouseLeave += Form1_MouseLeave;

        this.FormClosing += Form1_FormClosing;
    }

    void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        //Dispose signature after closing the form to avoid memory leak
        signature.Dispose();
    }

    void Form1_Paint(object sender, PaintEventArgs e)
    {
        if (signature != null)
            e.Graphics.DrawImage(signature, 0, 0);
    }

    void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        clicked = true;
        previousPoint = e.Location;
    }

    void Form1_MouseLeave(object sender, EventArgs e)
    {
        clicked = false;
    }

    void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        clicked = false;
    }

    void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (clicked)
        {
            if (signature == null)
                signature = new Bitmap(this.Width, this.Height);
            using (Graphics g = Graphics.FromImage(signature))
            {
                g.DrawLine(Pens.Black, previousPoint, e.Location);
                previousPoint = e.Location;
                this.Invalidate();
            }
        }
    }
}

The signature is drawn on an image. So you can save the image as required in your database.

Comments