Universal Windows Platform – Recognition App

Recognition App demonstrates how to use OcrEngine to recognise text within an Image that can be saved from the contents of the TextBox.

Step 1

If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.


Step 2

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…


Step 3

From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project

Step 4

Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10 Fall Creators Update (10.0; Build 16299) and the Minimum Version to be the same.


The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates

Step 5

Once done select from the Menu, Project, then Add New Item…


Step 6

From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project


Step 7

Once in the Code View for Library.cs the following should be entered:

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Globalization;
using Windows.Graphics.Imaging;
using Windows.Media.Ocr;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Popups;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;

public class Library
    private const string app_title = "Recognition App";
    private const string text_file_extension = ".txt";
    private const string image_file_extension = ".jpg";

    public async Task ConfirmAsync(string content, string title, string ok, string cancel)
        bool result = false;
        MessageDialog dialog = new MessageDialog(content, title);
        dialog.Commands.Add(new UICommand(ok, new UICommandInvokedHandler((cmd) => result = true)));
        dialog.Commands.Add(new UICommand(cancel, new UICommandInvokedHandler((cmd) => result = false)));
        await dialog.ShowAsync();
        return result;

    public async void NewAsync(Image source, TextBox target)
        if (await ConfirmAsync("Create New?", app_title, "Yes", "No"))
            source.Source = null;
            target.Text = string.Empty;

    public async void OpenAsync(Image source, TextBox target)
            FileOpenPicker picker = new FileOpenPicker
                SuggestedStartLocation = PickerLocationId.DocumentsLibrary
            StorageFile file = await picker.PickSingleFileAsync();
            switch (file.FileType)
                case text_file_extension:
                    target.Text = await FileIO.ReadTextAsync(file);
                case image_file_extension:
                    using (IRandomAccessStream stream = await file.OpenReadAsync())
                        BitmapDecoder bitmapDecoder = await BitmapDecoder.CreateAsync(stream);
                        SoftwareBitmap softwareBitmap = await bitmapDecoder.GetSoftwareBitmapAsync(
                            BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
                        OcrEngine engine = OcrEngine.TryCreateFromLanguage(new Language("en-us"));
                        OcrResult ocrResult = await engine.RecognizeAsync(softwareBitmap);
                        target.Text = ocrResult.Text;
                        BitmapImage image = new BitmapImage();
                        source.Source = image;


    public async void SaveAsync(Image source, TextBox target)
            FileSavePicker picker = new FileSavePicker
                SuggestedStartLocation = PickerLocationId.DocumentsLibrary
            picker.FileTypeChoices.Add("Image File", new List() { image_file_extension });
            picker.FileTypeChoices.Add("Text File", new List() { text_file_extension });
            picker.DefaultFileExtension = text_file_extension;
            StorageFile file = await picker.PickSaveFileAsync();
            switch (file.FileType)
                case text_file_extension:
                    await FileIO.WriteTextAsync(file, target.Text);
                case image_file_extension:
                    using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
                        RenderTargetBitmap render = new RenderTargetBitmap();
                        await render.RenderAsync(target);
                        IBuffer buffer = await render.GetPixelsAsync();
                        encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
                        (uint)render.PixelWidth, (uint)render.PixelHeight, 96.0, 96.0, buffer.ToArray());
                        await encoder.FlushAsync();
                        buffer = null;
                        encoder = null;


    public void Sample(ref TextBox target)
        target.Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris non massa diam. " +
            "Nunc luctus non lorem id imperdiet. Nunc quis mi nec enim malesuada commodo mollis eget nisl. " +
            "Sed vulputate in purus eu vulputate. Quisque commodo eu odio et malesuada. Duis porttitor, " +
            "lectus ut egestas placerat, purus nisi elementum diam, congue lacinia erat lectus sit amet felis. " +
            "Proin suscipit lobortis bibendum. Aliquam erat volutpat. Nunc vitae nulla nunc.";

In the Library.cs there are using statements to include the necessary functionality. The ConfirmAsync Method is used to create a dialog with two buttons, this is used by the NewAsync method to confirm if a new document is to be created, if the answer is “Yes” then the TextBox will have it’s contents set to an empty string. OpenAsync is used to set the TextBox contents to the contents of a file – this is opened with a FileOpenPicker using ReadTextAsync and can also open a file for the Image then using the OcrEngine to recognise any text in this. SaveAsync is used to get the contents of the TextBox and then this is saved to a file, using the FileSavePicker and WriteTextAsync and can also save the contents of the TextBox as an Image. Then there is a Sample Method which will set the TextBox to some example text.

Step 8

In the Solution Explorer select MainPage.xaml


Step 9

From the Menu choose View and then Designer


Step 10

The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:

<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
		<RowDefinition Height="50*"/>
		<RowDefinition Height="50*"/>
	<Image Grid.Row="0" Name="Source"/>
	<TextBox Grid.Row="1" Name="Target" AcceptsReturn="True" TextWrapping="Wrap"/>
<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Icon="Page2" Label="New" Click="New_Click"/>
	<AppBarButton Icon="OpenFile" Label="Open" Click="Open_Click"/>
	<AppBarButton Icon="Save" Label="Save" Click="Save_Click"/>
	<AppBarButton Icon="Document" Label="Sample" Click="Sample_Click"/>

The first block of XAML the main user interface of the Application, this features a Grid with two rows, the first is an Image which will be set when one has been opened and the text recognised and the second row is a TextBox where any text may be entered or opened and then saved as an image to be opened and recognised. The second block of XAML is is the CommandBar which contain New which can set the TextBox to empty text, Open which will allow a file to be opened and Save will allow a file to be saved and Sample is used to insert some example text.

Step 11

From the Menu choose View and then Code


Step 12

Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:

Library library = new Library();

private void New_Click(object sender, RoutedEventArgs e)

private void Open_Click(object sender, RoutedEventArgs e)
	library.OpenAsync(Source, Target);

private void Save_Click(object sender, RoutedEventArgs e)
	library.SaveAsync(Source, Target);

private void Sample_Click(object sender, RoutedEventArgs e)
	library.Sample(ref Target);

Below the MainPage() Method an instance of the Library Class is created, New_Click, Open_Click and Save_Click Methods call the related methods in the Library Class as does Sample_Click.

Step 13

That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application


Step 14

After the Application has started running you can enter any text or use Sample then use Save to store an Image file of the text, then Open to recognise a stored Image file as text in the Application


Step 15

To Exit the Application select the Close button in the top right of the Application


Creative Commons License


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s