3

MSDN says that the bitmap used in GetDiBits should not be selected into a DC before calling this function. But from my experience(with BitBlt) I know that I cannot draw an a bitmap unless it is selected.

  1. How does GetDiBits circumvents this? Can I just use an unselected, newly created bitmap as argument to this function?
  2. GetDiBits as well CreateDibSection returns an array. But MSDN says about the first function:

    "bits of the specified compatible bitmap"

    I thought DI stands for DeviceIndependent. Why is there a contradiction? This means that, according to MSDN, GetDiBits can be used only with CreateCompatibleBitmap (which is DD)? Then I can't send this array to another machine to display it,right?

  3. Both functions use a hDC. If CreateDibSection is truly DIndependent why does it need a hDC? All the needed info is provided through the bitmapinfoheader...
zx485
  • 28,498
  • 28
  • 50
  • 59
sergiu reznicencu
  • 1,039
  • 1
  • 11
  • 31

1 Answers1

5
  1. I cannot draw an a bitmap unless it is selected. How does GetDiBits circumvents this?

GetDIBits doesn't do any drawing. It reads pixel data from a bitmap and converts it into the desired color format. SetDIBits doesn't "draw" either, but it will set the pixel data in a bitmap.

  1. Naming confusion.

The DI in GetDIBitmap refers to the fact that the pixel data is returned in a device-independent format (specifically, the one you ask for). The source bitmap can be a compatible bitmap or a device-independent bitmap.

Similarly SetDIBitmap takes device-independent pixel data and converts it to the type of the target bitmap.

These functions are confusingly named.

  1. What's the DC for?

The DC is used to answer any questions about the pixel format on the device. For example, if the source format is a palette-based device-dependent bitmap, GetDIBits will assume the palette selected into the DC is the correct one. Note that the palette is not in the BITMAPINFOHEADER.

CreateDIBSection creates a hybrid bitmap that stashes data in a device-independent method, but may also keep a device-dependent copy in sync with it for performance. So it needs to know the DC of the intended device.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
  • GetDiBits returns an array. If I edit this array it will change the bitmap as well, right?(because it is a pointer to its memory...unless it is const). Then if edit the bmp this way, I am effectively coloring without any assistence from the hdc(no pallete ,size or whatever). Is this correct? – sergiu reznicencu Apr 11 '18 at 21:19
  • No, GetDIBits gives you a _copy_ of the pixel data (in the format you request). If you want to change the bitmap, you can change that pixel data and then update the bitmap using SetDIBits. And then you have to select it back into a memory DC and blit it again to see your change. – Adrian McCarthy Apr 11 '18 at 21:21
  • Thanks. CreateDibSection is more suited for this ,no? – sergiu reznicencu Apr 11 '18 at 21:24
  • Sure, but either way works. Just watch for synchronization problems if you're using GDI to draw to the bitmap *and* trying to read and write the pixel data yourself. (The MSDN page for CreateDIBSection has details.) – Adrian McCarthy Apr 11 '18 at 21:29
  • "_but may also keep a device-dependent copy in sync with it_" -- Where do you get that from? – zett42 Apr 13 '18 at 15:53
  • 1
    @zett42: From conversations with Windows evangelists many years ago. They explained that the DIB stays in system memory (a file mapping object). When you blit it, it has to be converted to the display's pixel format, and the converted data needs to be transferred to video memory. A DIB section, they explained, may cache that DDB in video memory so that a second blit (assuming the DIB hasn't been altered) doesn't need to do the conversion and transfer again. So, effectively, the DIB section may maintained as a DDB version in cache, which is re-sync'ed as needed. – Adrian McCarthy Apr 13 '18 at 16:18
  • 1
    @zett42: I believe it was also explained in the _Windows 2000 Graphics API Black Book_ by Damon Chandler, but I finally recycled my copy so I can't easily double check. – Adrian McCarthy Apr 13 '18 at 16:24