user140503
user140503

Reputation: 127

how save image to folder from Image control Win 8 apps

I have a Image control

       <Image Name="img" HorizontalAlignment="Left"  Height="400" Margin="499,155,0,0" VerticalAlignment="Top" Width="400"/>

And I capture photo from Camera

        private async void TakePhoto_Click(object sender, RoutedEventArgs e)
    {

        CameraCaptureUI camera = new CameraCaptureUI();
        camera.PhotoSettings.CroppedAspectRatio = new Size(4, 3);

        var photo = await camera.CaptureFileAsync(CameraCaptureUIMode.Photo);

        if (photo != null)
        {
            BitmapImage image = new BitmapImage();
            image.SetSource(await photo.OpenAsync(FileAccessMode.Read));
            img.Source = image;
        }
    }

And now I want to save this image from img control to folder, but I don't know how decode image from img control to StorageFile

        private async void SaveFile_Click(object sender, RoutedEventArgs e)
    {
        FileSavePicker savePicker = new FileSavePicker();
        savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        savePicker.FileTypeChoices.Add("jpeg image", new List<string>() { ".jpeg" });
        savePicker.SuggestedFileName = "New picture";

        StorageFile ff = await savePicker.PickSaveFileAsync();
        if (ff != null)
        {
            await photo.MoveAndReplaceAsync(ff);
        }
    }

this is a save method, which i want to save image from Image control

Upvotes: 2

Views: 409

Answers (1)

leafartist
leafartist

Reputation: 623

The var photo = await camera.CaptureFileAsync(CameraCaptureUIMode.Photo); returns a StorageFile. All you have to do is save it to the location you want.

You can accomplish this in may ways.

Example:

StorageFolder localFolder = ApplicationData.Current.LocalFolder; // the app's local storage folder


await photo.MoveAsync(localFolder); //move the file to a new location

2.

//Create a new copy in a new location

await photo.CopyAsync(localFolder);

Note that var photo == StorageFile photo. This means that the after the photo was captured, a copy of it was already saved to a temporary location(The App's TempState directory) on the local storage.

Upvotes: 2

Related Questions