How save photo capture windows 8 c# metro app?

2019-08-01 05:00发布

问题:

I looked everywhere but can't find how save my photo after I captured it. I use windows 8 media capture, and after I capture it, I show it on my page. But don't know how to save it to a particular place.

This is how I take the photo, pretty classic:

    private async void Camera_Clicked(object sender, TappedRoutedEventArgs e)
    {
        TurnOffPanels();

        CameraCaptureUI camera = new CameraCaptureUI();
        camera.PhotoSettings.CroppedAspectRatio = new Size(16, 9);
        StorageFile photo = await camera.CaptureFileAsync(CameraCaptureUIMode.Photo);

        if (photo != null)
        {
            BitmapImage bmp = new BitmapImage();
            IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);
            bmp.SetSource(stream);
            ImageSource.Source = bmp;
            ImageSource.Visibility = Visibility.Visible;

            appSettings[photoKey] = photo.Path;
        }
    }

And this is how I reload it after I've taken it :

    private async Task ReloadPhoto(String photoPath)
    {
            StorageFile photo = await StorageFile.GetFileFromPathAsync(photoPath);
            BitmapImage bmp = new BitmapImage();
            using (IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read))
            {
                bmp.SetSource(stream);
            }

    }

Does anyone have an idea how to save the photo?

Regards.

回答1:

Use FileSavePicker, example below :

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

photo is a StorageFile that you get form camera



回答2:

I tried, but don't success. I can now save but i save nothing. I think i failed with the ImageStream which i don't understand.

This is the code without the reader. Sorry i'm a beginner in Windows 8 C#

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

        FileSavePicker savePicker = new FileSavePicker();
        savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        savePicker.FileTypeChoices.Add("jpeg", new List<string>() { ".jpeg" });
        savePicker.SuggestedFileName = "New picture";

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


回答3:

perhaps this

      private async void btnSave(object sender, RoutedEventArgs e)
    {
        //ImageTarget.Source = await ResizeWritableBitmap(m_bitmap, 800, 800);
        if (m_bitmap == null)
            return;

         Windows.Storage.StorageFile filename = await GetSaveLocation();
        if (filename == null)
           return;

        IRandomAccessStream filestream = await filename.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);

        BitmapEncoder encode = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, filestream);

        byte []pixelbuff;
        var stream = m_bitmap.PixelBuffer.AsStream();
        pixelbuff = new byte[stream.Length];

        await stream.ReadAsync(pixelbuff, 0, pixelbuff.Length);

        encode.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight,(uint) m_bitmap.PixelWidth
                            ,(uint) m_bitmap.PixelHeight, 96.0, 96.0, pixelbuff);
        await encode.FlushAsync();
        await filestream.FlushAsync();

        filestream.Dispose();
    }