gpt4 book ai didi

c++ - 从 UWP vc++ 中生成的字节数组生成图像

转载 作者:可可西里 更新时间:2023-11-01 10:02:54 26 4
gpt4 key购买 nike

引用 Question@Decade Moon 回答
我如何使用该方法从字节数组而不是图像文件生成图像。
我试过如下但没有任何效果。没有显示图像

std::vector<char> data= std::vector<char>(imgx->Height * imgx->Width * 4);
int offset;
for (int row = 0; row < imgx->Height; row++)
{
for (int col = 0; col < imgx->Width; col++)
{
offset = (row * (int)(imgx->Width * 4)) + (col * 4);
data[offset] = 0x58; // Red
data[offset + 1] = 0x58; // Green
data[offset + 2] = 0x58; // Blue
data[offset + 3] = 0x58; // Alpha
}
};

最佳答案

我的方法与您提到的回复略有不同,但效果很好。

#include <wrl.h>  
#include <robuffer.h>

using namespace Windows::UI::Xaml::Media::Imaging;
using namespace Windows::Storage::Streams;
using namespace Microsoft::WRL;

typedef uint8 byte;
byte* GetPointerToPixelData(IBuffer^ pixelBuffer, unsigned int *length)
{
if (length != nullptr)
{
*length = pixelBuffer ->Length;
}
// Query the IBufferByteAccess interface.
ComPtr<IBufferByteAccess> bufferByteAccess;
reinterpret_cast<IInspectable*>(pixelBuffer)->QueryInterface(IID_PPV_ARGS(&bufferByteAccess));

// Retrieve the buffer data.
byte* pixels = nullptr;
bufferByteAccess->Buffer(&pixels);
return pixels;
}

MainPage::MainPage()
{
InitializeComponent();

auto bitmap = ref new WriteableBitmap(50, 50);
image->Source = bitmap;

unsigned int length;
byte* sourcePixels = GetPointerToPixelData(bitmap->PixelBuffer, &length);
const unsigned int width = bitmap->PixelWidth;
const unsigned int height = bitmap->PixelHeight;

create_async([this, width, height, sourcePixels] {
byte* temp = sourcePixels;

// generate RED - BLUE gradient
for(unsigned int k = 0; k < height; k++) {
for (unsigned int i = 0; i < (width * 4); i += 4) {
int pos = k * (width * 4) + (i);
temp[pos] = (byte)(0xFF * k / (float)height); // B
temp[pos + 1] = 0x0; // G
temp[pos + 2] = 0xFF - (byte)(0xFF * k / (float)height); // R
temp[pos + 3] = 0xFF; // A
}
}
});
}

enter image description here

关于c++ - 从 UWP vc++ 中生成的字节数组生成图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48293390/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com