NEWS

[C#] Hướng dẫn tạo chữ ký trên winform Signature Pad

[C#] Hướng dẫn tạo chữ ký trên winform Signature Pad
Đăng bởi: Thảo Meo - Lượt xem: 4967 20:43:37, 10/05/2022DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẻ tiếp tục hướng dẫn các bạn cách tạo chữ ký trên Winform Signature Pad.

[C#] Signature Pad in Winform

Dưới đây, là giao diện demo ứng dụng Signature Pad c#:

SignaturePad_winform

Khi ký xong các bạn có thể Export và Import, file chữ ký được lưu dưới dạng XML.

<?xml version="1.0" encoding="utf-8"?>
<Signature xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Glyphs>
    <Glyph>
      <Lines>
        <Line>
          <StartPoint>
            <X>61</X>
            <Y>42</Y>
          </StartPoint>
          <EndPoint>
            <X>61</X>
            <Y>42</Y>
          </EndPoint>
        </Line>
        <Line>
          <StartPoint>
            <X>61</X>
            <Y>42</Y>
.........................

1. Tạo class Line.cs

namespace SignaturePad
{
 
        [Serializable]
        public class Line
        {
            public Line()
            {

            }

            public Line(Point startPoint, Point endPoint)
            {
                this.StartPoint = startPoint;
                this.EndPoint = endPoint;
            }

            public Point StartPoint { get; set; }
            public Point EndPoint { get; set; }
        }
   
}

2. Tạo class Glyph.cs

namespace SignaturePad
{

        [System.Serializable]
        public class Glyph
        {
            public Glyph()
            {
                this.Lines = new List<Line>();
            }
            public List<Line> Lines { get; set; }
        }
   
}

3. Tạo class Signature.cs

namespace SignaturePad
{
   
        [Serializable]
        public class Signature
        {
            public Signature()
            {
                this.Glyphs = new List<Glyph>();
            }

            public List<Glyph> Glyphs { get; set; }
        }
   
}

Full source code C# Form1.cs:

using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace SignaturePad
{
    public partial class Form1 : Form
    {
        Boolean IsCapturing = false;
        private Point startPoint;
        private Point endPoint;
        Pen pen = new Pen(Color.Black);
        Glyph glyph = null;
        Signature signature = new Signature();
        String fileName = @"signature.xml";

        public Form1()
        {
            InitializeComponent();
        }

        public Cursor CreateCursor(Bitmap bitmap, Size size)
        {
            bitmap = new Bitmap(bitmap, size);
            return new Cursor(bitmap.GetHicon());
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            SignaturePanel.MouseMove += SignaturePanel_MouseMove;
            SignaturePanel.MouseUp += SignaturePanel_MouseUp;
            SignaturePanel.MouseDown += SignaturePanel_MouseDown;
            var bitmap = (Bitmap)Image.FromFile("Pen-icon.png");
            SignaturePanel.Cursor = CreateCursor(bitmap, new Size(bitmap.Width, bitmap.Height)); ;
        }

        private void SignaturePanel_MouseDown(object sender, MouseEventArgs e)
        {
            IsCapturing = true;
            glyph = new Glyph();
        }

        private void SignaturePanel_MouseUp(object sender, MouseEventArgs e)
        {
            IsCapturing = false;
            signature.Glyphs.Add(glyph);
            startPoint = new Point();
            endPoint = new Point();
        }



        private void SignaturePanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsCapturing)
            {
                if (startPoint.IsEmpty && endPoint.IsEmpty)
                {
                    endPoint = e.Location;
                }
                else
                {
                    startPoint = endPoint;
                    endPoint = e.Location;
                    Line line = new Line(startPoint, endPoint);
                    glyph.Lines.Add(line);
                    DrawLine(line);
                }

            }
        }

        private void DrawLine(Line line)
        {
            using (Graphics graphic = this.SignaturePanel.CreateGraphics())
            {
                graphic.DrawLine(pen, line.StartPoint, line.EndPoint);
            }
        }

        private void SerializeSignature()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Signature));

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            using (TextWriter textWriter = new StreamWriter(fileName))
            {
                serializer.Serialize(textWriter, signature);
                textWriter.Close();
            }
        }

        private void DeserializeSignature()
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(Signature));
            using (TextReader textReader = new StreamReader(fileName))
            {
                signature = (Signature)deserializer.Deserialize(textReader);
                textReader.Close();
            }
        }

        private void ClearSignaturePanel()
        {
           
            using (Graphics graphic = this.SignaturePanel.CreateGraphics())
            {
                SolidBrush solidBrush = new SolidBrush(Color.LightBlue);
                graphic.FillRectangle(solidBrush, 0, 0, SignaturePanel.Width, SignaturePanel.Height);
            }

        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            signature = new Signature();
            ClearSignaturePanel();
        }

        private void btn_import_Click(object sender, EventArgs e)
        {
            DeserializeSignature();
            ClearSignaturePanel();
            DrawSignature();
        }

        private void btn_export_Click(object sender, EventArgs e)
        {
            SerializeSignature();
        }

        private void DrawSignature()
        {
            foreach (Glyph glyph in signature.Glyphs)
            {
                foreach (Line line in glyph.Lines)
                {
                    DrawLine(line);
                }
            }

        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn tạo chữ ký trên winform Signature Pad
Đăng bởi: Thảo Meo - Lượt xem: 4967 20:43:37, 10/05/2022DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.