从 Byte 数组创建 CImage
我需要创建一个 CImage一个字节数组(实际上,它是一个无符号字符数组,但我可以转换为任何需要的形式)。字节数组的格式为“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
指向一个空(零填充)数组结束。
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.
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
All implementations of createCImage()
so far have ended up with data
pointing to an empty (zero filled) array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
CImage
非常巧妙地支持 DIB,并且有一个SetPixel()
方法,因此您可能可以做这样的事情(前面未编译、未经测试的代码!):也许不是最有效的方法,但我应该认为这是最简单的方法。
CImage
supports DIBs quite neatly and has aSetPixel()
method so you could presumably do something like this (uncompiled, untested code ahead!):Maybe not the most efficient method but I should think it is the simplest approach.
使用 memcpy 复制数据,然后根据需要执行 SetDIBits 或 SetDIBitsToDevice 操作。但请注意,原始图像数据的扫描线在 4 字节边界上对齐(IIRC,自从我这样做以来已经有几年了),因此从 GetDIBits 返回的数据永远不会与原始数据完全相同(嗯,可能会,具体取决于图像大小)。
因此,您很可能需要逐条扫描线进行memcpy。
Use memcpy to copy the data, then SetDIBits or SetDIBitsToDevice depending on what you need to do. Take care though, the scanlines of the raw image data are aligned on 4-byte boundaries (IIRC, it's been a few years since I did this) so the data you get back from GetDIBits will never be exactly the same as the original data (well it might, depending on the image size).
So most likely you will need to memcpy scanline by scanline.
谢谢大家,在你们的帮助下我最终成功解决了这个问题。它主要涉及 @tinman 和 @Roel 使用
SetDIBitsToDevice()
的建议,但它涉及一些额外的位调整和内存管理,所以我想我应该在这里分享我的终点。在下面的代码中,我假设设置了
width
、height
和Bpp
(每像素字节),并且data 是指向 RGB 像素值数组的指针。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.In the code below, I assume that
width
,height
andBpp
(Bytes per pixel) are set, and thatdata
is a pointer to the array of RGB pixel values.这是一个更简单的解决方案。您可以使用 GetPixelAddress(...) 代替所有这些 BITMAPHEADERINFO 和 SedDIBitsToDevice。我解决的另一个问题是 8 位图像,它需要定义颜色表。
Here is a simpler solution. You can use GetPixelAddress(...) instead of all this BITMAPHEADERINFO and SedDIBitsToDevice. Another problem I have solved was with 8-bit images, which need to have the color table defined.