Save WPF Ink Strokes To a Database

by jason20. December 2008 20:23

In my previous post about creating a bitmap from an InkCanvas in WPF, I mentioned that you could easily save the ink strokes for later. I wanted to post a code example for writing those strokes to a database and de-serialized them back into memory.

The first step in saving to a database is to get an array of bytes from the strokes in the InkCanvas. To do this, the StrokeCollection provides a convenient Save method:

byte[] signature;
using (MemoryStream ms = new MemoryStream())
{
icSignature.Strokes.Save(ms);
signature = ms.ToArray();
}
string sql = "INSERT INTO tblMyTable (mySigCol) VALUES (@Sig)";
SqlCommand comm = new SqlCommand(sql, conn);
comm.Parameters.AddWithValue("@Sig", signature);
comm.ExecuteNonQuery();

You can just as easily load the contents back from the database by creating a new stroke collection from the bytes save in the database:

string sql = "SELECT TOP 1 mySigCol FROM tblMyTable";
SqlCommand comm = new SqlCommand(sql, conn);
byte[] signature = (byte[])comm.ExecuteScalar();
using (MemoryStream ms = new MemoryStream(signature))
{
icSignature.Strokes = new System.Windows.Ink.StrokeCollection(ms);
ms.Close();
}

If you need to persist a signature or annotation from an InkCanvas to a database and back, the process is very simple and makes the InkCanvas even more useful.

Tags: , , ,

Development | WPF

Convert WPF InkCanvas to Bitmap

by jason9. December 2008 14:38

We live in a digital age and if you are like me, paper is only used when it is absolutely unavoidable. If you have a tablet PC, signing your name to attach it to a document may be one less thing you will need a physical sheet of paper for.

The WPF ink control can be put into your XAML to collect stylus strokes as a collection of geometry points. Of course, as the stylus moves, the movements are also shown on screen.

inktest

More...

Tags: , , ,

Development | WPF

About

Jason Williams is a .NET developer in Lincoln, Nebraska.

The name "Centrolutions" came out of a long search for a domain name. The goal was to create a name that conveyed an ideology of writing software centered (Centr--) on a solution (--olutions) for a particular problem. In other words, it was the only name in a long list that wasn't already registered on the internet.

If you're looking for the products I have for sale, you should go here.