📄 signaturecapture.cs
字号:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace SignatureCapture
{
public partial class SignatureCapture : UserControl
{
#region Fields
private Graphics _signatureSurface;
private Bitmap _bitmap;
private bool _stylusCaptured;
private int _oldX, _oldY;
private bool _signatureCaptured = false;
#endregion
#region Properties
public bool SignatureCaptured
{
get
{
return _signatureCaptured;
}
}
#endregion
#region Constructors
public SignatureCapture()
{
InitializeComponent();
//Prepare initial state of the signature drawing surface
InitializeGraphics();
}
#endregion
#region UserControl Methods
/// <summary>
/// As the control gets resized by the user, it must repaint itself
/// </summary>
/// <param name="e"></param>
protected override void OnResize(EventArgs e)
{
//Prepare initial state of the signature drawing surface
InitializeGraphics();
base.OnResize(e);
}
/// <summary>
/// The signature get drawn every time the form repaints itself
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
if (this._bitmap != null)
{
//Draw image whenever the form repaints itself
e.Graphics.DrawImage(this._bitmap, 0, 0);
}
base.OnPaint(e);
}
/// <summary>
/// This eliminates flicker
/// </summary>
/// <param name="e"></param>
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
}
/// <summary>
/// This fires when the user taps their stylus on the control
/// </summary>
/// <param name="e"></param>
protected override void OnMouseDown(MouseEventArgs e)
{
// Set stylus capture
this._stylusCaptured = true;
//Retrieve the old coordinates
this._oldX = e.X;
this._oldY = e.Y;
base.OnMouseDown(e);
}
/// <summary>
/// This fires when the user lifts their stylus from the control
/// </summary>
/// <param name="e"></param>
protected override void OnMouseUp(MouseEventArgs e)
{
// Release stylus capture
this._stylusCaptured = false;
base.OnMouseUp(e);
}
/// <summary>
/// This fires as the user draws their signature on the control
/// </summary>
/// <param name="e"></param>
protected override void OnMouseMove(MouseEventArgs e)
{
int _x, _y;
if (this._stylusCaptured)
{
// mark the fact that you have some signature
_signatureCaptured = true;
// Retrieve coordinates
_x = e.X;
_y = e.Y;
if (this._signatureSurface != null)
{
//Create a black pen with a thickness of 2
Pen _pen = new Pen(Color.Black, 2);
// Draw line from the old coordinates to the new coordinates
this._signatureSurface.DrawLine(_pen, this._oldX, this._oldY, _x, _y);
}
// Update the signature surface
this.Invalidate(new Rectangle(Math.Min(_x, this._oldX),
Math.Min(_y, this._oldY),
Math.Abs(this._oldX - _x) + 1,
Math.Abs(this._oldY - _y) + 1));
//Retrieve the old coordinates
this._oldX = _x;
this._oldY = _y;
}
base.OnMouseMove(e);
}
#endregion
#region Methods
/// <summary>
/// Prepares the initial or resized state of
/// the signature drawing surface
/// </summary>
private void InitializeGraphics()
{
this._stylusCaptured = false;
// Create offscreen drawing surface
this._bitmap = new Bitmap(this.Size.Width, this.Size.Height);
if (this._bitmap != null)
{
//Create a graphics object from the bitmap
this._signatureSurface = Graphics.FromImage(this._bitmap);
if (this._signatureSurface != null)
{
//Blank out the signature surface by making it all white
this._signatureSurface.FillRectangle(new SolidBrush(Color.White),
0,
0,
this.Size.Width,
this.Size.Height);
}
}
}
/// <summary>
/// Makes the signature surface blank
/// </summary>
public void Clear()
{
_signatureCaptured = false;
if (this._signatureSurface != null)
{
//Blank out the signature surface by making it all white
this._signatureSurface.FillRectangle(new SolidBrush(System.Drawing.Color.White),
0,
0,
this.Size.Width,
this.Size.Height);
this.Invalidate();
}
}
/// <summary>
/// Saves signature as a Gif file to the path provided
/// </summary>
/// <param name="filePath">filename to save the image</param>
public void Save(string filePath, ImageFormat imageFormat)
{
if (this._bitmap != null && !string.IsNullOrEmpty(filePath))
{
//Save the bitmap as a Gif file
this._bitmap.Save(@filePath, imageFormat);
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -