开发者

CreatePatternBrush and screen color depth

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 havin开发者_如何转开发g only the 32bpp pixel data?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜