c++ - WinRT c++ 中的 BitmapImage SetSourceAsync

标签 c++ windows-runtime c++-cx ppl iasyncoperation

我是 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/

相关文章:

c# - 在 WinRT 应用程序中处理 2、3、4、5 个手指敲击、双击和按住手势

c++ - std::shared_ptr<Type> 和 Type^ 之间的区别

c++ - C++/CX 中的 [this] 是什么? (Windows 8)

c++ - 向数组中插入一个元素

c# - Win RT SQLite 事务性插入

c++ - Linux 上的 dhcp 客户端,端口或使用?

c# - 在 WinRT 的默认程序中打开 StorageItem?

c++ - C++ 中的 _In_ 是什么?

c++ - 理解realloc

c++ - 如何指示 CMake 仅使用特定目录中的库