Drawing And Inking Using New InkCanvas Control For Universal Windows App

Windows 10 introduced built-in InkCanvas control to capture the user input. I am going to show the use of InkCanvas control to capture the user touch input and save it as image.

In Windows 8.1 apps you had to create a Canvas, listen to input events, and render your own strokes, but in Windows 10 use the built-in InkCanvas control to immediately capture use input.

This control allows user to quickly enable inking for user and easily expand its functionality by accessing the InkCanvas’s InkPresenter property. InkPresenter can be used to configure a collection of user input through touch or mouse input.

Let’s see the steps to perform this operation.

Create a new windows 10 UWP ap. Once successful created, go to MainPage.XAML page and write the following code to design your page.

  1. <InkCanvas x:Name="myCanvas" PointerPressed="myCanvas_PointerPressed" PointerMoved="myCanvas_PointerMoved" PointerReleased="myCanvas_PointerReleased" Margin="0,0,10,10">  
  2.   </InkCanvas>  
The control looks like the following, 

control

Next got to code behind page and write the following code,

Firstly, create new instance for InkPresenter.
  1. InkPresenter myPresenter = myCanvas.InkPresenter;  
Next define the input device type for Canvas control to let the control accepts inputs from those devices.

Pen, Mouse and Touch.
  1. myPresenter.InputDeviceTypes = Windows.UI.Core.CoreInputDeviceTypes.Pen 
  2. |Windows.UI.Core.CoreInputDeviceTypes.Mouse|Windows.UI.Core.CoreInputDeviceTypes.Touch;  
Set the pen colour, pencil tip shape and size.
  1. myAttributes.Color = Windows.UI.Colors.Crimson;  
  2. myAttributes.PenTip = PenTipShape.Circle;  
  3. myAttributes.Size = new Size(2, 5);  
The whole code on canvas pointer pressed event looks like the following,
  1. private void myCanvas_PointerPressed(object sender, PointerRoutedEventArgs e)  
  2. {  
  3.     InkPresenter inkPresenter = myCanvas.InkPresenter;  
  4.     inkPresenter.InputDeviceTypes = Windows.UI.Core.CoreInputDeviceTypes.Pen | Windows.UI.Core.CoreInputDeviceTypes.Mouse | Windows.UI.Core.CoreInputDeviceTypes.Touch;  
  5.     InkDrawingAttributes myAttributes = inkPresenter.CopyDefaultDrawingAttributes();  
  6.     myAttributes.Color = Windows.UI.Colors.Crimson;  
  7.     myAttributes.PenTip = PenTipShape.Circle;  
  8.     myAttributes.PenTipTransform = System.Numerics.Matrix3x2.CreateRotation((float) Math.PI / 4);  
  9.     myAttributes.Size = new Size(2, 5);  
  10.     inkPresenter.UpdateDefaultDrawingAttributes(myAttributes);  
  11. }  
Next save this ink into image,
  1. private async void savebtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     var savePicker = new FileSavePicker();  
  4.     savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;  
  5.     savePicker.FileTypeChoices.Add("PNG"new System.Collections.Generic.List < string >  
  6.     {  
  7.         ".png"  
  8.     });  
  9.     StorageFile file = await savePicker.PickSaveFileAsync();  
  10.     if (null != file)  
  11.     {  
  12.         try  
  13.         {  
  14.             using(IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))  
  15.             {  
  16.                 await myCanvas.InkPresenter.StrokeContainer.SaveAsync(stream);  
  17.             }  
  18.         }  
  19.         catch (Exception ex)  
  20.         {}  
  21.     }  
  22. }  
Now change the inkpresenter processing mode to erase to delete the ink,
  1. myCanvas.InkPresenter.InputProcessingConfiguration.Mode = InkInputProcessingMode.Erasing;  
Erase button click event looks like the following,
  1. private void erasebtn_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     myCanvas.InkPresenter.InputProcessingConfiguration.Mode = InkInputProcessingMode.Erasing;  
  4. }  
Now run the app and see the output like the following screen,

output

output

For Source Code

 


Similar Articles