How to convert DIB to DDB

Source: Internet
Author: User

 

 

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.