【发布时间】:2011-10-05 04:46:37
【问题描述】:
我需要从一个字节数组创建一个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 指向一个空(零填充)数组而告终。
【问题讨论】:
-
看起来你需要为你的 imgBits 填写一个 BITMAPINFO 结构,实例化一个 CImage,调用 Create() 来创建一个正确大小的空位图,GetDC() 来获取你的 CImage 的 DC,然后使用 ::SetDIBitsToDevice() 将您的 imgBits 绘制到您的 CImage。
-
@tinman:谢谢,看来这是要走的路。我敢肯定我以前也走这条路,但放弃了,因为我认为我的头球不正确。我再试一次……