2012-10-23 4 views
0

로컬 디렉토리에 bitmapimages를 저장하고 싶습니다. 그리고 그 코드를 썼습니다. 그러나 알 수없는 오류가 발생하여 컴파일 할 수 없습니다. 오류의 원인과 bitmapImages를 변환하고 저장하는 올바른 방법을 알려주십시오.Windows8에서 bitmapImages를 변환하고 저장하는 방법

void StoreAndGetBitmapImage() 
    { 
     BitmapImage image = new BitmapImage(new Uri("ms-appx:///Assets/" + "test.png")); 
     StorageFile storageFile = ConvertBitmapImageIntoStorageFile(image, "image_name"); 
     StoreStorageFile(storageFile); 
     BitmapImage resultImage = GetBitmapImage("image_name"); 
    } 

    StorageFile ConvertBitmapImageIntoStorageFile(BitmapImage bitmapImage,string fileName) 
    { 
     StorageFile file = Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(bitmapImage.UriSource).GetResults(); 
     file.RenameAsync(fileName); 
     return file; 
    } 

    void StoreStorageFile(StorageFile storageFile) 
    { 
     storageFile.CopyAsync(Windows.Storage.ApplicationData.Current.LocalFolder); 
    } 

    BitmapImage GetBitmapImage(string fileName) 
    { 
     BitmapImage bitmapImage; 
     bitmapImage = new BitmapImage(); 

     bitmapImage.UriSource = new Uri(new Uri(
      Windows.Storage.ApplicationData.Current.LocalFolder.Path + "\\" + 
      Windows.Storage.ApplicationData.Current.LocalFolder.Name), 
      fileName); 

     return bitmapImage; 
    } 

답변

0

await 비동기 메서드 호출이 필요합니다. 따라서 메서드를 async로 선언해야합니다. 예 :

async Task<StorageFile> ConvertBitmapImageIntoStorageFile(BitmapImage bitmapImage,string fileName) 
{ 
    StorageFile file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(bitmapImage.UriSource); 
    await file.RenameAsync(fileName); 
    return file; 
} 

await 결과가 메소드에서 반환됩니다. 작업이 완료되면 메서드가 바로이 위치에서 계속됩니다 (어쩌면 다른 스레드에서). 비동기 메소드는 IAsyncOperation 객체를 반환합니다. a Task. 이것은 시작된 프로 시저의 핸들이며 완료된 때를 판별하는 데 사용할 수 있습니다.

+0

고맙습니다. 나는이 메소드를 동 기적으로 실행하기를 원하기 때문에 의도적으로 "기다리고있다". – JohnyDgoode

+0

그런 다음 비동기 작업의 반환 값에 대해 Wait()을 호출해야합니다. –

+0

나는 본다! 감사 ! – JohnyDgoode

관련 문제