我是WinRT c ++的新手。我正在尝试从C#传递StorageFile图像并打开文件,并将其设置为WinRT中BitmapImage中的源,以提取图像的高度和宽度。我正在使用以下代码。
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 new BitmapImage();调试器停止说找不到源代码。
您能帮我写正确的代码吗?
最佳答案
我认为您打算写openOperation->Completed
+=
ref new...
和bmpOp->Completed
+=
ref new...
。我不是C ++专家,但是从我所看到的-异步操作通常包装在create_task
调用中。不确定为什么-也许避免在不取消订阅的情况下订阅事件?
我认为它应该大致如下所示:
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
关于c++ - WinRT C++中的BitmapImage SetSourceAsync,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25932162/