触摸屏幕签字,签批屏对接可不使用提供的SDK

一个偶然机会得知,触摸屏可替代签批屏,其原理就是通过GraphicsPath来记录触摸过的坐标点,再通过Paint事件实时绘制到屏幕上,下面对类进行了封装,封装类似汉王签批屏的模式,主要流程如下:

1、初始化

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="_signname"></param>
        /// <param name="_panel"></param>
        public void Init(string _signname, Panel _panel)
        {
            try
            {
                SignBox.Parent = _panel;
                SignBox.Left = 0;
                SignBox.Top = 0;
                SignBox.Width = _panel.Width;
                SignBox.Height = _panel.Height;
                SignBox.Paint += SignBox_Paint;
                SignBox.MouseDown += SignBox_MouseDown;
                SignBox.MouseMove += SignBox_MouseMove;
                SignName = _signname;
            }
            catch (Exception ex)
            {
                LogHelper.Debug(ex);
            }
        }

2、记录绘制轨迹

        private void SignBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                try
                {
                    mousePath.AddLine(e.X, e.Y, e.X, e.Y);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            SignBox.Invalidate();
        }

        private void SignBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                mousePath.StartFigure();
            }
        }

3、绘制

        private void SignBox_Paint(object sender, PaintEventArgs e)
        {
            try
            {
                myUserColor = System.Drawing.Color.Black;
                myAlpha = 255;
                Pen CurrentPen = new Pen(Color.FromArgb(myAlpha, myUserColor), myPenWidth);
                e.Graphics.DrawPath(CurrentPen, mousePath);
            }
            catch (Exception ex)
            {
                LogHelper.Debug(ex);
            }
        }

4、保存签字信息

        /// <summary>
        /// 签字
        /// </summary>
        /// <returns></returns>
        public bool Sign()
        {
            bool Ret = false;
            try
            {
                if (mousePath.PointCount > 0)
                {
                    using (SavedBitmap = new Bitmap(SignBox.Width, SignBox.Height))
                    {
                        SignBox.DrawToBitmap(SavedBitmap, new Rectangle(0, 0, SignBox.Width, SignBox.Height));
                        SavedBitmap.Save(SignName);
                        Ret = true;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Debug(ex);
            }
            return Ret;
        }

5、重签

        /// <summary>
        /// 清除
        /// </summary>
        public void Clear()
        {
            try
            {
                using (Graphics gra = SignBox.CreateGraphics())
                {
                    gra.Clear(Color.White);
                }
                mousePath.Reset();
            }
            catch (Exception ex)
            {
                LogHelper.Debug(ex);
            }
        }

6、释放

        /// <summary>
        /// 释放
        /// </summary>
        public void Uinit()
        {
            try
            {
                if (SavedBitmap != null) SavedBitmap.Dispose();
                if (mousePath != null) mousePath.Dispose();
            }
            catch (Exception ex)
            {
                LogHelper.Debug(ex);
            }
        }

效果如图

触摸屏幕签字,签批屏对接可不使用提供的SDK

上一篇:【每日一题】【DFS】2022年1月5日-543. 二叉树的直径


下一篇:108. 将有序数组转换为二叉搜索树