troubleshooting Question

reading 32 bit bitmaps

Avatar of flynny
flynnyFlag for United Kingdom of Great Britain and Northern Ireland asked on
12 Comments1 Solution1240 ViewsLast Modified:
Hi all,

I have a method which was reading in a 24bit bitmap and accessing the individual pixels( i really oly want to do some processing when a pixel is black.)

I've tried to change this to work with 32bit bitmaps however the output im getting is strange. can anyone tell me what i'm doing wrong please?

void readBitmap(LPTSTR path)
      CString szFilename(path);
      HBITMAP hBmp = (HBITMAP)::LoadImage(NULL,szFilename,

      //got bitmap now get and read the byte array
      //first read the header
      HDC hdc = GetDC(NULL);
      BITMAPINFO BitmapInfo = {0};
      BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);

      if(GetDIBits(hdc, hBmp, 0, 0, NULL, &BitmapInfo, DIB_RGB_COLORS))
            //got the header now need to read it
            BYTE *pBits = new BYTE[BitmapInfo.bmiHeader.biSizeImage];

       if (pBits)
               // zero out the bitmap bit buffers
               memset(pBits, 0, BitmapInfo.bmiHeader.biSizeImage);

                     //get the height and width of the image
               long h = BitmapInfo.bmiHeader.biHeight;
               long w = BitmapInfo.bmiHeader.biWidth;

               BYTE rVal, gVal, bVal;

               int x, y = 0;

                                 logzz  );
                           logmm << "starting.. \n";
               //now iterate through the image getting the each pixel
               for (x = 0; x < w; x++)
                     //iterate down the column first to try and find first black pixel
                     for (y = h-1; y > -1; y--)
                           GetDIBits(hdc, hBmp, y, BitmapInfo.bmiHeader.biHeight, pBits, &BitmapInfo, DIB_RGB_COLORS);

                           //will need to change to included 24 bitmaps etc.
                           rVal = pBits[(x * 4) + 2];   // red
                           gVal = pBits[(x * 4) + 1];      //green
                           bVal = pBits[(x * 4) + 0];      //blue

                           if(rVal == 0 && gVal == 0 && bVal == 0)
                        logzz, ios::app  );
                                 logmm << "#";

                                 //first load in one of the training bitmaps

                                 int z;
                                 for(z=0; z<10; z++)
                                          CString szFilename;
                                          szFilename.Format("C:\\data\\%d.bmp", z);

                                             HBITMAP tBmp = (HBITMAP)::LoadImage(NULL,_T(szFilename),

                                          //get the bitmap header of this bitmap to find height width
                                          //for cutting out subsection of bitmap
                                          BITMAPINFO tBitmapInfo = {0};
                                          tBitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
                                          GetDIBits(hdc, tBmp, 0, 0, NULL, &tBitmapInfo, DIB_RGB_COLORS);

                                          //now got header can get the height and width
                                          long th = tBitmapInfo.bmiHeader.biHeight;
                                          long tw = tBitmapInfo.bmiHeader.biWidth;

                                          //now get subsection of bitmap

                                          HBITMAP subsection = CreateSubBitmap(hBmp, x, 0, tw, th);

                                          bool same = CompareBitmaps(tBmp, subsection);                              
                  logzz, ios::app  );
                           logmm << "_";
                  logzz, ios::app  );
                        logmm << "\n";
                // clean up
            delete[] pBits;




Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 12 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros