Thursday, May 28, 2009

Signature panel

We can create a signature panel using Visual Studio and MouseMove, MouseDown and MouseUp events of any control.
// Code ***********************
Bitmap bitimage;
Graphics graphSignature;

Point pnt;
Point prepnt;
Graphics gSignature;
bool mousedownclicked = false;
public Form1()
{
InitializeComponent();

bitimage = new Bitmap(this.Width, this.Height);
graphSignature = Graphics.FromImage(bitimage);
gSignature = this.CreateGraphics();
// create a white background
gSignature.Clear(Color.White);
graphSignature.Clear(Color.White);

}
private void MouseMoveForm(object sender, MouseEventArgs e){
gSignature.DrawLine(new Pen(Color.Red, 5.0F), prepnt, new Point(e.X, e.Y));// For display
graphSignature.DrawLine(new Pen(Color.Red, 5.0F), prepnt, new Point(e.X, e.Y));// For saving
prepnt.X = e.X;
prepnt.Y = e.Y;
}
private void MouseDownForm(object sender, MouseEventArgs e)
{
mousedownclicked = true;

pnt.X = e.X;
pnt.Y = e.Y;

prepnt.X = e.X;
prepnt.Y = e.Y;
}
private void MouseUpForm(object sender, MouseEventArgs e)
{
mousedownclicked = false;
}
//Code END ******************

-Abinaw

No comments:

Post a Comment