How to save Bitmapimage to local folder Windows Store app

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.

Answers


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);
    }
}

Need Your Help

Differences in reading file using ifstream in g++ and msvc

c++ stream filestream std ifstream

When using ifstream class to read words from an input file, I have used the following expression:

Third party dependencies in a delphi expert

delphi

i am writting a delphi ide expert, with some third party dependencies (visual controls), my question is when this expert will be installed in a target machine , this pc will be requires have insta...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.