You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
7.4 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Reflection;
namespace WinSign
{
public partial class Sign : UserControl
{
private bool _shouldSign = false;
private BackgroundWorker _bgWorker = new BackgroundWorker();
string _signPointsX = "";
string _signPointsY = "";
public Sign()
{
InitializeComponent();
this.BorderStyle = BorderStyle.FixedSingle;
_bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_bgWorker_RunWorkerCompleted);
_bgWorker.DoWork += new DoWorkEventHandler(_bgWorker_DoWork);
this.DoubleBuffered = true;
}
#region " Properties "
/// <summary>
/// The format to save the signature file
/// </summary>
private ImageFormat _signatureFileFormat = ImageFormat.Bmp;
public ImageFormat FileFormat
{
get { return _signatureFileFormat; }
set { _signatureFileFormat = value; }
}
/// <summary>
/// The format to save the signature file
/// </summary>
private Bitmap _signatureBitmap = null;
public Bitmap SignatureBitmap
{
get { return _signatureBitmap; }
}
/// <summary>
/// Color of the signature Default BLUE
/// </summary>
private Color _penColor = Color.Blue;
public Color PenColor
{
get { return _penColor; }
set { _penColor = value; }
}
/// <summary>
/// Width of the Signature Default 4
/// </summary>
private int _penWidth = 4;
public int PenWidth
{
get { return _penWidth; }
set { _penWidth = value; }
}
/// <summary>
/// Required points for valid sign
/// </summary>
private int _requiredPoints = 20;
public int RequiredPoints
{
get { return _requiredPoints; }
set { _requiredPoints = value; }
}
/// <summary>
/// Background Image (Bitmap) for sign
/// </summary>
private Bitmap _backgroundImageBitmap = null;
public Bitmap BackgroundImageBitmap
{
get { return _backgroundImageBitmap; }
set { _backgroundImageBitmap = value; }
}
/// <summary>
/// Informs if the sign was present
/// </summary>
/// <returns></returns>
public bool IsValid
{
get
{
return (_signPointsX.Replace("|","").Trim().Length >= _requiredPoints);
}
}
#endregion
void _bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
e.Result = GetSignImage();
}
void _bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.BackgroundImage = (e.Result as Bitmap);
this.Refresh();
}
private void AddtoX(string x)
{
_signPointsX = _signPointsX + x;
}
private void AddtoY(string y)
{
_signPointsY = _signPointsY + y;
}
private void Sign_MouseDown(object sender, MouseEventArgs e)
{
_shouldSign = true;
tmrRefresh.Start();
AddtoX(e.X.ToString() + ",");
AddtoY(e.Y.ToString() + ",");
}
private void Sign_MouseLeave(object sender, EventArgs e)
{
_shouldSign = false;
tmrRefresh.Stop();
AddtoX("|");
AddtoY("|");
}
private void Sign_MouseMove(object sender, MouseEventArgs e)
{
if (_shouldSign)
{
AddtoX(e.X.ToString() + ',');
AddtoY(e.Y.ToString() + ',');
}
}
private void GenerateSign()
{
if (!_bgWorker.IsBusy)
{
_bgWorker.RunWorkerAsync();
}
}
public void ClearSignature()
{
_signPointsX = "";
_signPointsY = "";
if (null != this.BackgroundImage)
{
this.BackgroundImage = null;
}
_signatureBitmap = null;
if (_backgroundImageBitmap != null)
{
this.BackgroundImage = _backgroundImageBitmap;
}
this.Refresh();
}
private void Sign_MouseUp(object sender, MouseEventArgs e)
{
_shouldSign = false;
tmrRefresh.Stop();
GenerateSign();
AddtoX("|");
AddtoY("|");
}
private void tmrRefresh_Tick(object sender, EventArgs e)
{
GenerateSign();
}
[Obfuscation(Feature = "virtualization", Exclude = false)]
private Bitmap GetSignImage()
{
string[] arrX = _signPointsX.Split('|');
string[] arrY = _signPointsY.Split('|');
int CurrX = 0;
int CurrY = 0;
Bitmap bmp = null;
if (_backgroundImageBitmap != null)
bmp = (Bitmap)_backgroundImageBitmap.Clone();
else
bmp = new Bitmap(this.Width, this.Height);
Graphics g = null;
try
{
g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
if (_backgroundImageBitmap == null)
{
if (this.FileFormat != ImageFormat.Png || ((this.BackColor != Color.White)))
{
g.FillRectangle(new SolidBrush(this.BackColor), 0, 0, bmp.Width, bmp.Height);
}
}
Pen pn = new Pen(new SolidBrush(this.PenColor),this.PenWidth);
for (int i = 0; i < arrX.Length; i++)
{
if (arrX[i].Length > 0)
{
string[] innerPointsX = arrX[i].Split(',');
string[] innerPointsY = arrY[i].Split(',');
PointF[] tfArray = new PointF[innerPointsX.Length - 1];
for (int j = 0; j < innerPointsX.Length - 1; j++)
{
if (innerPointsX[j].Length > 0)
{
CurrX = Convert.ToInt32(innerPointsX[j]);
CurrY = Convert.ToInt32(innerPointsY[j]);
PointF tf = new PointF(CurrX, CurrY);
tfArray[j] = tf;
}
}
GraphicsPath path = new GraphicsPath();
path.AddLines(tfArray);
g.DrawPath(pn, path);
}
}
}
catch (Exception ex)
{
string err = ex.Message.ToString();
this.ClearSignature();
}
finally
{
if (null != g)
g.Dispose();
}
_signatureBitmap = bmp;
return bmp;
}
}
}