I want to save and load InkCanvas
gif
file without FilePicker
.
I saw a sample using FilePicker
, but I want to sav
I can imagine two different scenarios that could nudge you into this question:
In the first scenario, I suppose you don't care which path you use, so you can use the folder where the application data are stored:
var selectedFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
In the second case, you can let the user chose a path then, every time he click the "Save" button you can automatically save the image:
private async void btnSelectFolder_Click(object sender, RoutedEventArgs e)
{
var picker = new FolderPicker();
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
picker.FileTypeFilter.Add("*");
selectedFolder = await picker.PickSingleFolderAsync();
TxbFolder.Text = selectedFolder.Path;
}
In the click event handler of the Save button, you change only where you retrieve the file, the rest remains as in the example:
private async void btnSave_Click(object sender, RoutedEventArgs e)
{
// Get all strokes on the InkCanvas.
IReadOnlyList<InkStroke> currentStrokes = inkCanvas.InkPresenter.StrokeContainer.GetStrokes();
// Strokes present on ink canvas.
if (currentStrokes.Count > 0)
{
var file = await selectedFolder.CreateFileAsync("InkSample.gif", CreationCollisionOption.GenerateUniqueName);
if (file != null)
{
// The rest remains the same as in the example
// ...
}
}
}
In the following there is the XAML code and the main page constructor modified:
private StorageFolder selectedFolder;
public MainPage()
{
this.InitializeComponent();
// Set supported inking device types.
inkCanvas.InkPresenter.InputDeviceTypes =
Windows.UI.Core.CoreInputDeviceTypes.Mouse |
Windows.UI.Core.CoreInputDeviceTypes.Pen;
// Listen for button click to initiate save.
btnSave.Click += btnSave_Click;
// Listen for button click to clear ink canvas.
btnClear.Click += btnClear_Click;
btnSelectFolder.Click += btnSelectFolder_Click;
selectedFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
TxbFolder.Text = selectedFolder.Path;
}
XAML
<Grid
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="HeaderPanel" Orientation="Horizontal" Grid.Row="0">
<TextBlock x:Name="Header"
Text="Basic ink store sample"
Style="{ThemeResource HeaderTextBlockStyle}"
Margin="10,0,0,0" />
<TextBox x:Name="TxbFolder"
Text="Select a folder"
Width="250"
Margin="24,12,10,12"/>
<Button x:Name="btnSelectFolder"
Content="..."
Margin="0,0,10,0"/>
<Button x:Name="btnSave"
Content="Save"
Margin="24,0,10,0"/>
<Button x:Name="btnClear"
Content="Clear"
Margin="24,0,10,0"/>
</StackPanel>
<Grid Grid.Row="1">
<InkCanvas x:Name="inkCanvas" />
</Grid>
UWP apps run in a sandbox, so that the user can know what the app is doing and which files on her hard drive it accesses.
In case you want to save files to a location on the user's hard drive, you will have to be given access to this location first. There are several options how to achieve this:
StorageFile
under a key, which will allow you to retrieve it again next time.StorageFolder
under a key, which will allow you to retrieve it again next time.picturesLibrary
capability in the package.appxmanifest file and then get access to the user's pictures library for writing like this: Windows.Storage.StorageLibrary.GetLibraryAsync(Windows.Storage.KnownLibraryId.Pictures);
broadFileSystemAccess
capability in your app's manifest and then you can directly access any filesystem path the user has access to. The only problem with this is that you need to have a good reason to do this (like building a file explorer app, or similar), because this capability is checked during Microsoft Store Certification and it is possible that your app would get rejected if the presence of this capability would seem unnecessary for the type of application you are publishing.