Reputation: 38
private async void Button_Click2(object sender, RoutedEventArgs e)
{
CameraCaptureUI camera = new CameraCaptureUI();
camera.PhotoSettings.AllowCropping = true;
camera.PhotoSettings.CroppedAspectRatio = new Size(16, 9);
StorageFile photo = await camera.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (photo != null)
{
IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);
bmp.SetSource(stream);
imageGrid.Source = bmp;
}
}
private async void saveButton_Click3(object sender, RoutedEventArgs e)
{
//Save Image to the file
//What code goes here?
}
I set the image to a grid (imageGrid) after taking the picture, so that the user can view it. After completing some tasks, I want the user to be able to press a button to save the picture and text information. I don't have a problem saving text to a file, but I can't seem to find any information on saving an image in this way. I do not want to save it directly from the camera. I need to give the user the ability to save it on command. So my question is, how do I save an captured image after setting it into the UI as a XAML Image? Thanks in advance.
Upvotes: 1
Views: 3831
Reputation: 17855
As Sandra already suggested, you need to hang on to the result of CaptureFileAsync
. The method already saves the captured image to a file and returns it to you as a StorageFile
. Just store that reference to a private field and copy from it once the user clicks Save
:
private StorageFile photo;
private async void Button_Click2(object sender, RoutedEventArgs e)
{
CameraCaptureUI camera = new CameraCaptureUI();
camera.PhotoSettings.AllowCropping = true;
camera.PhotoSettings.CroppedAspectRatio = new Size(16, 9);
photo = await camera.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (photo != null)
{
IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);
bmp.SetSource(stream);
imageGrid.Source = bmp;
}
}
private async void saveButton_Click3(object sender, RoutedEventArgs e)
{
if (photo != null)
{
await photo.MoveAsync(ApplicationData.Current.LocalFolder, newFilename);
}
}
Upvotes: 4