Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

GetDIBits is very slow

Posted on 2006-06-27
17
Medium Priority
?
1,492 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
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…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

705 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