Difference between getbitmapbits and getdibits (Windows GDI)

Source: Internet
Author: User

Windows GDI has two APIs for obtaining bitmap image data: getbitmapbits and getdibits;
According to msdn, the former is used to obtain bits of the device's Independent Bitmap,
The latter is the bits that is compatible with bitmap,

So when calling this function,
The first major difference is that getdibits must provide a device content and select the bitmap handle into the device content (DC) to obtain bitmap information.
I think everyone knows the difference above,
In fact, it also hides another difference: For the same bitmap, the buffer of the obtained bits content is different!

We all know that the stored data in BMP files is flashback, that is, it is stored from the bottom right corner of the image, and the last part of the file is the upper left corner of the image (For details, refer to the introduction in Windows Programming );
Using the buffer obtained by getbitmapbits, the content in the lower right corner of the bitmap is the first byte, which is actually the same as the real image byte,

Getdibits is the opposite. The buffer sequence conforms to the order in the BMP file. If it follows the normal coordinates, the storage sequence should be flashback.
Therefore, you need to use these two APIs reasonably in the program to obtain the bitmap data you want.

The above is taken from the Internet. The following is an example:
Bytes ---------------------------------------------------------------------------------------------------
1. If our cbitmap is a device-compatible bitmap: That is, it is best to use the bitmap created by createcompatiblebitmap.
Getdibits: getdibits (memdc. m_hdc, BTP, 0, BP. bmheight, pdata, pbpinfo, dib_rgb_colors );
Then you can display the bitmap based on the bitmap data.
// Display the bitmap in the customer area
Cclientdc DC (this );
Stretchdibits (DC. m_hdc, 0, 0, BP. bmwidth, BP. bmheight, 0, 0, BP. bmwidth, BP. bmheight, pdata, pbpinfo, dib_rgb_colors, srccopy );

You can also use getbitmapbits to obtain bitmap information, but the obtained bitmap data must be converted to match the actual data of the original bitmap.

// For device-compatible bitmaps, the bitmap data obtained using getbitmapbits must be converted to display the bitmap correctly.
// Use getbitmapbits to exchange data in each row, and then forward the entire buffered data.
// The original storage format of bitmap is as follows:
// [G... K]
// [J... t]
// [...]
// [Y... B]
// [X... A]
// However, getbitmapbits is used to obtain the bitmap data format [X... a] [y... b] [...] [j... t] [g... k]
// We need to convert it to [g... k] [j... t] [...] [y... b] [X... a]
// Step: Set the data of each row in step 1 to [... x] [B... y] [...] [t... j] [k... g]
// Step 2: Take the format obtained in step 1 as a whole and reverse it to get [g... k] [j... t] [...] [y... b] [X... a]
Dwret = BTP. getbitmapbits (BP. bmwidthbytes * bp. bmheight, pdata); // This method obtains bitmap information. The bitmap object should be obtained in the form of loadbitmap.

Int nrow = bp. bmheight; // number of rows
// First swap the bitmap data of each row
Char * PS = NULL, * Pe = NULL;
For (Int J = 1; j <= nrow; j ++)
{
PS = pdata + (J-1) * bp. bmwidthbytes;
Pe = pdata + J * bp. bmwidthbytes-1;

Swaparray (Ps, PE, BP. bmwidthbytes );
}

// Swap the entire array of data
Swaparray (pdata, pdata + dwRet-1, dwret );

// Display the bitmap in the customer area
Cclientdc DC (this );
Stretchdibits (DC. m_hdc, 0, 0, BP. bmwidth, BP. bmheight, 0, 0, BP. bmwidth, BP. bmheight, pdata, pbpinfo, dib_rgb_colors, srccopy );
Localfree (pdata );

Void swaparray (char * ps, char * PE, int ncount)
{
Int nmidindex = ncount/2;

Char TMP;
For (INT I = 0; I <nmidindex; I ++)
{
TMP = * pS;
* PS = * PE;
* Pe = TMP;
PS ++;
Pe --;
}
 
}

 

2. If the bitmap is not related to the device,For example, if you use loadbitmap to load bitmap resources, you can use getbitmapbits to obtain the problematic data and convert the data to display the bitmap. For example:
 

// Create a device-independent bitmap
Cclientdc DC1 (this );
Cbitmap btp1;
Btp1.loadbitmap (idb_bitmap1 );
Btp1.getbitmap (& BP );
Pdata = (char *) localalloc (lptr, BP. bmwidthbytes * bp. bmheight );
Btp1.getbitmapbits (BP. bmwidthbytes * bp. bmheight, pdata );

Pbtinfo-> bmiheader. bibitcount = bp. bmbitspixel;
Pbtinfo-> bmiheader. biclrimportant = 0;
Pbtinfo-> bmiheader. bicompression = 0;
Pbtinfo-> bmiheader. biheight = bp. bmheight;
Pbtinfo-> bmiheader. biwidth = bp. bmwidth;
Pbtinfo-> bmiheader. biplanes = bp. bmplanes;
Pbtinfo-> bmiheader. bisizeimage = bp. bmwidthbytes * bp. bmheight;
Pbtinfo-> bmiheader. bisize = sizeof (bitmapinfo );
Pbtinfo-> bmiheader. bixpelspermeter = 0;
Pbtinfo-> bmiheader. biypelspermeter = 0;

Int nrows = bp. bmheight; // number of rows in the bitmap
Char * PS = NULL, * Pe = NULL;
For (INT I = 0; I <nrows; I ++)
{
PS = pdata + I * bp. bmwidthbytes;
Swaparray (Ps, BP. bmwidthbytes );
}
 
Swaparray (pdata, BP. bmwidthbytes * bp. bmheight );
Stretchdibits (dc1.m _ HDC, 0, 0, BP. bmwidth, BP. bmheight, 0, 0, BP. bmwidth, BP. bmheight, pdata, pbtinfo, dib_rgb_colors, srccopy );

// Parameter PS: the starting address of the array to be swapped
// Parameter dwcount: the total length of the data to be exchanged
Void ctdddlg: swaparray (char * ps, uint dwcount)
{
Char temp;
For (uint I = 0; I <dwcount/2; I ++)
{
Temp = Ps [I];
PS [I] = Ps [dwcount-I-1];
PS [dwcount-I-1] = temp;
}
}

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.