从字节数组创建 CImage [英] Create CImage from Byte array

查看:30
本文介绍了从字节数组创建 CImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to create a CImage from a byte array (actually, its an array of unsigned char, but I can cast to whatever form is necessary). The byte array is in the form "RGBRGBRGB...". The new image needs to contain a copy of the image bytes, rather than using the memory of the byte array itself.

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

I have tried many different ways of achieving this -- including going through various HBITMAP creation functions, trying to use BitBlt -- and nothing so far has worked.

要测试函数是否工作,它应该通过这个测试:

To test whether the function works, it should pass this test:

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 指向一个空(零填充)数组结束.

All implementations of createCImage() so far have ended up with data pointing to an empty (zero filled) array.

推荐答案

谢谢大家,最后在大家的帮助下解决了.它主要涉及@tinman 和@Roel 的使用SetDIBitsToDevice() 的建议,但它涉及一些额外的位操作和内存管理,所以我想我会在这里分享我的终点.

Thanks everyone, I managed to solve it in the end with your help. It mainly involved @tinman and @Roel's suggestion to use SetDIBitsToDevice(), but it involved a bit of extra bit-twiddling and memory management, so I thought I'd share my end-point here.

在下面的代码中,我假设 widthheightBpp(每个像素的字节数)是设置,并且 data 是指向 RGB 像素值数组的指针.

In the code below, I assume that width, height and Bpp (Bytes per pixel) are set, and that data is a pointer to the array of RGB pixel values.

// Create the header info
bmInfohdr.biSize = sizeof(BITMAPINFOHEADER);
bmInfohdr.biWidth = width;
bmInfohdr.biHeight = -height;
bmInfohdr.biPlanes = 1;
bmInfohdr.biBitCount = Bpp*8;
bmInfohdr.biCompression = BI_RGB;
bmInfohdr.biSizeImage = width*height*Bpp;
bmInfohdr.biXPelsPerMeter = 0;
bmInfohdr.biYPelsPerMeter = 0;
bmInfohdr.biClrUsed = 0;
bmInfohdr.biClrImportant = 0;

BITMAPINFO bmInfo;
bmInfo.bmiHeader = bmInfohdr;
bmInfo.bmiColors[0].rgbBlue=255;

// Allocate some memory and some pointers
unsigned char * p24Img = new unsigned char[width*height*3];
BYTE *pTemp,*ptr;
pTemp=(BYTE*)data;
ptr=p24Img;

// Convert image from RGB to BGR
for (DWORD index = 0; index < width*height ; index++)
{
    unsigned char r = *(pTemp++);
    unsigned char g = *(pTemp++);
    unsigned char b = *(pTemp++);   

    *(ptr++) = b;
    *(ptr++) = g;
    *(ptr++) = r;
}

// Create the CImage
CImage im;
im.Create(width, height, 24, NULL);

HDC dc = im.GetDC();
SetDIBitsToDevice(dc, 0,0,width,height,0,0, 0, height, p24Img, &bmInfo, DIB_RGB_COLORS);
im.ReleaseDC();

delete[] p24Img;

这篇关于从字节数组创建 CImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆