2014-09-19 3 views
0

WinRT C++을 처음 사용합니다. C#에서 StorageFile 이미지를 전달하고 파일을 열고 WinRT의 BitmapImage 소스로 설정하여 이미지의 높이와 너비를 추출하려고합니다. 다음 코드를 사용하고 있습니다.BitmapImage SetSourceAsync in WinRT C++

auto openOperation = StorageImageFile->OpenAsync(FileAccessMode::Read); // from http://msdn.microsoft.com/en-us/library/windows/desktop/hh780393%28v=vs.85%29.aspx 
openOperation->Completed = ref new 
    AsyncOperationCompletedHandler<IRandomAccessStream^>(
    [=](IAsyncOperation<IRandomAccessStream^> ^operation, AsyncStatus status) 
{ 
    auto Imagestream = operation->GetResults(); 
    BitmapImage^ bmp = ref new BitmapImage(); 
    auto bmpOp = bmp->SetSourceAsync(Imagestream); 
    bmpOp->Completed = ref new 
     AsyncActionCompletedHandler (
     [=](IAsyncAction^ action, AsyncStatus status) 
    { 
     action->GetResults(); 
     UINT32 imageWidth = (UINT32)bmp->PixelWidth; 
     UINT32 imageHeight = (UINT32)bmp->PixelHeight; 
    }); 
}); 

이 코드는 작동하지 않는 것 같습니다. BitmapImage^bmp = ref 새로운 BitmapImage(); 라인 이후 디버거는 소스 코드가 없다는 말을 멈 춥니 다. 올바른 코드를 작성할 수 있습니까?

+0

을 :

는 나는 다음과 같이 대략 모양한다고 생각합니다 > 완료'**'+ ='**'ref new ... ' –

답변

1

난 당신이 openOperation->Completed+=ref new...bmpOp->Completed+=ref new...를 작성하는 의미 생각합니다. 저는 C++의 전문가는 아니지만 제가 본 것으로부터 - 비동기 작업은 일반적으로 create_task 호출로 싸여 있습니다. 구독 취소없이 이벤트 구독을 피하는 이유는 무엇입니까? 난 당신이`openOperation-> Completed` **`+ =`** '심판 새로운 ...`와`bmpOp-를 작성하는 의미 생각

auto bmp = ref new BitmapImage(); 

create_task(storageImageFile->OpenAsync(FileAccessMode::Read)) // get the stream 
    .then([bmp](IRandomAccessStream^ ^stream) // continuation lambda 
{ 
    return create_task(bmp->SetSourceAsync(stream)); // needs to run on ASTA/Dispatcher thread 
}, task_continuation_context::use_current()) // run on ASTA/Dispatcher thread 
    .then([bmp]() // continuation lambda 
{ 
     UINT32 imageWidth = (UINT32)bmp->PixelWidth; // needs to run on ASTA/Dispatcher thread 
     UINT32 imageHeight = (UINT32)bmp->PixelHeight; // needs to run on ASTA/Dispatcher thread 

     // TODO: use imageWidth and imageHeight 
}, task_continuation_context::use_current()); // run on ASTA/Dispatcher thread 
+0

고마워. 필자는 ppl 작업 및 concunrrency 작업에 대해 거의 이해하지 못했습니다. 약간의 조언을 위해서 비동기 함수가 IAsyncOperation을 반환 할 때 create_task()를 사용해야하며 비동기 함수가 IAsyncAction을 반환 할 때 반환 값을 캡처하기 위해 task_continuation_context :: use_current()를 사용해야합니다. 그게 맞습니까? – croc

+0

'create_task'는 계속할 수있는 작업에'IAsyncOperation'을 래핑합니다. '.then (lambda, task_continuation_context :: use_current())'는 UI 객체를 터치 할 때 필요한 UI 스레드에서 람다를 실행합니다. –