1
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.

jessenave
  • 38
  • 1
  • 7
  • Possible duplicate of this question - http://stackoverflow.com/questions/12753437/saving-a-bitmapimage-object-as-a-file-in-local-storage-in-windows-8 – SWalters Dec 29 '12 at 02:01
  • thank you but no, I read that question. That has to do with saving straight from the camera. I cannot do that with my app. – jessenave Dec 29 '12 at 02:05
  • Unfortunately, as that thread indicates, it is not possible to save directly from a BitmapImage; you have to go back to the source. Since you are capturing the stream from the camera, it'd be as simple as hanging onto that stream object until the user presses the button. – SWalters Dec 29 '12 at 02:22

1 Answers1

4

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);
    }
}
Damir Arh
  • 17,637
  • 2
  • 45
  • 83