问题
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.
- How does
GetDiBits
circumvents this? Can I just use an unselected, newly created bitmap as argument to this function? GetDiBits
as wellCreateDibSection
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 withCreateCompatibleBitmap
(which is DD)? Then I can't send this array to another machine to display it,right?- 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...
回答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.
- 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.
- 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.
来源:https://stackoverflow.com/questions/49784146/understanding-getdibits-c