Click here to Skip to main content
Click here to Skip to main content

Metro Paint

By , 19 Mar 2013
 

Introduction

This Metro style application is for drawing basic shapes like line, rectangle, circle, ellipse, free hand shapes, eraser, and clear screen. It also provides a feature to choose stroke thickness, border color, fill color, and handwriting recognition. It allows to save the recognized text as a text file and only ink strokes can be saved as image. It is in the initial version so there are some limitations like saving a whole drawing as image, resizing and moving of the drawn components, etc. 

Using the Code

Basically, this app uses the Windows.UI.Xaml.Shapes namespace and pointer events as there are no mouse events. Here all the drawing features are done with the PointerMoved, PointerReleased, and PointerPressed events. All the drawing is done onto the canvas.

For selection of drawing tools I have used switch case. The tools are basically buttons and the icons used are just "Segoe UI Symbol" fonts. I have used a character map for getting the desired icons.

For drawing the components in the PointerPressed event the current co-ordinates are saved and then after the PointerMoved event, the second co-ordinates are saved. These co-ordinates can be directly used for line drawing but for another shape, I am getting the height and width by difference of the co-ordinates. The free hand drawing tool just adds the points coming in the path during the PointerPressed event. The metro app can't be closed programmatically so the close button will suspend the app and after some time the Task Manager will kill the app to free the resources.

The color combo box is filled programmatically. The code snippet is given below: 

var colors = typeof(Colors).GetTypeInfo().DeclaredProperties;
foreach (var item in colors)
{
    cbBorderColor.Items.Add(item);
    cbFillColor.Items.Add(item);
}

Since in WinRT there is no rendering method, it is not possible to save the drawn object as an image. Moreover, AdornerDecorator is also not available so component resizing and moving are not possible. I hope Microsoft will update WinRT with the missing features.

For handwriting recognition, I have used the Windows.UI.Input.Inking namespace. The basic concept is to use the InkManager class. It provides properties and methods to manage the input, manipulation, and processing (including handwriting recognition) of one or more InkStroke objects. When a user write something onto canvas, all the points covered in that path is saved as InkManager objects. The InkManager class provide an async method called RecognizeAsync(). The results of the recognition is a collection of InkRecognitionResult objects. To get the text components, I have used the GetTextCandidates() method of the InkRecognitionResult class and it retrieves the collection of strings identified as potential matches for handwriting recognition. The code snippet is given below: 

private async void btnRecognize_Click(object sender, RoutedEventArgs e)
{
    try
    {
        txtRecognizedText.Visibility = Windows.UI.Xaml.Visibility.Visible;
        btnSaveRecognizedText.Visibility = Windows.UI.Xaml.Visibility.Visible;
        canvas.SetValue(Grid.RowProperty, 3);
        canvas.SetValue(Grid.RowSpanProperty, 1);
        MyInkManager.Mode = InkManipulationMode.Inking;
        x = await MyInkManager.RecognizeAsync(InkRecognitionTarget.Recent);
        MyInkManager.UpdateRecognitionResults(x);
        foreach (InkRecognitionResult i in x)
        {
            RecognizedText = i.GetTextCandidates();
            FinalRecognizedText += " " + RecognizedText[0];
            txtRecognizedText.Text += FinalRecognizedText;
        }
        FinalRecognizedText = string.Empty;

    }
    catch (Exception)
    {
        if (canvas.Children.Count == 0)
        {
            var MsgDlg = new MessageDialog("Your screen has no handwriting. " + 
              "Please write something with pencil tool then click recognize.", 
              "Error while recognizing");
            MsgDlg.ShowAsync();
        }
        else
        {
            var MsgDlg = new MessageDialog("Please clear the screen then write " + 
              "something with pencil tool", "Error while recognizing");
            MsgDlg.ShowAsync();
        }
    }
}

Points of Interest

This application demonstrates how a user can draw shapes and how hand writing recognition is performed in a XAML/C# Metro style app. I request other developers to enhance my application with their comments and suggestions. I am very thankful to CodeProject, StackOverflow, and MSDN Forum for solving my problems. 

Copy-Paste apps from here in Windows store 

One day I was searching for a better paint app, and I found these two apps which are fully copy pasted from this article. I suggest Microsoft to not accept these kinds of spam apps. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Farhan Ghumra
Software Developer Simform Solutions Pvt. Ltd.
India India
Member
Windows Metro Store Apps Developer
 
Follow me on: My Blog on Windows 8 | Twitter | Facebook | LinkedIn
 
Check Out My Articles & Tips

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionNullReferenceException while using multi-touchmemberMember 993852224 Mar '13 - 13:05 
Very nice article! I am developing an app where I need some basic painting functionality and this article will be very helpful!
I've encountered one problem though. Since you are using pointerPressed/Released/Moved events, the app doesn't support multi-touch. If the user is using the Ellipse, Rectangle or Line tool and presses more than one point at a time and then releases them, the app will crash with a NullReferenceException. The exception points at different parts of the code where NewRectangle/Ellipse/Line is used. The reason for that is that you set NewRectangle/Ellipse/Line to null in your onRelease method.
AnswerRe: NullReferenceException while using multi-touchmemberFarhan Ghumra24 Mar '13 - 20:00 
I don't know how to handle multitouch, I recommend you to refer MSDN forum[^] or Stack Overflow [^]
Windows Metro Store Apps Developer
 
Follow me on: My Blog on Windows 8 | Twitter | Facebook | LinkedIn
 
Check Out My Articles On CodeProject

QuestionCanvasmemberKhaled Jemni10 Mar '13 - 1:24 
Awesome job Smile | :) I really like the app , it helped me a lot because I'm developing a similar Windows 8 app . I'm talking as a Windows Store apps developer . I have a simple question : What if I want to add a small graph on the canvas , like something will be drawn and the user must follow it , is it possible ? thanks for answering and thanks for the code Smile | :)
AnswerRe: CanvasmemberFarhan Ghumra10 Mar '13 - 20:54 
Hi Khaled, if you want to draw graph/chart I suggest you to use Telerik RadControls[^] (paid) or WinRT XAML Toolkit[^] (free)
Windows Metro Store Apps Developer
 
Follow me on: My Blog on Windows 8 | Twitter | Facebook | LinkedIn
 
Check Out My Articles On CodeProject

GeneralMy vote of 5mvpVietdungiitb19 Feb '13 - 22:33 
excellent, Thanks.
GeneralRe: My vote of 5memberFarhan Ghumra20 Feb '13 - 2:39 
You're welcome Smile | :)
Windows Metro Store Apps Developer
 
Follow me on: My Blog on Windows 8 | Twitter | Facebook | LinkedIn
 
Check Out My Articles On CodeProject

GeneralNice articlememberSimon Jackson17 Dec '12 - 3:30 
Nice clean article for surface painting with WinRT
 
Real shame the marketplace team didn't pick up on those bogies but you can at lease report them from your developer dashboard and point to your article for reference. They are usually quite hot provided you have a case.
GeneralRe: Nice articlememberFarhan Ghumra17 Dec '12 - 18:58 
Thanks Simon for your comments. I have reported already about that thing.
Windows Metro Store App Developer
 
Follow me on: My Blog on Windows 8 | Twitter | Facebook
 
Check Out My Windows 8 App : Metro Paint | Bing Map Route Navigation

GeneralMy vote of 5memberMihai MOGA14 Dec '12 - 5:45 
This is a great inspiring article. I am pretty much pleased with your good work. You put really very helpful information. Keep it up once again.
GeneralRe: My vote of 5memberFarhan Ghumra14 Dec '12 - 6:39 
Thanks for your vote and reply Smile | :)
Windows Metro Store App Developer
 
Follow me on: My Blog on Windows 8 | Twitter | Facebook
 
Check Out My Windows 8 App : Metro Paint | Bing Map Route Navigation

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 19 Mar 2013
Article Copyright 2012 by Farhan Ghumra
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid