I can imagine two different scenarios that could nudge you into this question:
- The application needs to store the file for some reason, but the user doesn't need to know about it.
- You want the user to know and to be able to access the file.
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>