Getting the content in a window.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DumRat
    New Member
    • Mar 2007
    • 93

    Getting the content in a window.

    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:

    Code:
            RECT rc;
    	
    	::GetWindowRect(hWnd, &rc);
    
    	int w = rc.right-rc.left;
    	int h = rc.bottom-rc.top;
    
    	HDC hDC = ::GetDC(hWnd);
    	HDC memDC = ::CreateCompatibleDC(hDC);
    
    	HBITMAP memBM = ::CreateCompatibleBitmap(hDC, w, h);
    
    	::SelectObject(memDC, memBM );
    
    	::BitBlt(memDC, 0, 0, w, h , hDC, rc.left, rc.top , SRCCOPY );
    	int size = 3 * w * h;
    	int *lpBits = new int[size];
    
    	::GetBitmapBits(memBM, size, lpBits );
    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.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Code commented in bold

    Code:
            RECT rc;
    
    	::GetWindowRect(hWnd, &rc);
    [b]	// GetWindowRect is the wrong function to call and will 
    	// result in the wrong values for w and h as it includes the 
    	// entire window including border and menu and title bars
    	// Use GetClientRect instead
    	// In a client rect rc.left == 0 and rc.top == 0 always
    	// so in the following code w = rc.right, h = rc.bottom[/b]
    
    	int w = rc.right-rc.left;
    	int h = rc.bottom-rc.top;
    
    [b]	// GetDC gets the client DC[/b]
    	HDC hDC = ::GetDC(hWnd);
    	HDC memDC = ::CreateCompatibleDC(hDC);
    
    	HBITMAP memBM = ::CreateCompatibleBitmap(hDC, w, h);
    
    	::SelectObject(memDC, memBM );
    
    	::BitBlt(memDC, 0, 0, w, h , hDC, rc.left, rc.top , SRCCOPY );
    [b]	// The source position parameters are wrong, hDC is a client DC
    	// Since you called GetWindowRect you will be copying from just
    	// to the right and just beneath the actual window
    	// Source position needs to be 0, 0[/b]
    
    
    [b]	// Why do all this when you can just manipulate the 
    	// Bitmap thorugh memDC?  You can use any of the drawing 
    	// functions that take a DC[/b]
    	int size = 3 * w * h;
    	int *lpBits = new int[size];
    
    	::GetBitmapBits(memBM, size, lpBits );
    Originally posted by DumRat
    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?

    Comment

    Working...