c# - 文件保存选取器 - 保存编辑后的图像(C# Metro 应用程序)

标签 c# microsoft-metro filesavepicker

我想打开一张图片,编辑它,然后保存它。我可以打开一个文件,但我无法保存它。按照我编写代码的方式,我只能用 .jpg 保存一个文件,但里面什么也没有。

请告诉我如何保存我打开和编辑的图像(尚未制作)。

public sealed partial class MainPage : Page
{
    BitmapImage originalImage = new BitmapImage();

    public MainPage()
    {
        this.InitializeComponent();
    }

    private async void OpenButton_Click(object sender, RoutedEventArgs e)
    {
        var filePicker = new FileOpenPicker();
        filePicker.FileTypeFilter.Add(".jpg");
        filePicker.FileTypeFilter.Add(".jpeg");
        filePicker.FileTypeFilter.Add(".gif");
        filePicker.ViewMode = PickerViewMode.Thumbnail;
        filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        filePicker.SettingsIdentifier = "PicturePicker";
        filePicker.CommitButtonText = "Select File";
        StorageFile selectedFile = await filePicker.PickSingleFileAsync();
        var stream = await selectedFile.OpenAsync(FileAccessMode.Read);

        if (selectedFile != null)
        {
            originalImage.SetSource(stream);
            pictureBox.Source = originalImage;
        }
    }

    private async void SaveButton_Click(object sender, RoutedEventArgs e)
    {
        FileSavePicker savePicker = new FileSavePicker(); 
        savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
        savePicker.FileTypeChoices.Add("jpeg image", new List<string>() { ".jpg" });
        savePicker.SuggestedFileName = "EditedImage";
        StorageFile file = await savePicker.PickSaveFileAsync();
    }
}

最佳答案

创建图像文件后,您需要对其进行更新,请参阅 FileSavePicker类。

在您的SaveButton_Click 方法中添加以下代码并尝试修改它。

这将使您可以将创建的文件更新为真实的图像文件。

if (file != null)
{
    // Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
    CachedFileManager.DeferUpdates(file);
    // write to file
    await FileIO.WriteTextAsync(file, file.Name);
    // Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
    // Completing updates may require Windows to ask for user input.
    FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
    if (status == FileUpdateStatus.Complete)
    {
        OutputTextBlock.Text = "File " + file.Name + " was saved.";
    }
    else
    {
        OutputTextBlock.Text = "File " + file.Name + " couldn't be saved.";
    }
}
else
{
    OutputTextBlock.Text = "Operation cancelled.";
}

关于c# - 文件保存选取器 - 保存编辑后的图像(C# Metro 应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27051805/

相关文章:

c# - 突出显示方法的导出点

c# - Rhinoscript 将 Python 对象移动到 C#

c# - 在磁贴更新上显示应用程序名称

c++ - 带有 DirectX 部分的 Metro 风格应用程序

c# - FileSavePicker 任务未显示在 wp8 c# 的 Window.Storage.Pickers 中

c# - 在 C++(win32 应用程序)中使用 C# 类时出现 EEFileLoadException

c# - UserControl 的 ViewModel 上的 DependencyProperty 未通过绑定(bind)更新

c# - 如何让 Metro GridView 中的组使用不同的布局?

c# - 如何使用FileSavePicker保存现有的StorageFile?