"How To" Series: Building a Signature Control in Compact Framework
It is not a rare requirement for a mobile application to provide functionality for capturing a person signature.
So, let's see if we can provide this feature into a mobile application with .NET Compact Framework.
The image bellow outlines the problem that we want to solve:
We have a mobile salesman, who collects customer orders. Our salesman should collect the customer's signatures along with the order's details. Our salesman is carrying a Pocket PC device. We should create
a software solution to allow customer's signature capturing and transmition of the signature over the wire to the office. There we have a Sql Server database to store the signatures into and signatures consuming app. , which may process the stored signatures.
We should solve the following tasks:
- Capturing a signature from a person from the mobile device
- Saving the captured signature locally on the mobile device
- Transmitting the saved signature over the wire and storing it on the server side
- Consuming the transmitted and saved signature on the server side
Let's try to solve these tasks:
Solving Task 1: Capturing a signature from a person on the mobile device
If a person should place its signature on a Pocket PC, he will probably use the stylus to write his signature on the Pocket PC screen. All we have to do is to capture the on-screen tapings and convert them into a bitmap. We will create a User Control to encapsulate this functionality. The main functionality is divided between the following UserControl event handlers:
- OnMouseDown - here we will "remember" the coordinates of the last on-screen tapping
- OnMouseMove - here we will draw a line from the remembered coordinates to the new(after the mouse move) on-screen mouse coordinates.
Following code example(simplified version)
...
private Bitmap SignatureImage;
private Graphics GraphicsHandle;
privte Point MouseCoords;
....
SignatureImage = new Bitmap(this.Width, this.Height);
GraphicsHandle = Graphics.FromImage(SignatureImage);
...
//remembering the coords of the last on-screen tapping into MouseCoords member
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
MouseCoords.X = e.X;
MouseCoords.Y = e.Y;
}
//draw a line from the remembered coords to the new on-screen tapping coords
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
GraphicsHandle.DrawLine(SignaturePen, MouseCoords.X, MouseCoords.Y, e.X, e.Y);
MouseCoords.X = e.X;
MouseCoords.Y = e.Y;
}
//saves the captured bitmap image into a stream
public void Save(Stream stream)
{
SignatureImage.Save(stream, ImageFormat.Bmp);
}
Solving Task 2: Saving the captured signature locally on the mobile device
This task is simpler. Let's pretend, we should save the captured image into a local SqlServer Mobile database.
We should have a table containing a field of type image to save the signature into it.
CRATE TABLE CUSTOMERSIGN
{
customerNo:int not null identity
sign:image
}
Following the code to save the captured signature:
//our signature control placed into a form
private SignatureControl MySignatureControl;
...
string connectionString = ...;
...
using (SqlCeConnection connection = new SqlCeConnection(connectionString))
{
connection.Open();
SqlCeCommand command = new SqlCeCommand("INSERT INTO CUSTOMERSIGN(sign)VALUES(?)", connection);
//getting the captured signature as stream
MemoryStream signStream = new MemoryStream();
MySignatureControl.Save(signStream);
param = new SqlCeParameter("sign", SqlDbType.Binary);
param.Value = signStream.ToArray();
command.Parameters.Add(param);
command.ExecuteNonQuery();
connection.Close();
}
Now we have the signature saved locally...
We may save the signature into another storage type like a file for example.
See the whole article here: http://xman892.blogspot.com/2006/12/how-to-series-building-signature.html