C ++字节数组到bitmapimage [英] c++ byte array to bitmapimage
本文介绍了C ++字节数组到bitmapimage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我关于stackoverflow的第一个问题。
我是UWP编程的新手,由于某种原因,我需要用C ++进行。现在,我正在尝试解决此问题:
我有一个字节数组的图像,想要在UI中显示它们。以下代码是我尝试过的代码,但是似乎无效。
这是C ++代码:
This is my first question on stackoverflow. I'm new to UWP programming, and due to some reason I need to do it in C++. Now I'm trying to solve this problem: I've got a byte array of images and want to show them in the UI. The following code is what I've tried but seems don't work. Here is C++ code:
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;
这是xaml代码:
<Image x:Name="outputPic" Source="Assets/Gray.png" Width="420" Stretch="Uniform" Height="420"/>
推荐答案
下面是一个示例:
MainPage.xaml(摘录)
<Image x:Name="image"/>
MainPage.xaml.cpp(摘录)
#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);
});
}
这篇关于C ++字节数组到bitmapimage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文