tags:

views:

81

answers:

2

I am creating a brush using CreatePatternBrush with a bitmap created with CreateBitmap.

The bitmap is 1 pixel wide and 24 pixels tall, I have the RGB value for each pixel, so I create an array of rgbquads and pass that to CreateBitmap.

This works fine when the screen color depth is 32bpp, since the bitmap I create is also 32bpp.

When the screen color depth is not 32bpp, this fails, and I understand why it does, since I should be creating a compatible bitmap instead.

It seems I should use CreateCompatibleBitmap instead, but how do I put the pixel data I have into that bitmap?

I have also read about CreateDIBPatternBrushPt, CreateDIBitmap, CreateDIBSection, etc.

I don´t understand what is a DIBSection, and find the subject generally confusing.

I do understand that I need a bitmap with the same color depth as the screen, but how do I create it having only the 32bpp pixel data?

A: 

I solved it by using CreateCompatibleBitmap and SetPixel. Not the best option I guess, but it works.

Carlos Alloatti
+1  A: 

You could create a DIB because you can use a Device Independent Bitmap independently of the screen color depth. See CreateDIBSection().

How can you create it having only the 32bpp pixel data? A DIB can be created with 32bpp data. As you can read in the documentation:

The CreateDIBSection function creates a DIB that applications can write to directly. The function gives you a pointer to the location of the bitmap bit values. If hSection is NULL, the system allocates memory for the DIB. If the function succeeds, the return value is a handle to the newly created DIB, and *ppvBits points to the bitmap bit values.

Try something like this:

VOID *ppvBits = NULL;
BITMAPINFO BitmapInfo;
memset(&BitmapInfo, 0, sizeof(BITMAPINFOHEADER));
BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BitmapInfo.bmiHeader.biWidth = 1;
BitmapInfo.bmiHeader.biHeight = 24;
BitmapInfo.bmiHeader.biPlanes = 1;
BitmapInfo.bmiHeader.biBitCount = 32;
BitmapInfo.bmiHeader.biCompression = BI_RGB;
HBITMAP hBitmap = CreateDIBSection(hDC, &BitmapInfo, DIB_RGB_COLORS, &ppvBits, NULL, 0);

In our case *ppvBits points to 1 * 24 * (32 / 8) allocated bytes.

It is important to know that if biHeight is positive, the bitmap is a bottom-up DIB and its origin is the lower-left corner. See BITMAPINFOHEADER Structure for more info.

Bill
Thank you for your answer, really.Before writing any code, I am trying to understand a few things:Does BITMAPINFO is a BITMAPINFO or a BITMAPINFOHEADER? I am pretty shure that bmiColors in BITMAPINFO is the color table, do I have to provide any bmiColors or what?The iUsage parameter of CreateDIBSection: "The type of data contained in the bmiColors array member of the BITMAPINFO structure pointed to by pbmi (either logical palette indexes or literal RGB values).""DIB_RGB_COLORS: The BITMAPINFO structure contains an array of literal RGB values." What array of literal RGB values?
Carlos Alloatti
I am guessing I should just ignore the bmiColors stuff and just pass a BITMAPINFOHEADER and not a BITMAPINFO, since this case is a 32bpp Bitmap and there is no color table.But I don´t know yet if I'am stupid or MSDN is very confusing, or maybe both. DO you think you can enlighten me a bit?
Carlos Alloatti
In your code you are ignoring bmiColors, so I think my reasoning is right.
Carlos Alloatti
Tested your code, it works. In my particular case I am sticking with CreateCompatibleBitmap and SetPixel, 1.5 secs vs 3.6 secs for CreateDIBSection, 5000 iterations. The reason for that numbers is that I am doing this in Visual FoxPro and the cost of creating the BITMAPINFOHEADER structure and converting the array of RGB integers to rgbquads is too high compared to 24 SETPIXEL. In other circumstances your answer is better I think. Thanks!
Carlos Alloatti
@Carlos I think your questions about bmiColors and iUsage are really valid. As far as I see the documentation at MSDN is not clear. I was taught to use CreateDIBSection() in this way in a newsgroup quite a few years ago. (BTW, Thanks for accepting my answer.)
Bill