【问题标题】:how save image to folder from Image control Win 8 apps如何将图像从图像控件保存到文件夹 Win 8 应用程序
【发布时间】:2014-06-13 06:36:15
【问题描述】:

我有一个图像控件

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

我从相机拍摄照片

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

现在我想将此图像从 img 控件保存到文件夹,但我不知道如何将图像从 img 控件解码到 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);
        }
    }

这是一个保存方法,我想从 Image 控件中保存图像

【问题讨论】:

    标签: c# windows xaml windows-8


    【解决方案1】:

    var photo = await camera.CaptureFileAsync(CameraCaptureUIMode.Photo); 返回一个 StorageFile。您所要做的就是将其保存到您想要的位置。

    您可以通过多种方式完成此操作。

    例子:

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

    注意var photo == StorageFile photo。这意味着在拍摄照片后,它的副本已经保存到本地存储的临时位置(应用程序的 TempState 目录)。

    【讨论】:

    • 但我想用另一种方法保存这张图片,我无法访问 var photo
    • 您可以在类中将 photo 变量设为 public static 并在任何地方访问它。
    猜你喜欢
    • 2015-04-14
    • 2018-05-30
    • 2023-03-15
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    相关资源
    最近更新 更多