By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,418 Members | 1,082 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,418 IT Pros & Developers. It's quick & easy.

Getting the content in a window.

P: 93
Hi,
I am in need of a way of doing the following.

1. I can get the handle to a window that I know of.
2. I can get the DC as well.

I want to Get the content of the window(DC) in to the memory as a bitmap so I can fiddle around with it.
I have seen this same problem all over internet and there are many suggestions. But I can't get my code to work.

Here's what I have in the end of a whole day:

Expand|Select|Wrap|Line Numbers
  1.  
  2.         RECT rc;
  3.  
  4.     ::GetWindowRect(hWnd, &rc);
  5.  
  6.     int w = rc.right-rc.left;
  7.     int h = rc.bottom-rc.top;
  8.  
  9.     HDC hDC = ::GetDC(hWnd);
  10.     HDC memDC = ::CreateCompatibleDC(hDC);
  11.  
  12.     HBITMAP memBM = ::CreateCompatibleBitmap(hDC, w, h);
  13.  
  14.     ::SelectObject(memDC, memBM );
  15.  
  16.     ::BitBlt(memDC, 0, 0, w, h , hDC, rc.left, rc.top , SRCCOPY );
  17.     int size = 3 * w * h;
  18.     int *lpBits = new int[size];
  19.  
  20.     ::GetBitmapBits(memBM, size, lpBits );
  21.  
  22.  
And that code is not mine. It is a Ctr + c + v from the net.

However, I can't figure it out. Can Anyone plz tell me what the format of the values in lpBits array? Is it RGBA ? etc. And how can I derive a single pixel out of it? Thanx very much in advance.
Mar 19 '07 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
Code commented in bold

Expand|Select|Wrap|Line Numbers
  1.         RECT rc;
  2.  
  3.     ::GetWindowRect(hWnd, &rc);
  4.     // GetWindowRect is the wrong function to call and will 
  5.     // result in the wrong values for w and h as it includes the 
  6.     // entire window including border and menu and title bars
  7.     // Use GetClientRect instead
  8.     // In a client rect rc.left == 0 and rc.top == 0 always
  9.     // so in the following code w = rc.right, h = rc.bottom
  10.  
  11.     int w = rc.right-rc.left;
  12.     int h = rc.bottom-rc.top;
  13.  
  14.     // GetDC gets the client DC
  15.     HDC hDC = ::GetDC(hWnd);
  16.     HDC memDC = ::CreateCompatibleDC(hDC);
  17.  
  18.     HBITMAP memBM = ::CreateCompatibleBitmap(hDC, w, h);
  19.  
  20.     ::SelectObject(memDC, memBM );
  21.  
  22.     ::BitBlt(memDC, 0, 0, w, h , hDC, rc.left, rc.top , SRCCOPY );
  23.     // The source position parameters are wrong, hDC is a client DC
  24.     // Since you called GetWindowRect you will be copying from just
  25.     // to the right and just beneath the actual window
  26.     // Source position needs to be 0, 0
  27.  
  28.  
  29.     // Why do all this when you can just manipulate the 
  30.     // Bitmap thorugh memDC?  You can use any of the drawing 
  31.     // functions that take a DC
  32.     int size = 3 * w * h;
  33.     int *lpBits = new int[size];
  34.  
  35.     ::GetBitmapBits(memBM, size, lpBits );
  36.  
  37.  
However, I can't figure it out. Can Anyone plz tell me what the format of the values in lpBits array? Is it RGBA ? etc. And how can I derive a single pixel out of it? Thanx very much in advance.
lpBits is not of a fixed format, it will depend on the display settings the user has on their computer. You could find out those settings using GetDeviceCaps and derive single pixels out of the data, however as I said in the code above why do this when you could just use GetPixel, SetPixel and all the other available drawing functions?
Mar 19 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.