I’ve just been porting a Windows Mobile (.Net CF) application over to the full Net Framework. I needed to provide a quick way of doing signature capture.
I was pointed by a friend (thanks Ross), to look at hosting the WPF InkCanvas control on my Windows Forms, form. I just dropped a Panel (Panel1) on my form and used the following code -
System.Windows.Controls.InkCanvas inkbox = null;
public frmpleasesign()
{
InitializeComponent();
inkbox = new System.Windows.Controls.InkCanvas();
inkbox.Name = "myinbox";
ElementHost elementHost = new ElementHost();
elementHost.Dock = DockStyle.None;
elementHost.Width = panel1.Width;
elementHost.Height = panel1.Height;
elementHost.Child = inkbox;
panel1.Controls.Add(elementHost);
}
To save the signature out to a byte array, that can be saved to my database I used, the following code -
public static byte[] SignatureToBitmapBytes(InkCanvas icSignature)
{
int margin = (int)icSignature.Margin.Left;
int width = (int)icSignature.ActualWidth - margin;
int height = (int)icSignature.ActualHeight - margin;
//render ink to bitmap
RenderTargetBitmap rtb =
new RenderTargetBitmap(width, height, 96d, 96d, PixelFormats.Default);
rtb.Render(icSignature);
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
byte[] bitmapBytes;
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
ms.Position = 0;
bitmapBytes = ms.ToArray();
}
return bitmapBytes;
}
Technorati Tags:
WPF,
Windows Mobile