tags:

views:

304

answers:

3

If I create a DIB using CreateDIBSection(), I can specify wether it should be top-down or bottom-up by setting the biHeight field of the BITMAPINFOHEADER struct to a negative or positive value. However, if i call GetObject() on such a bitmap to fill a DIBSECTION structure, then both the dsBm.bmHeight and dsBmih.biHeight seem to be always positive.

Is there a way to find out if a DIB is top-down or bottom-up when all I have is the HBITMAP?

+1  A: 

No; you need the BITMAPINFOHEADER for that, and it isn't recoverable from a HBITMAP.

chaos
A: 

I think the best you're going to be able to get from an HBITMAP is going to be GetDIBits. The problem with GetDIBits however, is that it also requires an HDC and the HBITMAP has to be a DDB.

see: GetDIBits Function

I'm not positive, but its possible you'll lose any top-downness in the conversion.

Jim H.
A: 

There is no way to retrieve that information from GDI. As you ahve discovered the BITMAP AND BITMAPINFOHEADER bmHeight fields retrieved via GetObject are always positive. The bmWidthBytes field is, likewise, positive. If you draw onto (or from) the DIBSection using the GDI handle GDI will draw the DIBSection the correct way up. Using the extracted BITMAPINFOHEADER is going to get top down bitmaps wrong.

The only way to tell from usermode would seem to be to use GDI to test - SetPixel the top left and bottom left pixels of the bitmap to two different values, then read the buffer to see which write landed in the first memory location.

Really the best option is to build into your API - whever you pass around a DIBSection as an HBITMAP, ensure that there is at least a bool fIsDibSectionTopDown passed or stored as well.

Chris Becke