SIGN UP MEMBER LOGIN:    
ARTICLE

Saving and Loading Ink on InkPicture Control in C#

Posted by Mahesh Chand Articles | Metro Style Apps in C# June 02, 2004
This article shows a real-world usage of Tablet PC InkPicutre control and ink. The attached file is a Visual Studio 2003 project, which allows you to load an image file, write on the image using ink and save the ink. It also allows you to load and view the saved ink on the image.
Reader Level:
Download Files:
 

This article shows a real-world usage of Tablet PC InkPicutre control and ink. The attached file is a Visual Studio 2003 project, which allows you to load an image file, write on the image using ink and save the ink. It also allows you to load and view the saved ink on the image.

The Application

The application is a simple Widnows Application with two Button controls, a CheckBox control, and an InkPicture control. ( See Figure 1).

 

Figure 1. InkPicture test application.

Loading Image

The Load Image button allows you to load in Image using the following code:

OpenFileDialog fileDlg = new OpenFileDialog();
fileDlg.Filter = "Image files (*.bmp; *.gif; *.jpg; *.png)|*.bmp;*.gif;*.jpg;*.png|" + "All files (*.*)|*.*";
if (fileDlg.ShowDialog() == DialogResult.OK)
{
try
{
inkPicture1.Image = Image.FromFile(fileDlg.FileName);
}
catch (Exception)
{
MessageBox.Show(this, "Unable to load the image file!", "Load Image Failed");
}
}

As you can see from this code, I simply set the file name selected from the Open File Dialog as Image property of InkPicture control, which views the image. After that I write something on InkPicuture using the pen. See Figure 2.

Figure 2. InkPicture control with an image.

Saving Image

The Save Ink button click saves the image. The following code saves the ink:

private void saveImageButton_Click(object sender, System.EventArgs e)
{
SaveFileDialog savedlg = new SaveFileDialog();
savedlg.Filter = "ISF files (*.isf)|*.isf| All files (*.*)|*.*";
if (savedlg.ShowDialog() == DialogResult.OK)
{
stream = savedlg.OpenFile();
if (stream != null)
{
if (inkPicture1.Image != null)
{
SaveInkImage(inkPicture1.Image, inkPicture1.Ink);
}
bytes = inkPicture1.Ink.Save();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
}
}
}
private void SaveInkImage(Image image, Ink imgInk)
{
memStream = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(memStream, image);
bytes = memStream.GetBuffer();
imgInk.ExtendedProperties.Add(guidImage, bytes);
}

The Save method also calls Save As dialog where you can specify the name of the ink file (.isf). See Figure 3.

Figure 3. Save Ink dialog.

Loading Ink

The following code is Show Ink Overlay CheckBox click which calls an Open File Dialog where you can select a saved ink and display it.  See Figure 4.

As you can see from this code, I create an Ink control and call its Load method, which takes a byte stream, which I create from the ink file. The GetInkImage method takes an Ink object and converts into an Image object. You can set the Image property of InkPicture control to display the image.

private void showInkCheckBox_CheckedChanged(object sender, System.EventArgs e)
{
if(showInkCheckBox.Checked)
{
OpenFileDialog opendlg = new OpenFileDialog();
opendlg.Filter = "ISF files (*.isf)|*.isf| All files (*.*)|*.*";
if (opendlg.ShowDialog() == DialogResult.OK)
{
stream = opendlg.OpenFile();
if (stream != null && stream.Length > 0)
{
bytes = new byte[stream.Length];
if (stream.Read(bytes, 0, bytes.Length) == stream.Length)
{
Ink ink = new Ink();
ink.Load(bytes);
inkPicture1.InkEnabled = false;
inkPicture1.Ink = ink;
inkPicture1.InkEnabled = true;
inkPicture1.Image = GetInkImage(ink);
inkPicture1.Invalidate();
}
stream.Close();
}
}
}
}
private Image GetInkImage(Ink imgInk)
{
if (imgInk.ExtendedProperties.Contains(guidImage))
{
bytes = (byte[])imgInk.ExtendedProperties[guidImage].Data;
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
BinaryFormatter formatter = new BinaryFormatter();
retImage = (Image)formatter.Deserialize(ms);
}
return retImage;
}
}

Figure 4. Loading an image.

See the attached project for more details.

Summary

In this article, I discussed the usage of Tablet PC ink and InkPicture control in real world applications. You can extend this application by allowing the users to select the color and width of the ink and select and delete the ink from the InkPicture control.

Login to add your contents and source code to this article
share this article :
post comment
 

hi sir ,the code given above is not working proprely can you please give me the code of how to save the pen tablet signature in the save bave button, i m using .net 2008 thanks..

Posted by rajendra koranga Feb 02, 2012

CAn not use picturebox1.Ink can not add Ink to the picturebox1

Posted by Prathiba Kaushalya May 22, 2011

Please Help me I want to save as a image(jpeg,bmp) after draw something in a picturebox using Ink i want to save as a image........!!

Posted by Prathiba Kaushalya May 22, 2011

Thanks Mahesh

Posted by Bhawna Singh Nov 08, 2010

Hi Bhawana,
This code is for .NET applications only and it will not work in MFC. I am not sure how to do it in MFC.

Posted by Mahesh Chand Nov 07, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor