简体   繁体   English

数组到C ++中的位图

[英]array to bitmap in c++

I'd like to create a bitmap by turning an array into a bitmap. 我想通过将数组转换为位图来创建位图。 First I create the bitmap from the data: 首先,我从数据创建位图:

BITMAPINFO info;
info.bmiHeader.biBitCount = 32;
info.bmiHeader.biClrImportant = 0;
info.bmiHeader.biClrUsed = 0;
info.bmiHeader.biCompression = BI_RGB;
info.bmiHeader.biHeight = -height(windowSize);
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info.bmiHeader.biSizeImage = 0;
info.bmiHeader.biWidth = width(windowSize);
info.bmiHeader.biXPelsPerMeter = 100;
info.bmiHeader.biYPelsPerMeter = 100;
Gdiplus::Bitmap *b = new Bitmap(&info, (void *)field);

And then I try to draw it on the screen, but it only contains black: 然后我尝试在屏幕上绘制它,但是它只包含黑色:

Gdiplus::Graphics *graphics = new Gdiplus::Graphics(hdc);
...
graphics->DrawImage(<pointer to bitmap>, 0, 0);

The array currently contains 32 bits of data per pixel, 8 bits for each component. 该阵列当前每个像素包含32位数据,每个组件8位。 The red component is shifted 24 bits to the left, green is shifted 16 bits to the left and blue is shifted 8 bits to the left. 红色分量向左移动24位,绿色分量向左移动16位,蓝色分量向左移动8位。

I can assure you that the field array contains data in which the colors aren't all black. 我可以向您保证,字段数组包含的数据中颜色并非全都是黑色。 Does anyone have an idea what I'm doing wrong? 有人知道我在做什么错吗?

Does the field array last for the whole lifetime of your image? 字段数组会在图像的整个生命周期中持续存在吗? Bitmap constructors that take a pointer to image data will actually store a reference to that data, rather than copying it. 使用指向图像数据指针的位图构造函数实际上将存储对该数据的引用,而不是复制它。

If you want to copy your data and have it stored in the Bitmap object, use NULL for the image bits (or use a constructor that does not require a pointer to image bits), and use the Bitmap.LockBits function to fill in the data. 如果要复制数据并将其存储在Bitmap对象中,请对图像位使用NULL(或使用不需要指向图像位的指针的构造函数),然后使用Bitmap.LockBits函数填充数据。 You don't have to copy the bits manually; 您不必手动复制这些位。 just fill in the BitmapData structure and use the ImageLockModeWrite|ImageLockModeUserInputBuf flags, and GDI+ will copy the bits by itself. 只需填写BitmapData结构并使用ImageLockModeWrite | ImageLockModeUserInputBuf标志,GDI +就会自动复制这些位。

You may also have better luck with the Bitmap(INT,INT,INT,PixelFormat,BYTE*) constructor (see http://msdn.microsoft.com/en-us/library/ms536315%28v=vs.85%29.aspx ) as it gives you more direct control over how GDI+ will interpret the data (including making it obvious whether your data contains alpha information). 您还可以使用Bitmap(INT,INT,INT,PixelFormat,BYTE *)构造函数来获得更好的运气(请参阅http://msdn.microsoft.com/zh-cn/library/ms536315%28v=vs.85%29。 aspx ),因为它使您可以更直接地控制GDI +解释数据的方式(包括使您的数据是否包含alpha信息显而易见)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM