Blue Theme Orange Theme Green Theme Red Theme
 
Discover the top 5 tips for understanding .NET Interop
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
Search :       Advanced Search »
Home » XAML » Digital Ink in WPF

Digital Ink in WPF

Digital ink used to only be available as part of the Tablet PC SDK. Now it is bundled with Windows Presentation Foundation. This tutorial demonstrates how easy it is to integrate digital ink into your Avalon applications using XAML and C#.

Page Views : 8977
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Originally available only as part of the Tablet PC SDK, digital ink is now integrated into Microsoft's Windows Presentation Foundation.  With the exception of handwriting recognition, Windows programmers can now leverage digital ink in their applications without additional SDKs or runtime libraries.

Enabling Ink

 

To enable the collection and display of ink input, developers need only include the InkCanvas element in their XAML document.

 

<Window x:Class="WindowsApplication1.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="WindowsApplication1" Width="550" Height="400">

<Grid>

                    <InkCanvas />

</Grid>

</Window>

When the above code is compiled, a small black square replaces the mouse cursor when the user hovers over the application window. When the user left-clicks and drags the mouse in the content area, "ink" is invoked and the user is able to draw. 

 

ink1.gif 

Like other tags in XAML, InkCanvas can be used in conjunction with other layout elements and controls. As such, it may be positioned or layered over other elements. The following example displays an image within an InkCanvas.

 

<Window x:Class="WindowsApplication1.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="WindowsApplication1" Width="550" Height="400">

          <Grid>

                   <InkCanvas>

                             <Image Source="http://www.google.com/intl/en/images/logo.gif" />

                   </InkCanvas>

          </Grid>

</Window> 

When compiled, the user is able to draw on the image because the graphic is a child of the InkCanvas element.

 

ink2.gif 


 

Controlling Stroke Appearance

 

Using just a bit of C#, it is also possible to control a number of ink properties including stroke color and thickness.  These properties are controlled using the DrawingAttributes class.

 

Using this class, you can either control the appearance of individual strokes using Stroke.DrawingAttributes or the default appearance of any new stroke collected by the InkCanvas using InkCanvas.DefaultDrawingAttributes.

 

Below is a list of properties supported by the DrawingAttributes class:

 

Name

Description

Color

Gets or sets the color of a Stroke.

FitToCurve

Gets or sets a value that indicates whether Bezier smoothing is used to render the Stroke.

Height

Gets or sets the height of the stylus used to draw the Stroke.

IgnorePressure

Gets or sets a value that indicates whether the thickness of a rendered Stroke changes according the amount of pressure applied.

IsHighlighter

Gets or sets a value that indicates whether the Stroke looks like a highlighter.

StylusTip

Gets or sets the shape of the stylus used to draw the Stroke.

StylusTipTransform

Gets or sets the Matrix that specifies the transformation to perform on the stylus' tip.

   Member names and descriptions taken from Microsoft's WinFX SDK

 

Using a few of the above properties, we can create a simple method for setting the default ink color to blue with a stroke thickness of 2px (2px by 2px).

 

private void setInkBlue(object sender, RoutedEventArgs e)

  {

      // set the DefaultDrawingAttributes for a blue pen.

      myInkCanvas.DefaultDrawingAttributes.Color = Colors.Blue;

      myInkCanvas.DefaultDrawingAttributes.Height = 2;

      myInkCanvas.DefaultDrawingAttributes.Width = 2;

  }

 

 

Editing with InkCanvas

 

InkCanvas also comes with a few "freebies".  One of these features is a built-in editing mode, allowing users to select, drag, resize, and erase ink strokes. 

 

There are two main properties available to control editing: InkCanvas.EditingMode and InkCanvas.EditingModeInverted.  The first property controls the tip of the stylus.  The second controls the "eraser".  Both of these properties use InkCanvasEditingMode to specify how stylus input is handled.

 

By default, the EditingMode of InkCanvas is set to Ink while EditingModeInverted is set to EraseByStroke.

However, InkCanvasEditingMode supports a number of settings: 

Name

Description

EraseByPoint

Indicates that the pen erases part of a stroke when the pen intersects the stroke. .

EraseByStroke

Indicates that the pen erases an entire stroke when the pen intersects the stroke. 

GestureOnly

Indicates that the InkCanvas responds to gestures, and does not receive ink. 

Ink

Indicates that ink appears on the InkCanvas when the pen sends data to it. 

InkAndGesture

Indicates that the InkCanvas responds to gestures, and receives ink. 

None

Indicates that no action is taken when the pen sends data to the InkCanvas

Select

Indicates that the pen selects strokes and elements on the InkCanvas.  

   Names and descriptions taken from Microsoft's WinFX SDK

 

 Specifying either Select or EraseByStroke will override this setting providing alternate functionality. The following method sets the editing mode to select:

 

// selection

private void Select(object sender, RoutedEventArgs e)

{

   // set the EditingMode to Select

   myInkCanvas.EditingMode = InkCanvasEditingMode.Select;

}

 

 When this method is called, the user can select and drag a stroke or group of strokes by either directly clicking on the stroke or
"lassoing" a group of strokes.

 ink3.gif

 

 

A similar method can be created to set the editing mode to erase:

 

// erase

private void Erase(object sender, RoutedEventArgs e)

{

    // set the EditingMode to EraseByStroke

    myInkCanvas.EditingMode = InkCanvasEditingMode.EraseByStroke;

}

 

When this method is called, the user can erase ink on the canvas by holding down the left mouse button and moving the cursor (an eraser in this mode) over any stroke.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Doug Cook
Doug is a user interface developer in Austin, TX. Currently, he is working with Windows Vista and WinFX (.NET 3.0) and is an active participant in a number of Microsoft beta programs.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
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.
Dynamic PDF
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.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.