2014-11-20 1 views
2

이미지를 열고 편집 한 다음 저장하고 싶습니다. 파일을 열 수는 있지만 저장하는 데 문제가 있습니다. 코드를 작성한 방식으로 .jpg 파일 만 저장할 수 있지만 아무 것도 없습니다.파일 저장 선택기 - 편집 된 이미지 저장 (C# Metro app)

이미지를 저장하는 방법을 알려주세요. 아직 열지도 않았지만 편집했습니다.

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