Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding GetDiBits. C++

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...
like image 982
sergiu reznicencu Avatar asked Mar 31 '26 23:03

sergiu reznicencu


1 Answers

  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.

like image 147
Adrian McCarthy Avatar answered Apr 03 '26 15:04

Adrian McCarthy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!