Introduction
This article shows how to collect and display a signature. The signature is made up of a list of line segments. Each segment contains an array of points (x and y coordinates).
There are two *.cs files MainForm.cs and SignatureControl.cs. These two files are used in C# .NET compact Framework to capture the signature and save that in a *.txt file in PocketPC. You can load the same signature from the *.txt file.
/***********CLIENT CODE****************/
/*****************************************************************/
//SignatureControl.cs File
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
using System.IO;
namespace PocketSignature
{
/****************************************************************************
* Description: Collects and displays a signature. The signature is made
* up of a list of line segments. Each segment contains an array of points
* (x and y coordinates).
* Draws to a memory bitmap to prevent flickering.
* Raises the SignatureUpdate event when a new segment is added to
* the signature.
*
*
*
*/
public class SignatureControl : Control
{
private string background = null;
public string Background
{
get
{
return background;
}
set
{
background = value;
}
}
// GDI objects
Bitmap bmp;
//String bmpFile="\\Program Files\\SignCaptureV2\\sign here.png";
Graphics graphics;
Pen pen = new Pen(Color.Black);
// list of line segments
ArrayList pVector = new ArrayList();
Point lastPoint = new Point(0,0);
// if drawing signature or not
bool drawSign = false;
// notify parent that line segment was updated
public event EventHandler SignatureUpdate;
public SignatureControl(){}
protected override void OnPaint(PaintEventArgs e)
{
// we draw on the memory bitmap on mousemove so there
// is nothing else to draw at this time
CreateGdiObjects();
e.Graphics.DrawImage(bmp, 0, 0);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
// don't pass to base since we paint everything, avoid flashing
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
// process if currently drawing signature
if (!drawSign)
{
// start collecting points
drawSign = true;
// use current mouse click as the first point
lastPoint.X = e.X;
lastPoint.Y = e.Y;
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
// process if drawing signature
if (drawSign)
{
// stop collecting points
drawSign = false;
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
// process if drawing signature
if (drawSign)
{
// draw the new segment on the memory bitmap
graphics.DrawLine(pen, lastPoint.X, lastPoint.Y, e.X, e.Y);
pVector.Add(lastPoint.X+" "+lastPoint.Y+" "+e.X+" "+e.Y);
// update the current position
lastPoint.X = e.X;
lastPoint.Y = e.Y;
// display the updated bitmap
Invalidate();
}
}
/// <summary>
/// Clear the signature.
/// </summary>
public void Clear()
{
pVector.Clear();
InitMemoryBitmap();
Invalidate();
}
/// <summary>
/// Create any GDI objects required to draw signature.
/// </summary>
private void CreateGdiObjects()
{
// only create if don't have one or the size changed
if (bmp == null || bmp.Width != this.Width ||
bmp.Height != this.Height)
{
// memory bitmap to draw on
InitMemoryBitmap();
}
}
/// <summary>
/// Create a memory bitmap that is used to draw the signature.
/// </summary>
private void InitMemoryBitmap()
{
// load the background image
if (this.Background==null)
{
bmp = new Bitmap(this.Width,this.Height);
graphics = Graphics.FromImage(bmp);
graphics.Clear(Color.White);
}
else
{
bmp = new Bitmap(this.Background);
// get graphics object now to make drawing during mousemove faster
graphics = Graphics.FromImage(bmp);
}
}
/// <summary>
/// Notify container that a line segment has been added.
/// </summary>
private void RaiseSignatureUpdateEvent()
{
if (this.SignatureUpdate != null)
SignatureUpdate(this, EventArgs.Empty);
}
private void CreateFile(String fileName,String fileContent)
{
if (File.Exists(fileName))
{
Console.WriteLine("{0} already exists.", fileName);
//return;
}
StreamWriter sr = File.CreateText(fileName);
sr.WriteLine (fileContent);
//MessageBox.Show("File creation complete");
sr.Close();
}
public void StoreSigData(String fileName)
{
string sigData = "";
for (int i = 0; ipvector.count// This commented operation is used to convert decimal to hex and store
//sigData = sigData + PadHex(int.Parse(xVector[i].ToString()))+" " +
// PadHex(int.Parse(yVector[i].ToString()))+ "\n";
sigData = sigData + pVector[i].ToString()+ "\n";
}
CreateFile(fileName,sigData);
}
// this method PadHex can be used to convert int to hex format //
// not in use
private string PadHex(int inNum )
{
uint uiDecimal = checked((uint)System.Convert.ToUInt32(inNum));
string s = String.Format("{0:x2}", uiDecimal);
return(s);
}
}
}
/*****************************************************************/
//MainForm.cs File
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Text;
//using Common;
using PocketSignature;
namespace SignCaptureV2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel areaSignature;
private System.Windows.Forms.Button butNew;
private System.Windows.Forms.Button Save;
private System.Windows.Forms.Button Load;
SignatureControl signature = new SignatureControl();
public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
// add signature control to form
signature.Location = areaSignature.Location;
signature.Size = areaSignature.Size;
signature.Background = "file://program/">\\Program Files\\SignCaptureV2\\sign here.png";
this.Controls.Add(signature);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.areaSignature = new System.Windows.Forms.Panel();
this.butNew = new System.Windows.Forms.Button();
this.Save = new System.Windows.Forms.Button();
this.Load = new System.Windows.Forms.Button();
//
// areaSignature
//
this.areaSignature.BackColor = System.Drawing.Color.Gainsboro;
this.areaSignature.Location = new System.Drawing.Point(8, 8);
this.areaSignature.Size = new System.Drawing.Size(224, 120);
this.areaSignature.Visible = false;
//
// butNew
//
this.butNew.Font = new System.Drawing.Font("Tahoma", 8.25F,
System.Drawing.FontStyle.Regular);
this.butNew.Location = new System.Drawing.Point(0, 232);
this.butNew.Size = new System.Drawing.Size(54, 24);
this.butNew.Text = "Clear";
this.butNew.Click += new System.EventHandler(this.butNew_Click);
//
// Save
//
this.Save.Font = new System.Drawing.Font("Tahoma", 8.25F,
System.Drawing.FontStyle.Regular);
this.Save.Location = new System.Drawing.Point(64, 232);
this.Save.Size = new System.Drawing.Size(72, 24);
this.Save.Text = "Save in File";
this.Save.Click += new System.EventHandler(this.Save_Click);
//
// Load
//
this.Load.Font = new System.Drawing.Font("Tahoma", 8.25F,
System.Drawing.FontStyle.Regular);
this.Load.Location = new System.Drawing.Point(144, 232);
this.Load.Size = new System.Drawing.Size(88, 24);
this.Load.Text = "Load from file";
this.Load.Click += new System.EventHandler(this.Load_Click);
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(258, 270);
this.Controls.Add(this.Load);
this.Controls.Add(this.Save);
this.Controls.Add(this.butNew);
this.Controls.Add(this.areaSignature);
this.Text = "PocketSignature";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(new MainForm());
}
private void butNew_Click(object sender, System.EventArgs e)
{
signature.Clear();
this.Refresh();
}
private void Save_Click(object sender, System.EventArgs e)
{
signature.StoreSigData("SignFile.txt");
}
private void Load_Click(object sender, System.EventArgs e)
{
int baseX = 10;
int baseY = 100;
string signatureFile = "SignFile.txt";
load_signature(baseX,baseY,signatureFile);
}
void load_signature(int baseX,int baseY,string signatureFile)
{
System.IO.StreamReader streamReader
= new System.IO.StreamReader("SignFile.txt");
string pointString = null;
while ((pointString = streamReader.ReadLine())!= null)
{
if(pointString.Trim().Length>0)
{
String[] points = new String[4];
points = pointString.Split(new Char[]{' '});
Pen pen = new Pen(Color.Black);
this.CreateGraphics().DrawLine(pen,
(baseX+int.Parse(points[0].ToString())),
(baseY+int.Parse(points[1].ToString())),
(baseX+int.Parse(points[2].ToString())),
(baseY+int.Parse(points[3].ToString())));
}
}
streamReader.Close();
}
}
}
/***********SERVER CODE****************/
pvector.count
0 Comments:
Post a Comment