Solved

GetDIBits is very slow

Posted on 2006-06-27
17
1,426 Views
Last Modified: 2013-12-03
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 shlomo@vapisoft.com


Thanks,
Shlomo;
0
Comment
Question by:VapiSoft
  • 7
  • 6
17 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 16992527
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.
0
 

Author Comment

by:VapiSoft
ID: 16992607
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)
{
 BITMAPINFOHEADER bih;
 //HPALETTE hPal;
 BITMAP Bitmap;
 //HDC hDC;

      HANDLE               hMem;
 WORD                 biBits;
      LPBITMAPINFOHEADER   lpbih;

      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;
      bih.biSizeImage=0;

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

      HANDLE hDIB = GlobalAlloc(GPTR, dwLen);
      
      if (!hDIB)
 {
            return 0;
      }
      
      lpbih = (LPBITMAPINFOHEADER)hDIB;
      
      *lpbih = bih;
      
 /// get the actual size
      nLines=GetDIBits(hdc,
            hBitmap,
            (UINT)0,
            (UINT)bih.biHeight,
  (LPVOID)NULL,
            (LPBITMAPINFO)lpbih,
            DIB_RGB_COLORS);

      bih = *lpbih;
      
      if (bih.biSizeImage == 0)
 {
            if(hDIB)
            {
                  GlobalFreeDIB(hDIB,5);
            }
            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);

 lpbih = (LPBITMAPINFOHEADER)hDIB;
 int color_table_size=ColorTableSize(lpbih);
 start=(LPBYTE)lpbih + (WORD)lpbih->biSize + color_table_size;
 nColumns=bih.biWidth;

      DWORD t1=GetTickCount();

 nLines=GetDIBits(hdc,
              hBitmap,
              (UINT)0,
              (UINT)bih.biHeight,
    start,
              (LPBITMAPINFO)lpbih,
              DIB_RGB_COLORS);
 

      //CreateDIBSection(hdc,(LPBITMAPINFO) &lpbih,DIB_RGB_COLORS,(void **) &start,0,0);
 //memcpy(start,start1,bih.biSizeImage);

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

 return hDIB;
}

0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16992754
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.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:VapiSoft
ID: 16993236
I get this hBitmap from the screen.

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

 msg->hDIB=getPictureHandle(hbmp,hdc,wr.cy,wr.cx,msg->pixels);

 SelectObject(memDC,oldBmp);
 DeleteObject(hbmp);
 DeleteObject(memDC);
 ReleaseDC(hwnd, hdc);
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16994992
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.
0
 

Author Comment

by:VapiSoft
ID: 16995603
Hi, I tried it and DID solve the problem.
I thank you very very much.
0
 

Author Comment

by:VapiSoft
ID: 17005074
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.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 17007109
Check display settings on this computer. Can you set them to true 32 bits color?
0
 

Author Comment

by:VapiSoft
ID: 17007137
Hi,

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).
0
 

Author Comment

by:VapiSoft
ID: 17007217
What is the difference between "Hardware acceleration" None and Full?
How can I get/set this value in the application (API or Registry)?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17007308
I don't know this.
0
 

Author Comment

by:VapiSoft
ID: 17007374
OK, thanks any how.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17293838
I think I answered original question.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 17359160
Note to PAQ searchers:
This Member asked several related questions.  Additional ideas and suggestions can be found there:

   http://www.experts-exchange.com/MFC/Q_21910717.html
   http://www.experts-exchange.com/MFC/Q_21908987.html
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question