Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

GetDIBits is very slow

Posted on 2006-06-27
17
Medium Priority
?
1,507 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

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 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

971 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