GetDIBits is very slow

I developing a program where I do a lot of GetDIBits.

In one computer it takes 60ms (which I think is a lot).
But in another computer it takes almost 1 second (which is unacceptable).

I am trying to find a way to get the DIB in another way.
Please note that I need it in 8 pixels per byte.

Please send the reply to my email

Who is Participating?
AlexFMConnect With a Mentor Commented:
Check display settings on this computer. Can you set them to true 32 bits color?
It depends on BITMAPINFO parameters and screen settings. If copying bits requires format conversion, function runs slowly.
Please show your code and describe the problem.

EE doesn't allow to publish email address, all answeres are posted here.
VapiSoftAuthor Commented:
Thanks, I just wanted to get a note and I received it.

I know that it needs a converion because I need it in 8 pixels per byte, but I think that one second is way too much (60ms is also too much).

Following is the code that returns the hDIB handle (from hBitmap).

HANDLE getPictureHandle(HBITMAP hBitmap, HDC hdc, int &nLines, int &nColumns, LPBYTE &start)
 BITMAP Bitmap;
 //HDC hDC;

      HANDLE               hMem;
 WORD                 biBits;

      GetObject(hBitmap, sizeof(Bitmap), (LPSTR)&Bitmap);
 int w2=Bitmap.bmWidth;
 int h2=Bitmap.bmHeight;

      biBits = n_pixels; // Bitmap.bmPlanes * Bitmap.bmBitsPixel;
      bih.biSize            = sizeof(BITMAPINFOHEADER);
      bih.biWidth           = w2;
      bih.biHeight          = h2;
      bih.biPlanes          = Bitmap.bmPlanes;
      bih.biBitCount        = n_pixels; // 32; // Bitmap.bmBitsPixel;
      bih.biCompression     = 0;
      bih.biSizeImage       = 0;
      bih.biXPelsPerMeter   = 3780;
      bih.biYPelsPerMeter   = 3780;
      bih.biClrUsed         = 0;
      bih.biClrImportant    = 0;

      int dwLen  = bih.biSize + ColorTableSize(&bih);

      HANDLE hDIB = GlobalAlloc(GPTR, dwLen);
      if (!hDIB)
            return 0;
      *lpbih = bih;
 /// get the actual size

      bih = *lpbih;
      if (bih.biSizeImage == 0)
            return 0;
      dwLen = bih.biSize + ColorTableSize(&bih) + bih.biSizeImage;
      hMem = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE|GMEM_ZEROINIT);
 if (!hMem) return 0;
      hDIB = hMem;
      //printDebugLog("GlobalReAlloc: hDIB=%x",hDIB);

 int color_table_size=ColorTableSize(lpbih);
 start=(LPBYTE)lpbih + (WORD)lpbih->biSize + color_table_size;

      DWORD t1=GetTickCount();


      //CreateDIBSection(hdc,(LPBITMAPINFO) &lpbih,DIB_RGB_COLORS,(void **) &start,0,0);

 DWORD t2=GetTickCount();
      printDebugLog("GetDIBits: dt=%d",t2-t1);

 return hDIB;

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

So, you want to get bits in 32 bpp format. What is hBitmap format, from where do you get it?
My guess is that GetDIBits runs fast if hBitmap has the same format, and gets more time for other formats. Please give more information about bitmap passed to this function, what is it's source and what do you want to do with it.
VapiSoftAuthor Commented:
I get this hBitmap from the screen.

 HDC hdc=GetWindowDC(hwnd);
 HDC memDC   =CreateCompatibleDC(hdc);
 HBITMAP hbmp=CreateCompatibleBitmap(hdc,,;
 HBITMAP oldBmp=(HBITMAP) SelectObject(memDC,hbmp);
 BitBlt(memDC,0,0,,, hdc, x_offset, y_offset, SRCCOPY);
 //oldBmp=(HBITMAP) SelectObject(memDC,hbmp);


 ReleaseDC(hwnd, hdc);
Try to do the following:

1) Create memory DC (source DC) and select hBitmap in it
2) Create destination 32 bpp DIB using CreateDIBSection
3) Create memory DC (destination) and select destination bitmap in it
4) BitBlt from source to destination DC
5) Read pixels from destination bitmap using GetBitmapBits.
VapiSoftAuthor Commented:
Hi, I tried it and DID solve the problem.
I thank you very very much.
VapiSoftAuthor Commented:
Hi AlexFM

I was a bit hasty. It did not real solved the problem, it just shifted the time from the GetDiBits (5) to the CreateDIBSection (2).
Although, the time of the CreateDIBSection is about half the time (360ms) it used to take for the GetDiBits 800ms).
Please note that it only happens in one of me computers.
VapiSoftAuthor Commented:

I found it (thanks to you).
It was 32 bits, but the problem was in the "Display Properties"->"Settings"->"Advanced"->"Troubleshoot"->
"Hardware acceleration".
It was: Full. I set it to None.
It solved the problem. Now  it takes only 40ms (1/10 than it used to take).
VapiSoftAuthor Commented:
What is the difference between "Hardware acceleration" None and Full?
How can I get/set this value in the application (API or Registry)?
I don't know this.
VapiSoftAuthor Commented:
OK, thanks any how.
I think I answered original question.
Note to PAQ searchers:
This Member asked several related questions.  Additional ideas and suggestions can be found there:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.