Visual c++ c++;bitmapimage的字节数组

Visual c++ c++;bitmapimage的字节数组,visual-c++,c++-cli,uwp,Visual C++,C++ Cli,Uwp,这是我关于stackoverflow的第一个问题。 我对UWP编程很陌生,由于某种原因,我需要用C++来做。现在我正试图解决这个问题: 我有一个图像字节数组,希望在UI中显示它们。下面的代码是我尝试过的,但似乎不起作用。 这里是C++代码: BYTE input[160000] = ...; InMemoryRandomAccessStream ^stream = ref new InMemoryRandomAccessStream(); DataWriter ^writer = ref new

这是我关于stackoverflow的第一个问题。 我对UWP编程很陌生,由于某种原因,我需要用C++来做。现在我正试图解决这个问题: 我有一个图像字节数组,希望在UI中显示它们。下面的代码是我尝试过的,但似乎不起作用。 这里是C++代码:

BYTE input[160000] = ...;
InMemoryRandomAccessStream ^stream = ref new InMemoryRandomAccessStream();
DataWriter ^writer = ref new DataWriter();
writer->WriteBytes(Platform::ArrayReference<BYTE>(input, sizeof(input)));
stream->WriteAsync(writer->DetachBuffer());
stream->Seek(0);
BitmapImage ^image = ref new BitmapImage();
image->SetSourceAsync(stream);
outputPic->Source = image;
字节输入[160000]=。。。;
InMemoryRandomAccessStream ^stream=ref new InMemoryRandomAccessStream();
DataWriter^writer=ref new DataWriter();
writer->WriteBytes(平台::ArrayReference(输入,sizeof(输入));
stream->WriteAsync(writer->DetachBuffer());
流->搜索(0);
BitmapImage^image=ref新建BitmapImage();
图像->设置源同步(流);
outputPic->Source=图像;
以下是xaml代码:

<Image x:Name="outputPic" Source="Assets/Gray.png" Width="420" Stretch="Uniform" Height="420"/>

以下是一个示例:

MainPage.xaml(节选)


MainPage.xaml.cpp(节选)

#包括
#包括
#包括
主页::主页()
{
初始化组件();
std::ifstream输入(“Assets\\StoreLogo.png”,std::ios::binary);
std::vector数据((std::istreambuf_迭代器(输入)),(std::istreambuf_迭代器());
//我刚刚通过data.data()获得了一个指向图像数据的指针
自动位图图像=参考新位图图像();
图像->源=位图图像;
自动流=ref new INMEMORYRANDOMACCESS stream();
自动写入器=引用新数据写入器(流->GetOutputStreamAt(0));
writer->WriteBytes(数组引用((无符号字符*)data.data(),data.size());
创建_任务(writer->StoreAsync())。然后([=](unsigned bytesStored)
{
返回位图图像->设置源同步(流);
});
}

“但似乎不起作用”是一个非常模糊的错误描述。请提供一个复制您的问题(所有错误消息包括逐字)。抱歉,描述不好,“但似乎不起作用”:图像只是不显示。在触发事件之前,图像显示正确,在触发事件之后,图像没有显示任何内容。从未使用过,但我猜应该有类似
create_task(image->SetSourceAsync(stream))。然后([](void){outputPic->Source=image;})。类似于其他异步调用,如WriteAsync。
<Image x:Name="image"/>
#include <fstream>
#include <vector>
#include <iterator>

MainPage::MainPage()
{
    InitializeComponent();

    std::ifstream input("Assets\\StoreLogo.png", std::ios::binary);
    std::vector<char> data((std::istreambuf_iterator<char>(input)), (std::istreambuf_iterator<char>()));

    // I just obtained a pointer to the image data through data.data()

    auto bitmapImage = ref new BitmapImage();
    image->Source = bitmapImage;

    auto stream = ref new InMemoryRandomAccessStream();
    auto writer = ref new DataWriter(stream->GetOutputStreamAt(0));

    writer->WriteBytes(ArrayReference<unsigned char>((unsigned char*)data.data(), data.size()));

    create_task(writer->StoreAsync()).then([=](unsigned bytesStored)
    {
        return bitmapImage->SetSourceAsync(stream);
    });
}