Draw Editor demonstrates how to use a InkCanvas to create a drawing application where you can Load and Save drawings
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.Globalization; using System.Threading.Tasks; using Windows.Foundation; using Windows.Storage; using Windows.Storage.Pickers; using Windows.Storage.Streams; using Windows.UI; using Windows.UI.Core; using Windows.UI.Input.Inking; using Windows.UI.Popups; using Windows.UI.Xaml.Controls; public class Library { private const string app_title = "Draw Editor"; private const string file_extension = ".drw"; private string ToString(Color value) { return $"{value.A:X2}{value.R:X2}{value.G:X2}{value.B:X2}"; } private Color FromString(string value) { return Color.FromArgb( Byte.Parse(value.Substring(0, 2), NumberStyles.HexNumber), Byte.Parse(value.Substring(2, 2), NumberStyles.HexNumber), Byte.Parse(value.Substring(4, 2), NumberStyles.HexNumber), Byte.Parse(value.Substring(6, 2), NumberStyles.HexNumber)); } public async Task<bool> 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 void Init(ref InkCanvas display, ref ComboBox size, ref ComboBox colour) { string selectedSize = ((ComboBoxItem)size.SelectedItem).Tag.ToString(); string selectedColour = ((ComboBoxItem)colour.SelectedItem).Tag.ToString(); InkDrawingAttributes attributes = new InkDrawingAttributes { Color = FromString(selectedColour), Size = new Size(int.Parse(selectedSize), int.Parse(selectedSize)), IgnorePressure = false, FitToCurve = true }; display.InkPresenter.UpdateDefaultDrawingAttributes(attributes); display.InkPresenter.InputDeviceTypes = CoreInputDeviceTypes.Mouse | CoreInputDeviceTypes.Pen | CoreInputDeviceTypes.Touch; } public void Colour(ref InkCanvas display, ref ComboBox colour) { if (display != null) { string selectedColour = ((ComboBoxItem)colour.SelectedItem).Tag.ToString(); InkDrawingAttributes attributes = display.InkPresenter.CopyDefaultDrawingAttributes(); attributes.Color = FromString(selectedColour); display.InkPresenter.UpdateDefaultDrawingAttributes(attributes); } } public void Size(ref InkCanvas display, ref ComboBox size) { if (display != null) { string selectedSize = ((ComboBoxItem)size.SelectedItem).Tag.ToString(); InkDrawingAttributes attributes = display.InkPresenter.CopyDefaultDrawingAttributes(); attributes.Size = new Size(int.Parse(selectedSize), int.Parse(selectedSize)); display.InkPresenter.UpdateDefaultDrawingAttributes(attributes); } } public async void New(InkCanvas display) { if (await ConfirmAsync("Create New Drawing?", app_title, "Yes", "No")) { display.InkPresenter.StrokeContainer.Clear(); } } public async void OpenAsync(InkCanvas display) { try { FileOpenPicker picker = new FileOpenPicker { SuggestedStartLocation = PickerLocationId.DocumentsLibrary }; picker.FileTypeFilter.Add(file_extension); StorageFile file = await picker.PickSingleFileAsync(); using (IInputStream stream = await file.OpenSequentialReadAsync()) { await display.InkPresenter.StrokeContainer.LoadAsync(stream); } } catch { } } public async void SaveAsync(InkCanvas display) { try { FileSavePicker picker = new FileSavePicker { DefaultFileExtension = file_extension, SuggestedFileName = "Drawing", SuggestedStartLocation = PickerLocationId.PicturesLibrary }; picker.FileTypeChoices.Add("Drawing", new List<string>() { file_extension }); StorageFile file = await picker.PickSaveFileAsync(); if (file != null) { using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite)) { await display.InkPresenter.StrokeContainer.SaveAsync(stream); } } } catch { } } }
In the Library.cs there are using statements to include the necessary functionality. The ToString and FromString Methods are used to convert from and to a Color. The ConfirmAsync Method is used to create a dialog with two buttons, this is used by the NewAsync method to confirm if a new drawing is to be created, if the answer is “Yes” then the InkCanvas will have the StrokeContainer of its InkPresenter cleared. The Colour and Size Methods are used to set InkDrawingAttributes accordingly. OpenAsync is used to set the StrokeContainer of the InkPresenter for the InkCanvas to the contents of a file – this is opened with a FileOpenPicker using the OpenSequentialReadAsync and LoadAsync, SaveAsync is used to get the contents of the StrokeContainer for the InkPresenter of the InkCanvas using the FileSavePicker and SaveAsync.
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> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Orientation="Horizontal"> <ComboBox Name="Size" VerticalAlignment="Center" Margin="20" SelectionChanged="Size_SelectionChanged"> <ComboBoxItem Content="2" Tag="2"/> <ComboBoxItem Content="4" Tag="4"/> <ComboBoxItem Content="8" Tag="8"/> <ComboBoxItem Content="10" Tag="10" IsSelected="True"/> <ComboBoxItem Content="16" Tag="16"/> <ComboBoxItem Content="20" Tag="20"/> <ComboBoxItem Content="24" Tag="24"/> <ComboBoxItem Content="32" Tag="32"/> <ComboBoxItem Content="48" Tag="48"/> <ComboBoxItem Content="64" Tag="64"/> </ComboBox> <ComboBox Name="Colour" VerticalAlignment="Center" Margin="20" SelectionChanged="Colour_SelectionChanged"> <ComboBoxItem Tag="FF000000" IsSelected="True"> <Rectangle Width="25" Height="14" Fill="Black"/> </ComboBoxItem> <ComboBoxItem Tag="FF808080"> <Rectangle Width="25" Height="14" Fill="Gray"/> </ComboBoxItem> <ComboBoxItem Tag="FFFF0000"> <Rectangle Width="25" Height="14" Fill="Red"/> </ComboBoxItem> <ComboBoxItem Tag="FFFFA500"> <Rectangle Width="25" Height="14" Fill="Orange"/> </ComboBoxItem> <ComboBoxItem Tag="FFFFFF00"> <Rectangle Width="25" Height="14" Fill="Yellow"/> </ComboBoxItem> <ComboBoxItem Tag="FF008000"> <Rectangle Width="25" Height="14" Fill="Green"/> </ComboBoxItem> <ComboBoxItem Tag="FF00FFFF"> <Rectangle Width="25" Height="14" Fill="Cyan"/> </ComboBoxItem> <ComboBoxItem Tag="FF0000FF"> <Rectangle Width="25" Height="14" Fill="Blue"/> </ComboBoxItem> <ComboBoxItem Tag="FFFF00FF"> <Rectangle Width="25" Height="14" Fill="Magenta"/> </ComboBoxItem> <ComboBoxItem Tag="FF800080"> <Rectangle Width="25" Height="14" Fill="Purple"/> </ComboBoxItem> </ComboBox> </StackPanel> <Grid Grid.Row="1" Background="White"> <InkCanvas Name="Display"/> </Grid> </Grid> <CommandBar VerticalAlignment="Bottom"> <AppBarButton Name="New" Icon="Page2" Label="New" Click="New_Click"/> <AppBarButton Name="Open" Icon="OpenFile" Label="Open" Click="Open_Click"/> <AppBarButton Name="Save" Icon="Save" Label="Save" Click="Save_Click"/> </CommandBar>
The first block of XAML the main user interface of the Application, this features a Grid with a ComboBox for the drawing size and drawing colour. The second row has the InkCanvas that will be used for drawing with either a mouse, touch or pen. The second block of XAML is is the CommandBar which contains the New, Open and Save commands to perform the actions of the Draw Editor using the InkCanvas.
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(); protected override void OnNavigatedTo(NavigationEventArgs e) { library.Init(ref Display, ref Size, ref Colour); } private void Size_SelectionChanged(object sender, SelectionChangedEventArgs e) { library.Size(ref Display, ref Size); } private void Colour_SelectionChanged(object sender, SelectionChangedEventArgs e) { library.Colour(ref Display, ref Colour); } private void New_Click(object sender, RoutedEventArgs e) { library.New(Display); } private void Open_Click(object sender, RoutedEventArgs e) { library.OpenAsync(Display); } private void Save_Click(object sender, RoutedEventArgs e) { library.SaveAsync(Display); }
Below the MainPage() Method an instance of the Library Class is created. The OnNavigatedTo is triggered when the MainPage is loaded and this will trigger the Init Method to setup the InkCanvas for drawing. The Size_SelectionChanged and Colour_SelectionChanged Events are triggered when the ComboBox items are selected. The New_Click, Open_Click, Save_Click and Remove_Click Methods call the related methods in the Library Class.
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 then select a draw size and colour and start drawing then you can Save the drawing to Open later or start a New drawing
Step 15
To Exit the Application select the Close button in the top right of the Application