C# WinForm 繪制 手寫 簽名
C# WinForm 實現 手寫 簽名
C# WinForm 書寫
參考博客(此文章作者的實現有閃爍,卡頓的問題,本文解決了此問題):
https://www.cnblogs.com/wuhuacong/p/2791434.html
界面(運行):
界面(布局):
代碼:
public partial class FrmSign : FrmFormFlat { /// <summary> /// 圖像 /// </summary> public Bitmap bitmap; //記錄直線或者曲線 private GraphicsPath mousePath = new GraphicsPath(); //畫筆顏色 private Color myUserColor = Color.Navy; //畫筆寬度 private int myPenWidth = 2; public FrmSign() { InitializeComponent(); } public FrmSign(Image image) { InitializeComponent(); if (image != null) { panel1.BackgroundImage = image; panel1.Width = image.Width; panel1.Height = image.Height; } } private void panel1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right) { mousePath.AddLine(e.X, e.Y, e.X, e.Y); Graphics g = panel1.CreateGraphics(); g.DrawPath(new Pen(myUserColor, myPenWidth), mousePath); } } private void panel1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right) { mousePath.StartFigure(); } } private void panel1_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawPath(new Pen(myUserColor, myPenWidth), mousePath); } private void GetBitmap() { //bitmap = new Bitmap(panel1.Width, panel1.Height); //panel1.DrawToBitmap(bitmap, new Rectangle(0, 0, panel1.Width, panel1.Height)); Bitmap bit = new Bitmap(panel1.Width, panel1.Height); Graphics g = Graphics.FromImage(bit); g.CompositingQuality = CompositingQuality.HighQuality; g.CopyFromScreen(panel1.PointToScreen(Point.Empty), Point.Empty, new Size(panel1.Width, panel1.Height)); bitmap = bit; g.Dispose(); } //保存 private void buttonFlatBC_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.OK; GetBitmap(); this.Close(); } //選擇顏色 private void buttonFlat1_Click(object sender, EventArgs e) { if (colorDialog1.ShowDialog() == DialogResult.OK) { buttonFlat1.BackColor = colorDialog1.Color; myUserColor = colorDialog1.Color; } } //清除 private void buttonFlat2_Click(object sender, EventArgs e) { mousePath.Reset(); panel1.Invalidate(); } }
樣式代碼就不貼出了,因為大家的樣式可能都不一樣。
完成