First, you do not need to convert DIB to DDB to display it on the device. However, you will see how slow the program is, and you cannot use function functions related to DDB operations, such as BitBlt ()....
The basic steps for creating a DDB from DIB are as follows:
1. You can create a logical palette from the information in the DIB color table. If the device supports this, you only need to perform this step. To create a palette, You need to assign it to the logical palette structure. Initialize palversion and palnumentries, and copy the number of colors from the DIB table. Then we can use the createpalette () function to use our initialized logical palette.
2. Select the logical palette to the device and implement it.
3. Create a DDB and use the createdibbitmap () function.
4. Do not forget to release the memory allocated to the logical palette structure.
In this way, you can create a logical palette, but it does not return information to the called program code. If DIB describes a 256-color bitmap and the device only supports 256 colors, DDB may not be correctly displayed on the device. This is because the number of colors used by the system is different from the number of Bitmap colors. Therefore, you need to modify the color so that it will return the logical color palette before we draw the bitmap, then we select and implement it to the context of the device.
// The following is the program code
Hbitmap dibtoddb (handle hdib)
{
LpbitmapinfoheaderLpbi;
HbitmapHbm;
CpalettePal;
Cpalette *Poldpal;
CclientdcDc (null );
If (hdib = null)
Return null;
Lpbi = (lpbitmapinfoheader) hdib;
Int ncolors = lpbi-> biclrused? Lpbi-> biclrused:
1 <lpbi-> bibitcount;
Bitmapinfo & bminfo = * (lpbitmapinfo) hdib;
Lpvoid lpdibbits;
If (bminfo. bmiheader. bibitcount> 8)
Lpdibbits = (lpvoid) (lpdword) (bminfo. bmicolors +
Bminfo. bmiheader. biclrused) +
(Bminfo. bmiheader. bicompression = bi_bitfields )? 3: 0 ));
Else
Lpdibbits = (lpvoid) (bminfo. bmicolors + ncolors );
// Create and select a logical palette if needed
If (ncolors <= 256 & dc. getdevicecaps (rastercaps) & rc_palette) {uint nsize = "sizeof (logpalette)" + (sizeof (paletteentry) * ncolors); logpalette * plp = "(logpalette "*) new byte [nsize]; plp-> palversion = 0x300;
Plp-> palnumentries = ncolors;
For (int I = 0; I palpalentry [I]. pered = bminfo. bmicolors [I]. rgbred;
Plp-> palpalentry [I]. pegreen = bminfo. bmicolors [I]. rgbgreen;
Plp-> palpalentry [I]. peblue = bminfo. bmicolors [I]. rgbblue;
Plp