gpt4 book ai didi

c++ - 从字节数组创建 CImage

转载 作者:搜寻专家 更新时间:2023-10-31 01:18:54 25 4
gpt4 key购买 nike

我需要创建一个 CImage来自一个字节数组(实际上,它是一个 unsigned char 数组,但我可以强制转换为任何必要的形式)。字节数组的形式为“RGBRGBRGB...”。新图像需要包含图像字节的拷贝,而不是使用字节数组本身的内存。

我已经尝试了许多不同的方法来实现这一点——包括通过各种 HBITMAP 创建函数,尝试使用 BitBlt——但到目前为止没有任何效果。

为了测试该功能是否有效,它应该通过这个测试:

BYTE* imgBits;
int width;
int height;
int Bpp; // BYTES per pixel (e.g. 3)
getImage(&imgBits, &width, &height, &Bpp); // get the image bits

// This is the magic function I need!!!
CImage img = createCImage(imgBits, width, height, Bpp);

// Test the image
BYTE* data = img.GetBits(); // data should now have the same data as imgBits

到目前为止,createCImage() 的所有实现都以指向空(零填充)数组的 data 结束。

最佳答案

CImage 非常巧妙地支持 DIB,并且有一个 SetPixel() 方法,所以您大概可以做这样的事情(前面未编译、未测试的代码!):

CImage img;
img.Create(width, height, 24 /* bpp */, 0 /* No alpha channel */);

int nPixel = 0;
for(int row = 0; row < height; row++)
{
for(int col = 0; col < width; col++)
{
BYTE r = imgBits[nPixel++];
BYTE g = imgBits[nPixel++];
BYTE b = imgBits[nPixel++];
img.SetPixel(row, col, RGB(r, g, b));
}
}

也许不是最有效的方法,但我认为这是最简单的方法。

关于c++ - 从字节数组创建 CImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6691292/

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