2014-11-20 16 views
2

我想打开图像,编辑它,然后保存它。我可以打开一个文件,但我在保存时遇到问题。我编写代码的方式,我只能用.jpg保存文件,但没有任何内容。文件保存选取器 - 保存编辑后的图像(C#Metro应用程序)

请向我解释如何保存已打开和编辑的图像(尚未制作)。

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

回答

0

创建图片文件后,您需要更新它,参见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."; 
} 
相关问题