Solved

GetDIBits is very slow

Posted on 2006-06-27
17
1,393 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:VapiSoft
Comment Utility
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
Comment Utility
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
Comment Utility
Hi, I tried it and DID solve the problem.
I thank you very very much.
0
 

Author Comment

by:VapiSoft
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 48

Accepted Solution

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

Author Comment

by:VapiSoft
Comment Utility
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
Comment Utility
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
Comment Utility
I don't know this.
0
 

Author Comment

by:VapiSoft
Comment Utility
OK, thanks any how.
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
I think I answered original question.
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now