Windows 8 如何使用C++;适用于Windows Metro应用程序?

Windows 8 如何使用C++;适用于Windows Metro应用程序?,windows-8,windows-runtime,winrt-xaml,c++-cx,imaging,Windows 8,Windows Runtime,Winrt Xaml,C++ Cx,Imaging,我可以使用以下代码从资源JPG图像文件轻松创建位图图像 Windows::Foundation::Uri^ uri = ref new Windows::Foundation::Uri(L"ms-appx:///Hippo.JPG"); Imaging::BitmapImage^ image = ref new Imaging::BitmapImage(uri); 但是WritableBitmap不接受Uri。我看到一个SetSource方法,但它需要一个irandomaccesstream而

我可以使用以下代码从资源JPG图像文件轻松创建位图图像

Windows::Foundation::Uri^ uri = ref new Windows::Foundation::Uri(L"ms-appx:///Hippo.JPG");
Imaging::BitmapImage^ image = ref new Imaging::BitmapImage(uri);
但是WritableBitmap不接受Uri。我看到一个SetSource方法,但它需要一个irandomaccesstream而不是一个Uri。我不知道如何从JPG文件创建一个。我在网上搜索了一遍又一遍,但找不到一个直截了当的答案。任何帮助都将不胜感激

我想要这样的东西

Windows::UI::Xaml::Media::Imaging::WriteableBitmap image = ref new Windows::UI::Xaml::Media::Imaging::WriteableBitmap();
image->SetSource(somehowGetRandomAccessStreamFromUri);
但是,如何从uri获取irandomaccesstream实例?我今天才开始使用C++ Metro应用程序,所以可能是错误的,但是我发现它太复杂了,洋葱剥落太多了。
var storageFile = await Package.Current.InstalledLocation.GetFileAsync(relativePath.Replace('/', '\\'));
var stream = await storageFile.OpenReadAsync();
var wb = new WriteableBitmap(1, 1);
wb.SetSource(stream);
我认为在C++/CX中,您可以这样做:

#include <ppl.h>
#include <ppltasks.h>

...

Concurrency::task<Windows::Storage::StorageFile^> getFileTask
    (Package::Current->InstalledLocation->GetFileAsync(L"Assets\\MyImage.jpg"));

auto getStreamTask = getFileTask.then(
    [] (Windows::Storage::StorageFile ^storageFile)
    {
        return storageFile->OpenReadAsync();
    });

getStreamTask.then(
    [] (Windows::Storage::Streams::IRandomAccessStreamWithContentType^ stream)
    {
        auto wb = ref new Windows::UI::Xaml::Media::Imaging::WriteableBitmap(1, 1);
        wb->SetSource(stream);
    });
#包括
#包括
...
并发::任务getFileTask
(Package::Current->InstalledLocation->GetFileAsync(L“Assets\\MyImage.jpg”);
auto getStreamTask=getFileTask.then(
[](Windows::Storage::StorageFile^StorageFile)
{
返回storageFile->OpenReadAsync();
});
getStreamTask.then(
[](Windows::Storage::Streams::IrandoMacAccessStreamWithContentType^stream)
{
auto wb=ref新Windows::UI::Xaml::Media::Imaging::WriteableBitmap(1,1);
wb->SetSource(流);
});

谢谢菲利普,它就像一个符咒。不过我不得不做一个小小的改变。由于某种原因,我收到一个无效参数异常,将L“Assets\\MyImage.jpg”更改为L“MyImage.jpg”。最后发现WritableBitmap的像素内容既不可读也不可写:我可以得到一个不提供访问缓冲区的iBFFER指针!无论如何,这是一个单独的问题。在C++中,AbIX使用该方法将缓冲区转换成I/O流。它也必须在C++中可行。检查C样本。我在另一个论坛中找到了一个解决方案,如如何从IBuffer获得字节。erpret_cast(writableImage->PixelBuffer);IBufferByteAccess*pBufferByteAccess=nullptr;HRESULT hr=pUnk->QueryInterface(IID_PPV_参数(&pBufferByteAccess));字节*像素=nullptr;pBufferByteAccess->Buffer(&pixels);