基本信息
源码名称:C# 电子签名及保存显示 实例源码下载
源码大小:0.06M
文件格式:.zip
开发语言:C#
更新时间:2017-02-01
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
一款基于C#开发的电子签名程序,主要实现了签名的保存,适合初学者使用,可将完成的签名保存到指定位置,可修改对应的画笔颜色、宽度、透明度等


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ElectronicSignature
{
    public partial class Frm_Main : Form
    {
        //記錄直線或者曲線的物件
        private System.Drawing.Drawing2D.GraphicsPath mousePath = new System.Drawing.Drawing2D.GraphicsPath();
        //畫筆透明度
        private int myAlpha = 100;
        //畫筆顏色對象
        private Color myUserColor = new Color();
        //畫筆寬度
        private int myPenWidth = 3;
        //簽名的圖片物件
        public Bitmap SavedBitmap;

        public Frm_Main()
        {
            InitializeComponent();
        }        

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void pictureBox1_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);
                }
            }
            pictureBox1.Invalidate();
        }

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

        #region 參數設置
        public void set()
        {
            //畫筆寬度
            myPenWidth = Convert.ToInt32(cbx_HBKD.Text.ToString());
            switch (cbx_HBYS.Text)
            {
                case "藍色":
                    myUserColor = System.Drawing.Color.Blue;
                    break;
                case "紅色":
                    myUserColor = System.Drawing.Color.Red;
                    break;
                case "黃色":
                    myUserColor = System.Drawing.Color.Yellow;
                    break;
            }
            myAlpha = Convert.ToInt32(cbx_TMD.Text);
        }
        #endregion

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            try
            {
                set();//設置畫筆的顏色、寬度、透明度
                Pen CurrentPen = new Pen(Color.FromArgb(myAlpha, myUserColor), myPenWidth);
                e.Graphics.DrawPath(CurrentPen, mousePath);
            }
            catch { }
        }

        private void btn_Clear_Click(object sender, EventArgs e)
        {
            pictureBox1.CreateGraphics().Clear(Color.White);
            mousePath.Reset();
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            bool isSave = true;
            SavedBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            pictureBox1.DrawToBitmap(SavedBitmap, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));

            #region 儲存圖片
            SaveFileDialog saveImageDialog = new SaveFileDialog();
            saveImageDialog.Title = "圖片儲存";
            saveImageDialog.Filter = @"jpeg|*.jpg|bmp|*.bmp|gif|*.gif";
            if (saveImageDialog.ShowDialog() == DialogResult.OK)
            {
                string fileName = saveImageDialog.FileName.ToString();
                if (fileName != "" && fileName != null)
                {
                    string fileExtName = fileName.Substring(fileName.LastIndexOf(".") 1).ToString();
                    System.Drawing.Imaging.ImageFormat imgformat = null;
                    //預設儲存為JPG格式   
                    if (imgformat == null)
                    {
                        imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
                    }
                    if (isSave)
                    {
                        try
                        {
                            SavedBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp);
                            SavedBitmap.Dispose();
                            MessageBox.Show("圖片已經成功儲存!");
                        }
                        catch
                        {
                            MessageBox.Show("儲存失敗,你還沒有截取過圖片或已經清空圖片!");
                        }
                    }
                }
            }
            #endregion
        }

        public Bitmap SaveImage(PictureBox pictureBox1)
        {
            Bitmap SavedBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            pictureBox1.DrawToBitmap(SavedBitmap, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
            return SavedBitmap;
        }
    }
}