c# - 如何从图像控制 Windows 8 应用程序将图像保存到文件夹

标签 c# windows xaml windows-8

我有一个图像控件

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

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

最佳答案

var photo =等待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 目录)。

关于c# - 如何从图像控制 Windows 8 应用程序将图像保存到文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23328817/

相关文章:

c# - 转换为实现接口(interface)的通用抽象类

c# - 有没有办法将任务并行库 (TPL) 与 SQLDataReader 一起使用?

windows - 错误 - Docker-compose/docker Windows

Windows 等同于 Linux "screen"或其他替代方案?

windows - 从命令行获取默认打印机名称?

c# - 如何在插入或更新表时检查表中是否已存在行或列

c# - 将无穷大数学行为添加到 Decimal

c# - Visual Studio 设置 : How to set vertical split view to default

c# - Wpf 如何将 xaml.cs 窗口逻辑分离到单独的类中

c# - 具有不同方向的 WPF 虚拟化 TreeView 不虚拟化?