Question

Capture a Window even a window is over

Asked by: CSecurity

Hi

I want to capture a window and even another program is over it, I want to capture that window's image.

Here is code I already have.

I get lpRect like this:
HWND hwnd = FindWindow("SciCalc", "Calculator");
      GetWindowRect(hwnd , &m_rScreen);

and I pass m_rScreen to capture Calculator's image.

But if I move notepad.exe over calc.exe, I get notepad's image.

Any ideas? Please advice...
Thanks from now

HBITMAP CopyScreenToBitmap(LPRECT lpRect, BYTE *pData, BITMAPINFO *pHeader)
{
    HDC         hScrDC, hMemDC;         // screen DC and memory DC
    HBITMAP     hBitmap, hOldBitmap;    // handles to deice-dependent bitmaps
    int         nX, nY, nX2, nY2;       // coordinates of rectangle to grab
    int         nWidth, nHeight;        // DIB width and height
    int         xScrn, yScrn;           // screen resolution
 
    // check for an empty rectangle
	if (IsRectEmpty(lpRect))       
		return NULL;      
 
	hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);     
	hMemDC = CreateCompatibleDC(hScrDC);      // get points of rectangle to grab  
 
    // get points of rectangle to grab
    nX  = lpRect->left;
    nY  = lpRect->top;
    nX2 = lpRect->right;
    nY2 = lpRect->bottom;
	
    // get screen resolution
    xScrn = GetDeviceCaps(hScrDC, HORZRES);
    yScrn = GetDeviceCaps(hScrDC, VERTRES);
 
    //make sure bitmap rectangle is visible
    if (nX < 0)
        nX = 0;
    if (nY < 0)
        nY = 0;
    if (nX2 > xScrn)
        nX2 = xScrn;
    if (nY2 > yScrn)
        nY2 = yScrn;
 
	nWidth = nX2 - nX;
	nHeight = nY2 - nY;
 
    // create a bitmap compatible with the screen DC
    hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
 
    // select new bitmap into memory DC
    hOldBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);
 
    // bitblt screen DC to memory DC
    BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);
 
    // select old bitmap back into memory DC and get handle to
    // bitmap of the screen  
    hBitmap = (HBITMAP)  SelectObject(hMemDC, hOldBitmap);
 
    // Copy the bitmap data into the provided BYTE buffer
    GetDIBits(hScrDC, hBitmap, 0, nHeight, pData, pHeader, DIB_RGB_COLORS);
 
    // clean up
    DeleteDC(hScrDC);
    DeleteDC(hMemDC);
 
    // return handle to the bitmap
    return hBitmap;
}

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-10-30 at 03:44:30ID24857584
Topics

C++ Programming Language

,

Microsoft Visual C++.Net

,

C Programming Language

Participating Experts
2
Points
500
Comments
108

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Findwindow?
    Hi, A simple question, so here is only 5 points. ^_^ For examples, THREE windows with the same caption and same class. I used the following code: ShowWindow(FindWindow(pchar('Caption'),pchar('ClassHere')),SW_HIDE); Even execute the above code three times, I can still only hi...
  2. Kill Application with process id e.g notepad.exe
    How can i kill application like notepad not with findwindow, i will search for the processID like notepad.exe and send WM_CLOSE or WM_DESTROY. Can i check notepad is running and the kill the application? Can you write complete code, i'm not so good in this Regards Trausti
  3. How to shell calc.exe and bring it into focus when minimiz…
    Experts, I want to shell an external application like calc.exe from a button in my vb program, but I also need to see if it is already open so that I prevent the user from shelling it again. I also need to bring focus to that app (calc.exe) each time the user wants it, in cas...
  4. FindWindow
    Hi Is there a findwindow function that retruns all the titles of the windows opend in windows ??

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: TosterPosted on 2009-10-30 at 03:56:33ID: 25701686

Either move the window to the foreground before capturing the screen
SetForegroundWindow(hwnd);

or don't copy it from the display's DC, use the window's instead (you would have to rewrite some of the function's code to do this)
HDC hWndDC = GetDC(hwnd);

 

by: CSecurityPosted on 2009-10-30 at 04:04:08ID: 25701729

I did this but I get black image

HBITMAP CopyScreenToBitmap(LPRECT lpRect, BYTE *pData, BITMAPINFO *pHeader)
{
    HDC         hScrDC, hMemDC;         // screen DC and memory DC
    HBITMAP     hBitmap, hOldBitmap;    // handles to deice-dependent bitmaps
    int         nX, nY, nX2, nY2;       // coordinates of rectangle to grab
    int         nWidth, nHeight;        // DIB width and height
    int         xScrn, yScrn;           // screen resolution
 
    // check for an empty rectangle
	if (IsRectEmpty(lpRect))       
		return NULL;      
 
	//hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);     
	
	HWND hwnd = FindWindow("SciCalc", "Calculator");
	GetWindowRect(hwnd , lpRect);
	hScrDC = GetDC(hwnd);
	hMemDC = CreateCompatibleDC(hScrDC);      // get points of rectangle to grab  
 
    // get points of rectangle to grab
    nX  = lpRect->left;
    nY  = lpRect->top;
    nX2 = lpRect->right;
    nY2 = lpRect->bottom;
	
    // get screen resolution
    xScrn = GetDeviceCaps(hScrDC, HORZRES);
    yScrn = GetDeviceCaps(hScrDC, VERTRES);
 
    //make sure bitmap rectangle is visible
    if (nX < 0)
        nX = 0;
    if (nY < 0)
        nY = 0;
    if (nX2 > xScrn)
        nX2 = xScrn;
    if (nY2 > yScrn)
        nY2 = yScrn;
 
	nWidth = nX2 - nX;
	nHeight = nY2 - nY;
 
    // create a bitmap compatible with the screen DC
    hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
 
    // select new bitmap into memory DC
    hOldBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);
 
    // bitblt screen DC to memory DC
    BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);
 
    // select old bitmap back into memory DC and get handle to
    // bitmap of the screen  
    hBitmap = (HBITMAP)  SelectObject(hMemDC, hOldBitmap);
 
    // Copy the bitmap data into the provided BYTE buffer
    GetDIBits(hScrDC, hBitmap, 0, nHeight, pData, pHeader, DIB_RGB_COLORS);
 
    // clean up
    DeleteDC(hScrDC);
    DeleteDC(hMemDC);
 
    // return handle to the bitmap
    return hBitmap;
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:

Select allOpen in new window

 

by: TosterPosted on 2009-10-30 at 04:10:38ID: 25701768

Since you're now copying from the window, not the screen, you should start copying at the top left corner (0; 0)

BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, 0, 0, SRCCOPY);

Hope this works.. not much experience with this.

 

by: pgnatyukPosted on 2009-10-30 at 04:18:02ID: 25701798

Win32: Capture an image

There is an example about the desktop.

Shame on me, but I don't remember if it is possible to call GetDC for a window from another process.

 

 

by: CSecurityPosted on 2009-10-30 at 04:19:28ID: 25701803

Not worked... Something is wrong again...

I moved calculator close to start menu and I got start menu Image within black borders...

 

by: pgnatyukPosted on 2009-10-30 at 04:24:44ID: 25701831

Win32: Capture an image  - this does not work?

 

by: CSecurityPosted on 2009-10-30 at 04:35:28ID: 25701880

Did this, and again captures somewhere else of window...

    HDC         hScrDC, hMemDC;         // screen DC and memory DC
    HBITMAP     hBitmap, hOldBitmap;    // handles to deice-dependent bitmaps
    int         nX, nY, nX2, nY2;       // coordinates of rectangle to grab
    int         nWidth, nHeight;        // DIB width and height
    int         xScrn, yScrn;           // screen resolution
 
	
	RECT lpRect;
 
	Sleep(3000);
	HWND hwnd = FindWindow("SciCalc", "Calculator");
	GetWindowRect(hwnd , &lpRect);
	hScrDC = GetDC(hwnd);
	hMemDC = CreateCompatibleDC(hScrDC);
 
 
    nX  = lpRect.left;
    nY  = lpRect.top;
    nX2 = lpRect.right;
    nY2 = lpRect.bottom;
	
    // get screen resolution
    xScrn = GetDeviceCaps(hScrDC, HORZRES);
    yScrn = GetDeviceCaps(hScrDC, VERTRES);
 
    //make sure bitmap rectangle is visible
    if (nX < 0)
        nX = 0;
    if (nY < 0)
        nY = 0;
    if (nX2 > xScrn)
        nX2 = xScrn;
    if (nY2 > yScrn)
        nY2 = yScrn;
 
	nWidth = nX2 - nX;
	nHeight = nY2 - nY;
 
    // create a bitmap compatible with the screen DC
    hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
 
    // select new bitmap into memory DC
    hOldBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);
 
    // bitblt screen DC to memory DC
    //BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);
	BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, 0, 0, SRCCOPY);
 
    // select old bitmap back into memory DC and get handle to
    // bitmap of the screen  
    hBitmap = (HBITMAP)  SelectObject(hMemDC, hOldBitmap);
 
 
	Capture(hScrDC, &lpRect, "C:\\res.bmp");

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:

Select allOpen in new window

 

by: pgnatyukPosted on 2009-10-30 at 04:41:43ID: 25701915

Fine. Good start. Let me few minutes to check. I think there is a problem with the coordinates.

 

by: pgnatyukPosted on 2009-10-30 at 04:52:25ID: 25701980

I attached the code. It is Win32 application, empty project.

#include <Windows.h>
 
BOOL Capture(HDC, LPRECT, LPCWSTR);
 
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
					  LPWSTR lpCmdLine, int nCmdShow)
{
	HWND hWnd;
	HDC hDC;
	RECT rect;
 
	hWnd = FindWindow(NULL, L"Calculator");
	if (hWnd != NULL)
	{
		SetForegroundWindow(hWnd);
		GetWindowRect(hWnd , &rect);
		hDC = GetDC(GetDesktopWindow());
 
		if (hDC != NULL)
			Capture(hDC, &rect, L"capture.bmp");
	}
	return 0;
}
 
BOOL Capture(HDC hDC, LPRECT lpRect, LPCWSTR lpszFile)
{
	BITMAPINFO bmi = { 0 };
 
	LPBYTE pBits = NULL;
	bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bmi.bmiHeader.biWidth = lpRect->right - lpRect->left;
	bmi.bmiHeader.biHeight = lpRect->bottom - lpRect->top;
	bmi.bmiHeader.biPlanes = 1;
	bmi.bmiHeader.biBitCount = 32;
	bmi.bmiHeader.biCompression = BI_RGB;
	HBITMAP hBitmap = CreateDIBSection(hDC, &bmi, 
		DIB_RGB_COLORS, (LPVOID*)&pBits, NULL, 0);
 
	HDC hMemDC = CreateCompatibleDC(hDC);
	HGDIOBJ hOld = SelectObject(hMemDC, hBitmap);
	BitBlt(hMemDC, 0, 0, 
		lpRect->right - lpRect->left,
		lpRect->bottom - lpRect->top,
		hDC, lpRect->left, lpRect->top, SRCCOPY);
	SelectObject(hMemDC, hOld);
	DeleteDC(hMemDC);
 
	DWORD nImageSize = ((((bmi.bmiHeader.biWidth * 
		bmi.bmiHeader.biBitCount) 
		+ 31) & ~31) >> 3) 
		* bmi.bmiHeader.biHeight;
 
	BITMAPFILEHEADER header = { 0 };
	header.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + 
		(DWORD)sizeof(BITMAPINFOHEADER); 
	header.bfSize = nImageSize + sizeof(BITMAPFILEHEADER) + 
		sizeof(BITMAPINFOHEADER);
	header.bfType = 0x4D42;
 
	HANDLE hFile = CreateFile(lpszFile, GENERIC_WRITE,
		0, NULL, CREATE_ALWAYS, 
		FILE_ATTRIBUTE_NORMAL, NULL);   
 
	if (hFile != INVALID_HANDLE_VALUE) 
	{
		DWORD nWritten = 0;;
		WriteFile(hFile, (LPVOID)&header, 
			sizeof(BITMAPFILEHEADER), 
			&nWritten, NULL);
		WriteFile(hFile, (LPVOID)&bmi, 
			sizeof(BITMAPINFOHEADER), 
			&nWritten, NULL);
		WriteFile(hFile, (LPVOID)pBits, nImageSize, 
			&nWritten, NULL);
		CloseHandle(hFile);
	}
 
	return TRUE;
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:

Select allOpen in new window

 

by: pgnatyukPosted on 2009-10-30 at 04:55:34ID: 25702007

I attached WinMain with one change -  SetForegroundWindow I replaced with ShowWindow(SW_HIDE) and ShowWindow(SW_SHOW)

#include <Windows.h>
 
BOOL Capture(HDC, LPRECT, LPCWSTR);
 
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
					  LPWSTR lpCmdLine, int nCmdShow)
{
	HWND hWnd;
	HDC hDC;
	RECT rect;
 
	hWnd = FindWindow(NULL, L"Calculator");
	if (hWnd != NULL)
	{
		ShowWindow(hWnd, SW_HIDE);
		ShowWindow(hWnd, SW_SHOW);
		Sleep(100);
		GetWindowRect(hWnd , &rect);
		hDC = GetDC(GetDesktopWindow());
 
		if (hDC != NULL)
			Capture(hDC, &rect, L"capture.bmp");
	}
	return 0;
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:

Select allOpen in new window

 

by: CSecurityPosted on 2009-10-30 at 05:00:31ID: 25702033

Thank you so much! It worked, but Show hide of window makes problem, because I'm going to do capturing 5 times a second.... It becomes blinking and I can't work with PC...

 

by: CSecurityPosted on 2009-10-30 at 05:06:21ID: 25702081

Honestly I don't want to save it to FILE

I want only the HBITMAP out of it

 

by: pgnatyukPosted on 2009-10-30 at 05:10:16ID: 25702102

So comment lines with ShowWindow and Sleep.
If you application will be visible, it will work. Otherwise the solution will be not that simple.
If you don't need to save in a file, change Capture function - remove all code related to the file and return HBITMAp from it.
I attached the code.

HBITMAP TakeScreen(HDC hDC, LPRECT lpRect)
{
	BITMAPINFO bmi = { 0 };
 
	LPBYTE pBits = NULL;
	bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bmi.bmiHeader.biWidth = lpRect->right - lpRect->left;
	bmi.bmiHeader.biHeight = lpRect->bottom - lpRect->top;
	bmi.bmiHeader.biPlanes = 1;
	bmi.bmiHeader.biBitCount = 32;
	bmi.bmiHeader.biCompression = BI_RGB;
	HBITMAP hBitmap = CreateDIBSection(hDC, &bmi, 
		DIB_RGB_COLORS, (LPVOID*)&pBits, NULL, 0);
 
	HDC hMemDC = CreateCompatibleDC(hDC);
	HGDIOBJ hOld = SelectObject(hMemDC, hBitmap);
	BitBlt(hMemDC, 0, 0, 
		lpRect->right - lpRect->left,
		lpRect->bottom - lpRect->top,
		hDC, lpRect->left, lpRect->top, SRCCOPY);
	SelectObject(hMemDC, hOld);
	DeleteDC(hMemDC);
	return hBitmap;
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:

Select allOpen in new window

 

by: pgnatyukPosted on 2009-10-30 at 05:19:18ID: 25702169

Full code below

#include <Windows.h>
 
HBITMAP TakeScreen(HDC hDC, LPRECT lpRect);
 
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
					  LPWSTR lpCmdLine, int nCmdShow)
{
	HWND hWnd;
	HDC hDC;
	RECT rect;
 
	hWnd = FindWindow(NULL, L"Calculator");
	if (hWnd != NULL)
	{
		SetForegroundWindow(hWnd);
		ShowWindow(hWnd, SW_HIDE);
		ShowWindow(hWnd, SW_SHOWNORMAL);
		Sleep(100);
		GetWindowRect(hWnd , &rect);
		hDC = GetDC(GetDesktopWindow());
		HBITMAP hBmp = TakeScreen(hDC, &rect);
		if (hBmp != NULL) //Delete this HBITMAP only when you don't need it
			::DeleteObject(hBmp);
	}
	return 0;
}
 
HBITMAP TakeScreen(HDC hDC, LPRECT lpRect)
{
	BITMAPINFO bmi = { 0 };
 
	LPBYTE pBits = NULL;
	bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bmi.bmiHeader.biWidth = lpRect->right - lpRect->left;
	bmi.bmiHeader.biHeight = lpRect->bottom - lpRect->top;
	bmi.bmiHeader.biPlanes = 1;
	bmi.bmiHeader.biBitCount = 32;
	bmi.bmiHeader.biCompression = BI_RGB;
	HBITMAP hBitmap = CreateDIBSection(hDC, &bmi, 
		DIB_RGB_COLORS, (LPVOID*)&pBits, NULL, 0);
 
	HDC hMemDC = CreateCompatibleDC(hDC);
	HGDIOBJ hOld = SelectObject(hMemDC, hBitmap);
	BitBlt(hMemDC, 0, 0, 
		lpRect->right - lpRect->left,
		lpRect->bottom - lpRect->top,
		hDC, lpRect->left, lpRect->top, SRCCOPY);
	SelectObject(hMemDC, hOld);
	DeleteDC(hMemDC);
	return hBitmap;
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:

Select allOpen in new window

 

by: CSecurityPosted on 2009-10-30 at 05:25:35ID: 25702216

Thank you so much... Last question, could you write HBITMAP to a byte array, I mean this:

HBITMAP TakeScreen(HDC hDC, LPRECT lpRect, BYTE *pData)

Is it possible?

 

by: TosterPosted on 2009-10-30 at 05:32:36ID: 25702265

Isn't it already a byte array?

 

by: pgnatyukPosted on 2009-10-30 at 05:35:50ID: 25702292

You have the bit array. it is LPBYTE pBits. Its size is:
DWORD nImageSize = ((((bmi.bmiHeader.biWidth *
            bmi.bmiHeader.biBitCount)
            + 31) & ~31) >> 3)
            * bmi.bmiHeader.biHeight;
32 bits per pixel.

 

by: CSecurityPosted on 2009-10-30 at 05:40:38ID: 25702314

I want DIBbits like this:

GetDIBits(hScrDC, hBitmap, 0, nHeight, pData, pHeader, DIB_RGB_COLORS);

but can't get it working

 

by: pgnatyukPosted on 2009-10-30 at 05:52:22ID: 25702388

You got it - attached
pBits it's exactly GetDIBits.

You can call GetDIBits for hBmp, if you don't trust me. :)
GetDIBits(hDC, hBmp, 0, 1, NULL, (BITMAPINFO*)pbi,  DIB_RGB_COLORS);  

#include <Windows.h>
 
BOOL TakeScreen(HDC hDC, LPRECT lpRect, HBITMAP& hBitmap, LPBYTE& pByte, DWORD& nImageSize);
 
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
					  LPWSTR lpCmdLine, int nCmdShow)
{
	HWND hWnd;
	HDC hDC;
	RECT rect;
 
	hWnd = FindWindow(NULL, L"Calculator");
	if (hWnd != NULL)
	{
		SetForegroundWindow(hWnd);
		ShowWindow(hWnd, SW_HIDE);
		ShowWindow(hWnd, SW_SHOWNORMAL);
		Sleep(100);
		GetWindowRect(hWnd , &rect);
		hDC = GetDC(GetDesktopWindow());
		HBITMAP hBmp = NULL;
		LPBYTE pBits = NULL;
		DWORD nSize = 0;
		if (TakeScreen(hDC, &rect, hBmp, pBits, nSize))
		{
			//here is your code that will work with the image
			::DeleteObject(hBmp);
		}
	}
	return 0;
}
 
BOOL TakeScreen(HDC hDC, LPRECT lpRect, HBITMAP& hBitmap, LPBYTE& pBits, DWORD& nImageSize)
{
	BITMAPINFO bmi = { 0 };
 
	bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bmi.bmiHeader.biWidth = lpRect->right - lpRect->left;
	bmi.bmiHeader.biHeight = lpRect->bottom - lpRect->top;
	bmi.bmiHeader.biPlanes = 1;
	bmi.bmiHeader.biBitCount = 32;
	bmi.bmiHeader.biCompression = BI_RGB;
	hBitmap = CreateDIBSection(hDC, &bmi, 
		DIB_RGB_COLORS, (LPVOID*)&pBits, NULL, 0);
 
	if (hBitmap == NULL)
		return FALSE;
 
	HDC hMemDC = CreateCompatibleDC(hDC);
	HGDIOBJ hOld = SelectObject(hMemDC, hBitmap);
	BitBlt(hMemDC, 0, 0, 
		lpRect->right - lpRect->left,
		lpRect->bottom - lpRect->top,
		hDC, lpRect->left, lpRect->top, SRCCOPY);
	SelectObject(hMemDC, hOld);
	DeleteDC(hMemDC);
 
	nImageSize = ((((bmi.bmiHeader.biWidth * 
		bmi.bmiHeader.biBitCount) 
		+ 31) & ~31) >> 3) 
		* bmi.bmiHeader.biHeight;
 
	return TRUE;
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:

Select allOpen in new window

 

by: CSecurityPosted on 2009-10-30 at 05:54:45ID: 25702404

I'm passing this:

VIDEOINFOHEADER *pVih = (VIDEOINFOHEADER*)m_mt.pbFormat;
hDib = TakeScreen(hDC, &rect, pData, (BITMAPINFO *)&(pVih->bmiHeader));

Do you think it's compression problem and your code doesn't read my BITMAPHEADER ?

 

by: CSecurityPosted on 2009-10-30 at 05:55:37ID: 25702415

I can't get image stream, that's why I say...

See atached code

I also removed GetDIBits, but again no luck

HBITMAP TakeScreen(HDC hDC, LPRECT lpRect, BYTE *pBits, BITMAPINFO *pHeader)
{
	BITMAPINFO bmi = { 0 };
 
	bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bmi.bmiHeader.biWidth = lpRect->right - lpRect->left;
	bmi.bmiHeader.biHeight = lpRect->bottom - lpRect->top;
	bmi.bmiHeader.biPlanes = 1;
	bmi.bmiHeader.biBitCount = 32;
	bmi.bmiHeader.biCompression = BI_RGB;
	HBITMAP hBitmap = CreateDIBSection(hDC, &bmi, 
		DIB_RGB_COLORS, (LPVOID*)&pBits, NULL, 0);
 
	HDC hMemDC = CreateCompatibleDC(hDC);
	HGDIOBJ hOld = SelectObject(hMemDC, hBitmap);
	BitBlt(hMemDC, 0, 0, 
		lpRect->right - lpRect->left,
		lpRect->bottom - lpRect->top,
		hDC, lpRect->left, lpRect->top, SRCCOPY);
	SelectObject(hMemDC, hOld);
 
		DWORD nImageSize = ((((bmi.bmiHeader.biWidth * 
		bmi.bmiHeader.biBitCount) 
		+ 31) & ~31) >> 3) 
		* bmi.bmiHeader.biHeight;
	BITMAPFILEHEADER header = { 0 };
	header.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + 
		(DWORD)sizeof(BITMAPINFOHEADER); 
	header.bfSize = nImageSize + sizeof(BITMAPFILEHEADER) + 
		sizeof(BITMAPINFOHEADER);
	header.bfType = 0x4D42;
 
	GetDIBits(hDC, hBitmap, 0, bmi.bmiHeader.biHeight, pBits, &bmi, DIB_RGB_COLORS);
	DeleteDC(hMemDC);
	return hBitmap;
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:

Select allOpen in new window

 

by: CSecurityPosted on 2009-10-30 at 06:16:25ID: 25702606

A question...

What's wrong in my original code?

It was working, but now it doesn't when I did the modifications

HBITMAP CopyScreenToBitmap(LPRECT lpRect, BYTE *pData, BITMAPINFO *pHeader)
{
    HDC         hScrDC, hMemDC;         // screen DC and memory DC
    HBITMAP     hBitmap, hOldBitmap;    // handles to deice-dependent bitmaps
    int         nX, nY, nX2, nY2;       // coordinates of rectangle to grab
    int         nWidth, nHeight;        // DIB width and height
    int         xScrn, yScrn;           // screen resolution
 
 
 
	HWND hwnd;
	hwnd = FindWindow(NULL, "Calculator");
	GetWindowRect(hwnd , lpRect);
	hScrDC = GetDC(GetDesktopWindow());
	hMemDC = CreateCompatibleDC(hScrDC);
 
	nWidth = lpRect->right - lpRect->left;
	nHeight = lpRect->bottom - lpRect->top;
 
	char tmp[200];
	sprintf(tmp, "Width: %d\r\nHeight: %d\r\n", nWidth, nHeight);
	FILE *f;
	f = fopen("C:\\Res.txt", "a+");
	fwrite(tmp, strlen(tmp), 1, f);
	fclose(f);
 
    // create a bitmap compatible with the screen DC
    hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
 
    // select new bitmap into memory DC
    hOldBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);
 
    // bitblt screen DC to memory DC
    //BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);
	BitBlt(hMemDC, 0, 0, lpRect->right - lpRect->left,	lpRect->bottom - lpRect->top, hScrDC, lpRect->left, lpRect->top, SRCCOPY);
 
    // select old bitmap back into memory DC and get handle to
    // bitmap of the screen  
    hBitmap = (HBITMAP)  SelectObject(hMemDC, hOldBitmap);
 
    // Copy the bitmap data into the provided BYTE buffer
    GetDIBits(hScrDC, hBitmap, 0, nHeight, pData, pHeader, DIB_RGB_COLORS);
 
    // clean up
    DeleteDC(hScrDC);
    DeleteDC(hMemDC);
 
    // return handle to the bitmap
    return hBitmap;
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:

Select allOpen in new window

 

by: pgnatyukPosted on 2009-10-30 at 06:44:07ID: 25702844

BitBlt is commented

 

by: pgnatyukPosted on 2009-10-30 at 06:45:45ID: 25702859

hBitmap = (HBITMAP)  SelectObject(hMemDC, hOldBitmap);
This line is a mistake

 

by: pgnatyukPosted on 2009-10-30 at 06:47:54ID: 25702883

I can change my TakeScreen and give BITNAPINFO from there. You can also (with my code) - there is a local variable BITMAPINFO, make it as one more parameter BITMAPINFO& mbi and comment this local variable.

 

by: CSecurityPosted on 2009-10-30 at 07:11:53ID: 25703102

What I should do instead of :

hBitmap = (HBITMAP)  SelectObject(hMemDC, hOldBitmap);

 

by: pgnatyukPosted on 2009-10-30 at 07:13:45ID: 25703118

SelectObject(hMemDC, hOldBitmap);
and do not forget about BitBlt.

 

by: CSecurityPosted on 2009-10-30 at 07:29:41ID: 25703282

I really don't understand!!!

What's wrong now?

Still I don't get any stream.... :((

HBITMAP CopyScreenToBitmap(LPRECT lpRect, BYTE *pData, BITMAPINFO *pHeader)
{
    HDC         hScrDC, hMemDC;         // screen DC and memory DC
    HBITMAP     hBitmap, hOldBitmap;    // handles to deice-dependent bitmaps
    int         nX, nY, nX2, nY2;       // coordinates of rectangle to grab
    int         nWidth, nHeight;        // DIB width and height
    int         xScrn, yScrn;           // screen resolution
 
 
 
	HWND hwnd;
	hwnd = FindWindow(NULL, "Calculator");
	GetWindowRect(hwnd , lpRect);
	hScrDC = GetDC(GetDesktopWindow());
	hMemDC = CreateCompatibleDC(hScrDC);
 
	nWidth = lpRect->right - lpRect->left;
	nHeight = lpRect->bottom - lpRect->top;
 
    // create a bitmap compatible with the screen DC
    hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
 
    // select new bitmap into memory DC
    hOldBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);
 
    // bitblt screen DC to memory DC
    //BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);
	BitBlt(hMemDC, 0, 0, lpRect->right - lpRect->left,	lpRect->bottom - lpRect->top, hScrDC, lpRect->left, lpRect->top, SRCCOPY);
	SelectObject(hMemDC, hOldBitmap);
 
    // Copy the bitmap data into the provided BYTE buffer
    GetDIBits(hScrDC, hBitmap, 0, nHeight, pData, pHeader, DIB_RGB_COLORS);
 
    // clean up
    DeleteDC(hScrDC);
    DeleteDC(hMemDC);
 
    // return handle to the bitmap
    return hBitmap;
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:

Select allOpen in new window

 

by: pgnatyukPosted on 2009-10-30 at 07:33:34ID: 25703320

Because you create a compatible bitmap and do not copy anything onto it. I've told you - uncomment BitBlt:
BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);

 

by: pgnatyukPosted on 2009-10-30 at 07:34:38ID: 25703332

It is not a stream. It is an image copied from your desktop.

 

by: pgnatyukPosted on 2009-10-30 at 07:36:05ID: 25703354

sorry. you have uncommented. Let me check

 

by: CSecurityPosted on 2009-10-30 at 07:37:46ID: 25703373

Also here is mine:

It doesn't work either... But I'm 100% sure in this code one of these possiblities causes error, otherwise it works.

a) maybe image size (height width, etc) problem

b) API calls bitblt etc problem

HBITMAP CopyScreenToBitmap(LPRECT lpRect, BYTE *pData, BITMAPINFO *pHeader)
{
    HDC         hScrDC, hMemDC;         // screen DC and memory DC
    HBITMAP     hBitmap, hOldBitmap;    // handles to deice-dependent bitmaps
    int         nX, nY, nX2, nY2;       // coordinates of rectangle to grab
    int         nWidth, nHeight;        // DIB width and height
    int         xScrn, yScrn;           // screen resolution
 
	
	HWND hwnd = FindWindow("SciCalc", "Calculator");
	GetWindowRect(hwnd , lpRect);
	hScrDC = GetDC(GetDesktopWindow());
	hMemDC = CreateCompatibleDC(hScrDC);
 
	nWidth = lpRect->right - lpRect->left;
	nHeight = lpRect->bottom - lpRect->top;
 
    // create a bitmap compatible with the screen DC
    hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
 
    // select new bitmap into memory DC
    hOldBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);
 
    // bitblt screen DC to memory DC
    //BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);
	BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, 0, 0, SRCCOPY);
 
    // select old bitmap back into memory DC and get handle to
    // bitmap of the screen  
    hBitmap = (HBITMAP)  SelectObject(hMemDC, hOldBitmap);
 
    // Copy the bitmap data into the provided BYTE buffer
    GetDIBits(hScrDC, hBitmap, 0, nHeight, pData, pHeader, DIB_RGB_COLORS);
 
    // clean up
    DeleteDC(hScrDC);
    DeleteDC(hMemDC);
 
    // return handle to the bitmap
    return hBitmap;
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:

Select allOpen in new window

 

by: pgnatyukPosted on 2009-10-30 at 07:39:04ID: 25703382

Why you think it does not work?
HBITMAP CopyScreenToBitmap(LPRECT lpRect, BYTE *pData, BITMAPINFO *pHeader)
You call the funtions and then the parameters pData, pHeader are NULL?
so change:
HBITMAP CopyScreenToBitmap(LPRECT lpRect, BYTE*& pData, BITMAPINFO*& pHeader)
In this case will be enough:
HBITMAP CopyScreenToBitmap(LPRECT lpRect);
GetDIBits you can call from the caller.

 

by: pgnatyukPosted on 2009-10-30 at 07:41:34ID: 25703405

BitBlt(hMemDC, lpRect->left, lpRect->top, nWidth, nHeight, hScrDC, 0, 0, SRCCOPY);
SelectObject(hMemDC, hOldBitmap);
 
it should be that way. You took an image from 0,0

 

by: pgnatyukPosted on 2009-10-30 at 07:42:53ID: 25703412

hBitmap = (HBITMAP) SelectObject(hMemDC, hOldBitmap);
Exactly this line makes the biggest trouble.
It HAS to be

SelectObject(hMemDC, hOldBitmap);

 

by: CSecurityPosted on 2009-10-30 at 07:49:18ID: 25703479

Did what you said...

Still no luck

HBITMAP CopyScreenToBitmap(LPRECT lpRect, BYTE *pData, BITMAPINFO *pHeader)
{
    HDC         hScrDC, hMemDC;         // screen DC and memory DC
    HBITMAP     hBitmap, hOldBitmap;    // handles to deice-dependent bitmaps
    int         nX, nY, nX2, nY2;       // coordinates of rectangle to grab
    int         nWidth, nHeight;        // DIB width and height
    int         xScrn, yScrn;           // screen resolution
 
	
	HWND hwnd = FindWindow("SciCalc", "Calculator");
	GetWindowRect(hwnd , lpRect);
	hScrDC = GetDC(GetDesktopWindow());
	hMemDC = CreateCompatibleDC(hScrDC);
 
	nWidth = lpRect->right - lpRect->left;
	nHeight = lpRect->bottom - lpRect->top;
	
    // create a bitmap compatible with the screen DC
    hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
 
    // select new bitmap into memory DC
	hOldBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);
 
    // bitblt screen DC to memory DC
    //BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);
	//BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, 0, 0, SRCCOPY);
	BitBlt(hMemDC, lpRect->left, lpRect->top, nWidth, nHeight, hScrDC, 0, 0, SRCCOPY);
 
    // select old bitmap back into memory DC and get handle to
    // bitmap of the screen  
    SelectObject(hMemDC, hOldBitmap);
 
    // Copy the bitmap data into the provided BYTE buffer
    GetDIBits(hScrDC, hBitmap, 0, nHeight, pData, pHeader, DIB_RGB_COLORS);
 
    // clean up
    DeleteDC(hScrDC);
    DeleteDC(hMemDC);
 
    // return handle to the bitmap
    return hBitmap;
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:

Select allOpen in new window

 

by: pgnatyukPosted on 2009-10-30 at 08:12:14ID: 25703742

Try now

#include <Windows.h>
 
HBITMAP CopyScreenToBitmap(LPRECT lpRect, BYTE*& pData, BITMAPINFO* pHeader);
BOOL Save(LPCWSTR lpszFile, LPBYTE pBits, LPBITMAPINFO pHeader);
 
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
					  LPWSTR lpCmdLine, int nCmdShow)
{
	HWND hWnd;
	RECT rect;
 
	LPBYTE pData = NULL;
	BITMAPINFO header = { 0 };
	HBITMAP hBitmap = CopyScreenToBitmap(&rect, pData, &header);
	if (hBitmap)
	{
	//	Save(L"cap_screen.bmp", pData, &header);
		::DeleteObject(hBitmap);
	}
	return 0;
}
 
HBITMAP CopyScreenToBitmap(LPRECT lpRect, BYTE*& pData, LPBITMAPINFO pHeader)
{
	HDC         hScrDC, hMemDC;         // screen DC and memory DC
	HBITMAP     hBitmap, hOldBitmap;    // handles to deice-dependent bitmaps
	int         nX, nY, nX2, nY2;       // coordinates of rectangle to grab
	int         nWidth, nHeight;        // DIB width and height
	int         xScrn, yScrn;           // screen resolution
 
 
	HWND hwnd = FindWindow(L"SciCalc", L"Calculator");
	SetForegroundWindow(hwnd);
	ShowWindow(hwnd, SW_HIDE);
	ShowWindow(hwnd, SW_SHOWNORMAL);
	Sleep(100);
	GetWindowRect(hwnd , lpRect);
 
	hScrDC = GetDC(GetDesktopWindow());
	hMemDC = CreateCompatibleDC(hScrDC);
 
	nWidth = lpRect->right - lpRect->left;
	nHeight = lpRect->bottom - lpRect->top;
 
	pHeader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	pHeader->bmiHeader.biWidth = nWidth;
	pHeader->bmiHeader.biHeight = nHeight;
	pHeader->bmiHeader.biPlanes = 1;
	pHeader->bmiHeader.biBitCount = 32;
	pHeader->bmiHeader.biCompression = BI_RGB;
	hBitmap = CreateDIBSection(hScrDC, pHeader, 
		DIB_RGB_COLORS, (LPVOID*)&pData, NULL, 0);
 
	// select new bitmap into memory DC
	hOldBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);
 
	BitBlt(hMemDC, 0, 0, 
		lpRect->right - lpRect->left,
		lpRect->bottom - lpRect->top,
		hScrDC, lpRect->left, lpRect->top, SRCCOPY);
 
	// select old bitmap back into memory DC and get handle to
	// bitmap of the screen  
	SelectObject(hMemDC, hOldBitmap);
 
	// clean up
	DeleteDC(hMemDC);
	ReleaseDC(GetDesktopWindow(), hScrDC);
 
	// return handle to the bitmap
	return hBitmap;
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:

Select allOpen in new window

 

by: CSecurityPosted on 2009-10-30 at 08:23:57ID: 25703877

But it doesn't write anything to Pdata byte array

 

by: CSecurityPosted on 2009-10-30 at 08:25:05ID: 25703889

Or if it writes... I can't see anything in stream I get out of it

 

by: pgnatyukPosted on 2009-10-30 at 08:33:37ID: 25703971

the attached app just works

#include <Windows.h>
 
HBITMAP CopyScreenToBitmap(LPRECT lpRect, LPBYTE& pData, LPBITMAPINFO pHeader);
BOOL Save(LPCWSTR lpszFile, LPBYTE pBits, LPBITMAPINFO pHeader);
 
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
					  LPWSTR lpCmdLine, int nCmdShow)
{
	HWND hWnd;
	RECT rect;
 
	LPBYTE pData = NULL;
	BITMAPINFO header = { 0 };
	HBITMAP hBitmap = CopyScreenToBitmap(&rect, pData, &header);
	if (hBitmap)
	{
		Save(L"cap_screen.bmp", pData, &header);
		::DeleteObject(hBitmap);
	}
	return 0;
}
 
HBITMAP CopyScreenToBitmap(LPRECT lpRect, LPBYTE& pData, LPBITMAPINFO pHeader)
{
	HDC         hScrDC, hMemDC;         // screen DC and memory DC
	HBITMAP     hBitmap, hOldBitmap;    // handles to deice-dependent bitmaps
	int         nX, nY, nX2, nY2;       // coordinates of rectangle to grab
	int         nWidth, nHeight;        // DIB width and height
	int         xScrn, yScrn;           // screen resolution
 
 
	HWND hwnd = FindWindow(L"SciCalc", L"Calculator");
	SetForegroundWindow(hwnd);
	ShowWindow(hwnd, SW_HIDE);
	ShowWindow(hwnd, SW_SHOWNORMAL);
	Sleep(100);
	GetWindowRect(hwnd , lpRect);
 
	hScrDC = GetDC(GetDesktopWindow());
	hMemDC = CreateCompatibleDC(hScrDC);
 
	nWidth = lpRect->right - lpRect->left;
	nHeight = lpRect->bottom - lpRect->top;
 
	pHeader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	pHeader->bmiHeader.biWidth = nWidth;
	pHeader->bmiHeader.biHeight = nHeight;
	pHeader->bmiHeader.biPlanes = 1;
	pHeader->bmiHeader.biBitCount = 32;
	pHeader->bmiHeader.biCompression = BI_RGB;
	hBitmap = CreateDIBSection(hScrDC, pHeader, 
		DIB_RGB_COLORS, (LPVOID*)&pData, NULL, 0);
 
	// select new bitmap into memory DC
	hOldBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);
 
	BitBlt(hMemDC, 0, 0, 
		lpRect->right - lpRect->left,
		lpRect->bottom - lpRect->top,
		hScrDC, lpRect->left, lpRect->top, SRCCOPY);
 
 
	// select old bitmap back into memory DC and get handle to
	// bitmap of the screen  
	SelectObject(hMemDC, hOldBitmap);
	// clean up
	DeleteDC(hMemDC);
 
	ReleaseDC(GetDesktopWindow(), hScrDC);
	// return handle to the bitmap
	return hBitmap;
}
 
BOOL Save(LPCWSTR lpszFile, LPBYTE pBits, LPBITMAPINFO pHeader)
{
	DWORD nImageSize = ((((pHeader->bmiHeader.biWidth * 
		pHeader->bmiHeader.biBitCount) 
		+ 31) & ~31) >> 3) 
		* pHeader->bmiHeader.biHeight;
 
	BITMAPFILEHEADER header = { 0 };
	header.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + 
		(DWORD)sizeof(BITMAPINFOHEADER); 
	header.bfSize = nImageSize + sizeof(BITMAPFILEHEADER) + 
		sizeof(BITMAPINFOHEADER);
	header.bfType = 0x4D42;
 
	HANDLE hFile = CreateFile(lpszFile, GENERIC_WRITE,
		0, NULL, CREATE_ALWAYS, 
		FILE_ATTRIBUTE_NORMAL, NULL);   
 
	if (hFile != INVALID_HANDLE_VALUE) 
	{
		DWORD nWritten = 0;;
		WriteFile(hFile, (LPVOID)&header, 
			sizeof(BITMAPFILEHEADER), 
			&nWritten, NULL);
		WriteFile(hFile, (LPVOID)pHeader, 
			sizeof(BITMAPINFOHEADER), 
			&nWritten, NULL);
		WriteFile(hFile, (LPVOID)pBits, nImageSize, 
			&nWritten, NULL);
		CloseHandle(hFile);
	}
 
	return TRUE;
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:

Select allOpen in new window

 

by: CSecurityPosted on 2009-10-30 at 08:38:10ID: 25704012

Wait...

Look at this code...

It streams image, but a little part of calculator if I move it to bottom of page close to Start Menu Button, I mean left close to 0 and bottom close to END.

What coordinates I should modify ONLY in THIS code to get it 100% working... Otherwise all thing in this code works...

HBITMAP CopyScreenToBitmap(LPRECT lpRect, BYTE*& pData, LPBITMAPINFO pHeader)
{
	HDC         hScrDC, hMemDC;         // screen DC and memory DC
    HBITMAP     hBitmap, hOldBitmap;    // handles to deice-dependent bitmaps
    int         nX, nY, nX2, nY2;       // coordinates of rectangle to grab
    int         nWidth, nHeight;        // DIB width and height
    int         xScrn, yScrn;           // screen resolution
 
	HWND hwnd = FindWindow("SciCalc", "Calculator");
	GetWindowRect(hwnd , lpRect);
	hScrDC = GetDC(hwnd);
	hMemDC = CreateCompatibleDC(hScrDC);      // get points of re
	
 
    // get points of rectangle to grab
    nX  = lpRect->left;
    nY  = lpRect->top;
    nX2 = lpRect->right;
    nY2 = lpRect->bottom;
	
    // get screen resolution
    xScrn = GetDeviceCaps(hScrDC, HORZRES);
    yScrn = GetDeviceCaps(hScrDC, VERTRES);
 
    //make sure bitmap rectangle is visible
    if (nX < 0)
        nX = 0;
    if (nY < 0)
        nY = 0;
    if (nX2 > xScrn)
        nX2 = xScrn;
    if (nY2 > yScrn)
        nY2 = yScrn;
 
	nWidth = nX2 - nX;
	nHeight = nY2 - nY;
 
    // create a bitmap compatible with the screen DC
    hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
 
    // select new bitmap into memory DC
    hOldBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);
 
    // bitblt screen DC to memory DC
    BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, 0, 0, SRCCOPY);
 
    // select old bitmap back into memory DC and get handle to
    // bitmap of the screen  
    hBitmap = (HBITMAP)  SelectObject(hMemDC, hOldBitmap);
 
    // Copy the bitmap data into the provided BYTE buffer
    GetDIBits(hScrDC, hBitmap, 0, nHeight, pData, pHeader, DIB_RGB_COLORS);
 
    // clean up
    DeleteDC(hScrDC);
    DeleteDC(hMemDC);
 
    // return handle to the bitmap
    return hBitmap;
 
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:

Select allOpen in new window

 

by: pgnatyukPosted on 2009-10-30 at 08:39:42ID: 25704027

The file

 

by: CSecurityPosted on 2009-10-30 at 08:40:18ID: 25704035

As I said I don't want to save it, I'm using those images to stream... So my code above works and captures part of calculator if I move it close to start menu... If you solve this one, EVERYTHING would be DONE... And so far, thank you so much, really thanks for your help

 

by: pgnatyukPosted on 2009-10-30 at 08:40:25ID: 25704037

This code works with GetDIBis

BOOL SaveMemDCToBitmapNoPalete(LPCSTR lpszFileName, HDC hMemDC,HBITMAP hBitmap)
{
	int   wBitCount = 32;
 
	BITMAP   Bitmap;  
	BITMAPFILEHEADER   bmfHdr;    
	BITMAPINFOHEADER   bi;
 
	GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap); 
 
	bi.biSize = sizeof(BITMAPINFOHEADER);    
	bi.biWidth = Bitmap.bmWidth;    
	bi.biHeight = Bitmap.bmHeight;  
	bi.biPlanes = 1;    
	bi.biBitCount = wBitCount;    
	bi.biCompression = BI_RGB;    
	bi.biSizeImage = 0;  
	bi.biXPelsPerMeter = 0;    
	bi.biYPelsPerMeter = 0;    
	bi.biClrUsed = 0;    
	bi.biClrImportant = 0; 
 
	DWORD dwBmBitsSize = ((Bitmap.bmWidth * wBitCount + 31) / 32) * 4 * Bitmap.bmHeight; 
 
	HANDLE hDib = GlobalAlloc(GHND,dwBmBitsSize+sizeof(BITMAPINFOHEADER)); 
	LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);    
	*lpbi = bi;
 
	GetDIBits(hMemDC, hBitmap, 0, (UINT)Bitmap.bmHeight,  
		(LPSTR)lpbi + sizeof(BITMAPINFOHEADER), (BITMAPINFO *)lpbi, DIB_RGB_COLORS);    
 
	HANDLE fh = CreateFile(lpszFileName,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,
		FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);   
	ASSERT(fh != INVALID_HANDLE_VALUE);
 
	DWORD dwDIBSize   =   sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwBmBitsSize;
 
	bmfHdr.bfType = 0x4D42;    
	bmfHdr.bfSize = dwDIBSize;  
	bmfHdr.bfReserved1 = 0;    
	bmfHdr.bfReserved2 = 0;    
	bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);    
 
	DWORD dwWritten;
 
	WriteFile(fh,   (LPSTR)&bmfHdr,   sizeof(BITMAPFILEHEADER),   &dwWritten,   NULL);    
	WriteFile(fh, (LPSTR)lpbi, dwDIBSize, &dwWritten, NULL);    
	GlobalUnlock(hDib);    
	GlobalFree(hDib);    
	CloseHandle(fh);
 
	return TRUE;
 
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:

Select allOpen in new window

 

by: pgnatyukPosted on 2009-10-30 at 08:43:23ID: 25704070

It streams image, but a little part of calculator if I move it to bottom of page close to Start Menu Button, I mean left close to 0 and bottom close to END.

I didn't understand what you mean.

There is a window with the top most style that covers the calculator?

 

by: CSecurityPosted on 2009-10-30 at 08:45:07ID: 25704093

The code I sent above streams image from start button if I move calculator close to start button. Only then I get some images otherwise I get BLACK image. If you modify that to capture correctly calculator window image, we are done

 

by: pgnatyukPosted on 2009-10-30 at 08:49:05ID: 25704134

:) I still don't understand.
With the code I posted I receive the calculator screenshot each time if this window is not covered by a topmost window such as TaskManager.

 

by: pgnatyukPosted on 2009-10-30 at 08:53:19ID: 25704173

Code from ID:25703971 works.
About which code you are talking?

 

by: CSecurityPosted on 2009-10-30 at 08:55:38ID: 25704208

Could you give me your email, I'll email you entire project

 

by: pgnatyukPosted on 2009-10-30 at 08:58:02ID: 25704233

No. It's forbidden here. The question and the answer should be here. Moderators will delete the comment with my email.

 

by: pgnatyukPosted on 2009-10-30 at 08:59:26ID: 25704251

Post the code and put a comment near the line you are talking about.

 

by: CSecurityPosted on 2009-10-30 at 09:03:44ID: 25704293

SO get this code...

http://tmhare.mvps.org/downloads/vcam.zip

And here is modified filters.cpp

Compile it, register the ax file, open calculator, then open AmCap... See what happens... You'll see how funny is result

 

by: pgnatyukPosted on 2009-10-30 at 09:05:19ID: 25704312

Nice. :) I got it.

 

by: pgnatyukPosted on 2009-10-30 at 09:12:28ID: 25704380

Filters.cpp(4) : fatal error C1083: Cannot open include file: 'streams.h': No such file or directory

If you say it works...
I attached the code that looks suspicious.

Why you need all this check? Remove or comment it and test.

   // get points of rectangle to grab
    nX  = lpRect->left;
    nY  = lpRect->top;
    nX2 = lpRect->right;
    nY2 = lpRect->bottom;
 
    // get screen resolution
    xScrn = GetDeviceCaps(hScrDC, HORZRES);
    yScrn = GetDeviceCaps(hScrDC, VERTRES);
 
    //make sure bitmap rectangle is visible
    if (nX < 0)
        nX = 0;
    if (nY < 0)
        nY = 0;
    if (nX2 > xScrn)
        nX2 = xScrn;
    if (nY2 > yScrn)
        nY2 = yScrn;

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:

Select allOpen in new window

 

by: CSecurityPosted on 2009-10-30 at 09:15:29ID: 25704411

include Platform SDK's DirectShow Baseclasses folder, streams.h is there

I removed those codes, but still no luck

 

by: pgnatyukPosted on 2009-10-30 at 09:16:42ID: 25704418

can you show me a broken image?

 

by: CSecurityPosted on 2009-10-30 at 09:18:17ID: 25704435

Here

 

by: CSecurityPosted on 2009-10-30 at 09:24:17ID: 25704508

By the way... It seems dead image. When I move mouse over buttons buttons become hover, but I can't see that in webcam, just when I move calculator window, I see some changes with bad quality of image

 

by: pgnatyukPosted on 2009-10-30 at 09:25:01ID: 25704517

:(
Part of the calculator was on another monitor? :)
Can I propose to move the calculator window?

 

by: CSecurityPosted on 2009-10-30 at 09:25:16ID: 25704521

The image is taken when calculator was above top corner of screen and close to left corner but not reached it. I can see image just when calculator window is there and as I said it seems dead image... I have no any idea

 

by: pgnatyukPosted on 2009-10-30 at 09:26:09ID: 25704532

Because you copy the desktop. Not the calculator window. You copy what you see on the desktop. hSrcDC is the desktop.

 

by: pgnatyukPosted on 2009-10-30 at 09:29:06ID: 25704563

Try that.

	HWND hwnd = FindWindow(L"SciCalc", L"Calculator");
	SetForegroundWindow(hwnd);
	ShowWindow(hwnd, SW_HIDE);
	ShowWindow(hwnd, SW_SHOWNORMAL);
	SetFocus(hwnd);
	GetWindowRect(hwnd , lpRect);
                                              
1:
2:
3:
4:
5:
6:

Select allOpen in new window

 

by: pgnatyukPosted on 2009-10-30 at 09:29:31ID: 25704571

For test.
To check how it will be.

 

by: CSecurityPosted on 2009-10-30 at 09:32:37ID: 25704602

I changed that to:

hScrDC = GetWindowDC(hwnd);

and

BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nWidth, nHeight, SRCCOPY);

still no luck get similar images

 

by: CSecurityPosted on 2009-10-30 at 09:34:17ID: 25704616

I tested your code, I just got blinking calculator, but no luck

 

by: pgnatyukPosted on 2009-10-30 at 09:35:32ID: 25704631

HWND hwnd = FindWindow(L"SciCalc", L"Calculator");
SetForegroundWindow(hwnd);
GetWindowRect(hwnd , lpRect);
Sleep(10);

For me even this code above works

 

by: CSecurityPosted on 2009-10-30 at 09:37:15ID: 25704648

Nothing changed... You say it worked for you, you mean in webcam???

 

by: pgnatyukPosted on 2009-10-30 at 09:37:29ID: 25704651

Blinking - it's because the calculator needs time to draw itself. Give few ticks.

 

by: CSecurityPosted on 2009-10-30 at 09:38:20ID: 25704662

No, problem isn't blinking... I can't see any image

 

by: pgnatyukPosted on 2009-10-30 at 09:40:22ID: 25704680

no. not webcam. I just capture the calculator window.
If you want you can use my function and the copy pData to your video structure. You know the size.
But I don't think that this is the problem. GDI is not very fast. We can switch to GDI+ with Image, or CImage from ATL. I don't know if it will be faster.

 

by: pgnatyukPosted on 2009-10-30 at 09:43:56ID: 25704714

Are you sure that the problem is not in the code that copies the captured image into the application window? It looks like the ractengle shifted?

 

by: pgnatyukPosted on 2009-10-30 at 09:45:20ID: 25704726

You can compile my code as Win32 application and test - check the files. If the saved images will be correct, we nee to look for an error in other place.

 

by: CSecurityPosted on 2009-10-30 at 09:45:32ID: 25704732

If you have better code to copy bitmap to byte array, I'll test it

 

by: pgnatyukPosted on 2009-10-30 at 09:48:01ID: 25704748

it's one line:
memcpy(your_array, pData, nImageSize)

 

by: CSecurityPosted on 2009-10-30 at 09:49:44ID: 25704762

Do you think the code you gave me which writes it to file, could be changed to write buffer to pData?

If yes, do you think writing BMP file to stream should work? I think what we do now is without BMP header and struct etc . I think that data should be in stream...

Could you make your code above which writes to file to copy data to pData in format which will be streamed?

 

by: pgnatyukPosted on 2009-10-30 at 09:51:42ID: 25704782

In few seconds I will post the code GetDIBits as you like

#include <Windows.h>
 
HBITMAP CopyScreenToBitmap(LPRECT lpRect, LPBYTE& pData, LPBITMAPINFO pHeader);
BOOL Save(LPCWSTR lpszFile, LPBYTE pBits, LPBITMAPINFO pHeader);
 
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
					  LPWSTR lpCmdLine, int nCmdShow)
{
	RECT rect;
 
	LPBYTE pData = NULL;
	BITMAPINFO header = { 0 };
	HBITMAP hBitmap = CopyScreenToBitmap(&rect, pData, &header);
 
	if (hBitmap)
	{
 
		DWORD nImageSize = ((((header.bmiHeader.biWidth * 
			header.bmiHeader.biBitCount) 
			+ 31) & ~31) >> 3) 
			* header.bmiHeader.biHeight;
 
		LPBYTE p = (LPBYTE)malloc(nImageSize);
		memcpy(p, pData, nImageSize);
 
		Save(L"cap_screen.bmp", p, &header);
		::DeleteObject(hBitmap);
 
		free(p);
	}
	return 0;
}
 
HBITMAP CopyScreenToBitmap(LPRECT lpRect, LPBYTE& pData, LPBITMAPINFO pHeader)
{
	HDC         hScrDC, hMemDC;         // screen DC and memory DC
	HBITMAP     hBitmap, hOldBitmap;    // handles to deice-dependent bitmaps
	int         nX, nY, nX2, nY2;       // coordinates of rectangle to grab
	int         nWidth, nHeight;        // DIB width and height
	int         xScrn, yScrn;           // screen resolution
 
 
	HWND hwnd = FindWindow(L"SciCalc", L"Calculator");
	SetForegroundWindow(hwnd);
	GetWindowRect(hwnd , lpRect);
	Sleep(10);
 
	hScrDC = GetDC(GetDesktopWindow());
	hMemDC = CreateCompatibleDC(hScrDC);
 
	nWidth = lpRect->right - lpRect->left;
	nHeight = lpRect->bottom - lpRect->top;
 
	pHeader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	pHeader->bmiHeader.biWidth = nWidth;
	pHeader->bmiHeader.biHeight = nHeight;
	pHeader->bmiHeader.biPlanes = 1;
	pHeader->bmiHeader.biBitCount = 32;
	pHeader->bmiHeader.biCompression = BI_RGB;
	hBitmap = CreateDIBSection(hScrDC, pHeader, 
		DIB_RGB_COLORS, (LPVOID*)&pData, NULL, 0);
 
	// select new bitmap into memory DC
	hOldBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);
 
	BitBlt(hMemDC, 0, 0, 
		lpRect->right - lpRect->left,
		lpRect->bottom - lpRect->top,
		hScrDC, lpRect->left, lpRect->top, SRCCOPY);
 
 
	// select old bitmap back into memory DC and get handle to
	// bitmap of the screen  
	SelectObject(hMemDC, hOldBitmap);
	// clean up
	DeleteDC(hMemDC);
 
	ReleaseDC(GetDesktopWindow(), hScrDC);
	// return handle to the bitmap
	return hBitmap;
}
 
 
BOOL Save(LPCWSTR lpszFile, LPBYTE pBits, LPBITMAPINFO pHeader)
{
	DWORD nImageSize = ((((pHeader->bmiHeader.biWidth * 
		pHeader->bmiHeader.biBitCount) 
		+ 31) & ~31) >> 3) 
		* pHeader->bmiHeader.biHeight;
 
	BITMAPFILEHEADER header = { 0 };
	header.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + 
		(DWORD)sizeof(BITMAPINFOHEADER); 
	header.bfSize = nImageSize + sizeof(BITMAPFILEHEADER) + 
		sizeof(BITMAPINFOHEADER);
	header.bfType = 0x4D42;
 
	HANDLE hFile = CreateFile(lpszFile, GENERIC_WRITE,
		0, NULL, CREATE_ALWAYS, 
		FILE_ATTRIBUTE_NORMAL, NULL);   
 
	if (hFile != INVALID_HANDLE_VALUE) 
	{
		DWORD nWritten = 0;;
		WriteFile(hFile, (LPVOID)&header, 
			sizeof(BITMAPFILEHEADER), 
			&nWritten, NULL);
		WriteFile(hFile, (LPVOID)pHeader, 
			sizeof(BITMAPINFOHEADER), 
			&nWritten, NULL);
		WriteFile(hFile, (LPVOID)pBits, nImageSize, 
			&nWritten, NULL);
		CloseHandle(hFile);
	}
 
	return TRUE;
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:

Select allOpen in new window

 

by: CSecurityPosted on 2009-10-30 at 09:55:23ID: 25704825

in my code, pData have BITMAP header, structure, etc. and data or pure data?

 

by: pgnatyukPosted on 2009-10-30 at 10:03:56ID: 25704899

it is just the bit array. The bitmapinfo is in (BITMAPINFO *)&(pVih->bmiHeader)

 

by: pgnatyukPosted on 2009-10-30 at 10:11:37ID: 25704970

Take a look

#include <Windows.h>
 
HBITMAP CopyScreenToBitmap(LPRECT lpRect);
BOOL Save(LPCWSTR lpszFile, LPBYTE pBits, LPBITMAPINFO pHeader);
 
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
					  LPWSTR lpCmdLine, int nCmdShow)
{
	RECT rect;
 
	HBITMAP hBitmap = CopyScreenToBitmap(&rect);
 
	if (hBitmap)
	{
 
		BITMAP bitmap = { 0 };
		GetObject(hBitmap, sizeof(BITMAP), &bitmap);
		DWORD nImageSize = bitmap.bmWidthBytes * bitmap.bmHeight;
		HDC hDC = GetDC(GetDesktopWindow());
 
		DWORD dwBmBitsSize = ((bitmap.bmWidth * 32 + 31) / 32) * 4 * bitmap.bmHeight; 
 
		HANDLE hDib = GlobalAlloc(GHND,dwBmBitsSize+sizeof(BITMAPINFOHEADER)); 
		LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);    
		BITMAPINFOHEADER header = { 0 };
		header.biSize = sizeof(BITMAPINFOHEADER);
		header.biWidth = bitmap.bmWidth;
		header.biHeight = bitmap.bmHeight;
		header.biPlanes = 1;
		header.biBitCount = 32;
		header.biCompression = BI_RGB;
		*lpbi = header;
 
		GetDIBits(hDC, hBitmap, 0, (UINT)bitmap.bmHeight,  
			(LPSTR)lpbi + sizeof(BITMAPINFOHEADER), (BITMAPINFO *)lpbi, 
			DIB_RGB_COLORS);    
 
		ReleaseDC(GetDesktopWindow(), hDC);
		Save(L"cap_screen.bmp", (LPBYTE)lpbi + sizeof(BITMAPINFOHEADER), (BITMAPINFO *)lpbi);
		GlobalUnlock(hDib);    
		GlobalFree(hDib);    
		::DeleteObject(hBitmap);
	}
	return 0;
}
 
HBITMAP CopyScreenToBitmap(LPRECT lpRect)
{
	HDC         hScrDC, hMemDC;         // screen DC and memory DC
	HBITMAP     hBitmap, hOldBitmap;    // handles to deice-dependent bitmaps
	int         nX, nY, nX2, nY2;       // coordinates of rectangle to grab
	int         nWidth, nHeight;        // DIB width and height
	int         xScrn, yScrn;           // screen resolution
 
 
	HWND hwnd = FindWindow(L"SciCalc", L"Calculator");
	SetForegroundWindow(hwnd);
	GetWindowRect(hwnd , lpRect);
	Sleep(10);
 
	hScrDC = GetDC(GetDesktopWindow());
	hMemDC = CreateCompatibleDC(hScrDC);
 
	nWidth = lpRect->right - lpRect->left;
	nHeight = lpRect->bottom - lpRect->top;
 
	BITMAPINFO header = { 0 };
	LPVOID pData = NULL;
	header.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	header.bmiHeader.biWidth = nWidth;
	header.bmiHeader.biHeight = nHeight;
	header.bmiHeader.biPlanes = 1;
	header.bmiHeader.biBitCount = 32;
	header.bmiHeader.biCompression = BI_RGB;
	hBitmap = CreateDIBSection(hScrDC, &header, 
		DIB_RGB_COLORS, (LPVOID*)&pData, NULL, 0);
 
	// select new bitmap into memory DC
	hOldBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);
 
	BitBlt(hMemDC, 0, 0, 
		lpRect->right - lpRect->left,
		lpRect->bottom - lpRect->top,
		hScrDC, lpRect->left, lpRect->top, SRCCOPY);
 
 
	// select old bitmap back into memory DC and get handle to
	// bitmap of the screen  
	SelectObject(hMemDC, hOldBitmap);
	// clean up
	DeleteDC(hMemDC);
 
	ReleaseDC(GetDesktopWindow(), hScrDC);
	// return handle to the bitmap
	return hBitmap;
}
 
 
BOOL Save(LPCWSTR lpszFile, LPBYTE pBits, LPBITMAPINFO pHeader)
{
	DWORD nImageSize = ((((pHeader->bmiHeader.biWidth * 
		pHeader->bmiHeader.biBitCount) 
		+ 31) & ~31) >> 3) 
		* pHeader->bmiHeader.biHeight;
 
	BITMAPFILEHEADER header = { 0 };
	header.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + 
		(DWORD)sizeof(BITMAPINFOHEADER); 
	header.bfSize = nImageSize + sizeof(BITMAPFILEHEADER) + 
		sizeof(BITMAPINFOHEADER);
	header.bfType = 0x4D42;
 
	HANDLE hFile = CreateFile(lpszFile, GENERIC_WRITE,
		0, NULL, CREATE_ALWAYS, 
		FILE_ATTRIBUTE_NORMAL, NULL);   
 
	if (hFile != INVALID_HANDLE_VALUE) 
	{
		DWORD nWritten = 0;;
		WriteFile(hFile, (LPVOID)&header, 
			sizeof(BITMAPFILEHEADER), 
			&nWritten, NULL);
		WriteFile(hFile, (LPVOID)pHeader, 
			sizeof(BITMAPINFOHEADER), 
			&nWritten, NULL);
		WriteFile(hFile, (LPVOID)pBits, nImageSize, 
			&nWritten, NULL);
		CloseHandle(hFile);
	}
 
	return TRUE;
}
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:

Select allOpen in new window

 

by: CSecurityPosted on 2009-10-30 at 10:12:02ID: 25704977

HEEEY! I found the problem... Image we take is larger than what it should be.

Image should be resized to 320 * 240... How we can resize image to that size?

 

by: pgnatyukPosted on 2009-10-30 at 10:13:12ID: 25704989

I don't have your video structure, so I emulated it:
HANDLE hDib = GlobalAlloc(GHND,dwBmBitsSize+sizeof(BITMAPINFOHEADER));
LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);    
In your app you don't need it. This Line with GetDIBits should be exactly as you have, but in another place.
      

 

by: pgnatyukPosted on 2009-10-30 at 10:13:57ID: 25704995

:)StrechBlt()

 

by: pgnatyukPosted on 2009-10-30 at 10:16:44ID: 25705018

show me the code with the mistake

 

by: pgnatyukPosted on 2009-10-30 at 10:24:32ID: 25705093

an example. wrote very fast

HBITMAP Stretch(HBITMAP hBitmap, LPRECT lpRect)
{
	HDC hDC = GetDC(NULL);
	HDC hMemDC1 = CreateCompatibleDC(hDC);
	HBITMAP hBitmap1 = CreateCompatibleBitmap(hDC,
		lpRect->right - lpRect->left,
		lpRect->bottom - lpRect->top);
	HGDIOBJ hOld1 = SelectObject(hMemDC1, hBitmap1);
 
	HDC hMemDC2 = CreateCompatibleDC(hDC);
	HGDIOBJ hOld2 = SelectObject(hMemDC2, hBitmap);
 
	BITMAP bitmap = { 0 };
	GetObject(hBitmap, sizeof(BITMAP), &bitmap);
	StretchBlt(hMemDC1, 0, 0, lpRect->right - lpRect->left,
		lpRect->bottom - lpRect->top, hMemDC2, 0, 0, 
		bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);
 
	SelectObject(hMemDC1, hOld1);
	SelectObject(hMemDC2, hOld2);
	DeleteDC(hMemDC1);
	DeleteDC(hMemDC2);
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:

Select allOpen in new window

 

by: CSecurityPosted on 2009-10-30 at 10:24:54ID: 25705094

Here

BitBlt(hMemDC, 0, 0, 320, 240, hScrDC, 320, 240, SRCCOPY);

We have to copy and resize image to 320 * 240

I tried code above but not worked

 

by: CSecurityPosted on 2009-10-30 at 10:26:07ID: 25705101

The code you wrote above, where I should set it to 320 * 240 ?

 

by: pgnatyukPosted on 2009-10-30 at 10:27:16ID: 25705111

RECT rect = { 0, 0, 320, 240 };

 

by: pgnatyukPosted on 2009-10-30 at 10:32:00ID: 25705148

tell me.
return hBitmap1;

HBITMAP Stretch(HBITMAP hBitmap, LPRECT lpRect)
{
	HDC hDC = GetDC(NULL);
	HDC hMemDC1 = CreateCompatibleDC(hDC);
	HBITMAP hBitmap1 = CreateCompatibleBitmap(hDC,
		lpRect->right - lpRect->left,
		lpRect->bottom - lpRect->top);
	HGDIOBJ hOld1 = SelectObject(hMemDC1, hBitmap1);
 
	HDC hMemDC2 = CreateCompatibleDC(hDC);
	HGDIOBJ hOld2 = SelectObject(hMemDC2, hBitmap);
 
	BITMAP bitmap = { 0 };
	GetObject(hBitmap, sizeof(BITMAP), &bitmap);
	StretchBlt(hMemDC1, 0, 0, lpRect->right - lpRect->left,
		lpRect->bottom - lpRect->top, hMemDC2, 0, 0, 
		bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);
 
	SelectObject(hMemDC1, hOld1);
	SelectObject(hMemDC2, hOld2);
	DeleteDC(hMemDC1);
	DeleteDC(hMemDC2);
	return hBitmap1;
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:

Select allOpen in new window

 

by: pgnatyukPosted on 2009-10-30 at 10:33:24ID: 25705157

Full app that takes the calculator screenshot, make it 320x240 and save in the file. The app use GetDIBits - if it fit for your code better.

#include <Windows.h>
 
HBITMAP CopyScreenToBitmap(LPRECT lpRect);
BOOL Save(LPCWSTR lpszFile, LPBYTE pBits, LPBITMAPINFO pHeader);
HBITMAP Stretch(HBITMAP hBitmap, LPRECT lpRect);
 
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
					  LPWSTR lpCmdLine, int nCmdShow)
{
	RECT rect;
 
	HBITMAP hBitmap = CopyScreenToBitmap(&rect);
 
	if (hBitmap)
	{
		RECT rect = { 0, 0, 320, 240 };
		HBITMAP hb1 = Stretch(hBitmap, &rect);
		DeleteObject(hBitmap);
		hBitmap = hb1;
 
		BITMAP bitmap = { 0 };
		GetObject(hBitmap, sizeof(BITMAP), &bitmap);
		DWORD nImageSize = bitmap.bmWidthBytes * bitmap.bmHeight;
		HDC hDC = GetDC(GetDesktopWindow());
 
		DWORD dwBmBitsSize = ((bitmap.bmWidth * 32 + 31) / 32) * 4 * bitmap.bmHeight; 
 
		HANDLE hDib = GlobalAlloc(GHND,dwBmBitsSize+sizeof(BITMAPINFOHEADER)); 
		LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);    
		BITMAPINFOHEADER header = { 0 };
		header.biSize = sizeof(BITMAPINFOHEADER);
		header.biWidth = bitmap.bmWidth;
		header.biHeight = bitmap.bmHeight;
		header.biPlanes = 1;
		header.biBitCount = 32;
		header.biCompression = BI_RGB;
		*lpbi = header;
 
		GetDIBits(hDC, hBitmap, 0, (UINT)bitmap.bmHeight,  
			(LPSTR)lpbi + sizeof(BITMAPINFOHEADER), (BITMAPINFO *)lpbi, 
			DIB_RGB_COLORS);    
 
		ReleaseDC(GetDesktopWindow(), hDC);
		Save(L"cap_screen.bmp", (LPBYTE)lpbi + sizeof(BITMAPINFOHEADER), (BITMAPINFO *)lpbi);
		GlobalUnlock(hDib);    
		GlobalFree(hDib);    
		::DeleteObject(hBitmap);
	}
	return 0;
}
 
HBITMAP CopyScreenToBitmap(LPRECT lpRect)
{
	HDC         hScrDC, hMemDC;         // screen DC and memory DC
	HBITMAP     hBitmap, hOldBitmap;    // handles to deice-dependent bitmaps
	int         nX, nY, nX2, nY2;       // coordinates of rectangle to grab
	int         nWidth, nHeight;        // DIB width and height
	int         xScrn, yScrn;           // screen resolution
 
 
	HWND hwnd = FindWindow(L"SciCalc", L"Calculator");
	SetForegroundWindow(hwnd);
	GetWindowRect(hwnd , lpRect);
	Sleep(10);
 
	hScrDC = GetDC(GetDesktopWindow());
	hMemDC = CreateCompatibleDC(hScrDC);
 
	nWidth = lpRect->right - lpRect->left;
	nHeight = lpRect->bottom - lpRect->top;
 
	BITMAPINFO header = { 0 };
	LPVOID pData = NULL;
	header.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	header.bmiHeader.biWidth = nWidth;
	header.bmiHeader.biHeight = nHeight;
	header.bmiHeader.biPlanes = 1;
	header.bmiHeader.biBitCount = 32;
	header.bmiHeader.biCompression = BI_RGB;
	hBitmap = CreateDIBSection(hScrDC, &header, 
		DIB_RGB_COLORS, (LPVOID*)&pData, NULL, 0);
 
	// select new bitmap into memory DC
	hOldBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);
 
	BitBlt(hMemDC, 0, 0, 
		lpRect->right - lpRect->left,
		lpRect->bottom - lpRect->top,
		hScrDC, lpRect->left, lpRect->top, SRCCOPY);
 
 
	// select old bitmap back into memory DC and get handle to
	// bitmap of the screen  
	SelectObject(hMemDC, hOldBitmap);
	// clean up
	DeleteDC(hMemDC);
 
	ReleaseDC(GetDesktopWindow(), hScrDC);
	// return handle to the bitmap
	return hBitmap;
}
 
 
BOOL Save(LPCWSTR lpszFile, LPBYTE pBits, LPBITMAPINFO pHeader)
{
	DWORD nImageSize = ((((pHeader->bmiHeader.biWidth * 
		pHeader->bmiHeader.biBitCount) 
		+ 31) & ~31) >> 3) 
		* pHeader->bmiHeader.biHeight;
 
	BITMAPFILEHEADER header = { 0 };
	header.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + 
		(DWORD)sizeof(BITMAPINFOHEADER); 
	header.bfSize = nImageSize + sizeof(BITMAPFILEHEADER) + 
		sizeof(BITMAPINFOHEADER);
	header.bfType = 0x4D42;
 
	HANDLE hFile = CreateFile(lpszFile, GENERIC_WRITE,
		0, NULL, CREATE_ALWAYS, 
		FILE_ATTRIBUTE_NORMAL, NULL);   
 
	if (hFile != INVALID_HANDLE_VALUE) 
	{
		DWORD nWritten = 0;;
		WriteFile(hFile, (LPVOID)&header, 
			sizeof(BITMAPFILEHEADER), 
			&nWritten, NULL);
		WriteFile(hFile, (LPVOID)pHeader, 
			sizeof(BITMAPINFOHEADER), 
			&nWritten, NULL);
		WriteFile(hFile, (LPVOID)pBits, nImageSize, 
			&nWritten, NULL);
		CloseHandle(hFile);
	}
 
	return TRUE;
}
 
HBITMAP Stretch(HBITMAP hBitmap, LPRECT lpRect)
{
	HDC hDC = GetDC(NULL);
	HDC hMemDC1 = CreateCompatibleDC(hDC);
	HBITMAP hBitmap1 = CreateCompatibleBitmap(hDC,
		lpRect->right - lpRect->left,
		lpRect->bottom - lpRect->top);
	HGDIOBJ hOld1 = SelectObject(hMemDC1, hBitmap1);
 
	HDC hMemDC2 = CreateCompatibleDC(hDC);
	HGDIOBJ hOld2 = SelectObject(hMemDC2, hBitmap);
 
	BITMAP bitmap = { 0 };
	GetObject(hBitmap, sizeof(BITMAP), &bitmap);
	StretchBlt(hMemDC1, 0, 0, lpRect->right - lpRect->left,
		lpRect->bottom - lpRect->top, hMemDC2, 0, 0, 
		bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);
 
	SelectObject(hMemDC1, hOld1);
	SelectObject(hMemDC2, hOld2);
	DeleteDC(hMemDC1);
	DeleteDC(hMemDC2);
	return hBitmap1;
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:

Select allOpen in new window

 

by: CSecurityPosted on 2009-10-30 at 10:40:45ID: 25705225

Look what I get now

 

by: CSecurityPosted on 2009-10-30 at 10:41:09ID: 25705228

   // create a bitmap compatible with the screen DC
    hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);

    // select new bitmap into memory DC
    hOldBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);

      RECT rect = { 0, 0, 320, 240 };
      HBITMAP hb1 = Stretch(hOldBitmap, &rect);

      hOldBitmap = (HBITMAP) SelectObject(hMemDC, hb1);

 

by: pgnatyukPosted on 2009-10-30 at 10:49:31ID: 25705294

You don't see? you need rect = { 0, 0, 240, 320}

 

by: pgnatyukPosted on 2009-10-30 at 10:53:39ID: 25705322

That was the mistake in the code posted first time:
hBitmap = (HBITMAP) SelectObject(hMemDC, hOldBitmap);

Check. It's 01:54 AM here. :)

 

by: pgnatyukPosted on 2009-10-30 at 10:57:32ID: 25705355

actually I don't understand what is the output size. :(
You should know.

 

by: CSecurityPosted on 2009-10-30 at 10:58:19ID: 25705360

I finally solved size issue, now when I move notepad over calc, I see notepad... What was wrong for this problem?

 

by: pgnatyukPosted on 2009-10-30 at 10:58:44ID: 25705363

my image is exactly 320x240

 

by: CSecurityPosted on 2009-10-30 at 11:00:05ID: 25705374

What to change here to GET only Calc's image?


      HWND hwnd = FindWindow("SciCalc", "Calculator");
      GetWindowRect(hwnd , lpRect);
      hScrDC = GetWindowDC(hwnd);
      hMemDC = CreateCompatibleDC(hScrDC);      // get points of re

 

by: pgnatyukPosted on 2009-10-30 at 11:00:34ID: 25705381

I've told you - you are taking images from your desktop. You receive exactly what you see on the screen. Because of that in my code I added SetForegroundWindow and other stuff - you will face these issues too.

 

by: pgnatyukPosted on 2009-10-30 at 11:01:43ID: 25705390

at least so:

	HWND hwnd = FindWindow("SciCalc", "Calculator");
	SetForegroundWindow(hwnd);
	GetWindowRect(hwnd , lpRect);
	Sleep(10);
                                              
1:
2:
3:
4:

Select allOpen in new window

 

by: CSecurityPosted on 2009-10-30 at 11:04:44ID: 25705416

but
SetForegroundWindow(hwnd);


Makes calculator focused window and I can't work with my PC

 

by: CSecurityPosted on 2009-10-30 at 11:06:14ID: 25705434

Even after setting foreground window, I see notepad over it

 

by: pgnatyukPosted on 2009-10-30 at 11:10:37ID: 25705468

switch focus back to your window.
I don't see any notepad. You are in the debug mode?
The function makes the copy of your desktop. You just say from which rectangle. What you see in this rectangle on the desktop will be in the taken image.

 

by: CSecurityPosted on 2009-10-30 at 11:20:24ID: 25705561

There isn't a way to capture image of a window with it's handle even if it's minimized?

 

by: CSecurityPosted on 2009-10-30 at 11:50:43ID: 31647934

Thanks

 

by: pgnatyukPosted on 2009-10-30 at 18:44:56ID: 25707983

You are welcome.
I'm not sure that you marked as the solution the correct answer.

 

by: pgnatyukPosted on 2009-10-30 at 19:08:09ID: 25708061

Probably there is a way but I just don't know about it.
Few years ago, we had to install video drivers and make virtual displays in order to launch GUI applications.
There were a so called terminal sessions. Code injection. I don't know, maybe there is something else very simple.
For me - a visible window draws itself on the desktop, we need to grab it.

 

by: CSecurityPosted on 2009-10-31 at 01:33:49ID: 25708887

Thanks once again for your help.
Just a quick question, is it possible to Inject a DLL to calculator and make that DLL to capture it's own image even if it's minimized?

 

by: pgnatyukPosted on 2009-10-31 at 02:06:13ID: 25708939

The DLL injection is almost always possible. Calculator, probably, is the easiest case.
But I don't know how it will help with the screen capturing if the calculator is minimized - actually same as invisible. In this state the window will not draw itself. That's all. Try to get GetWindowRect of a minimized window, you will see the coordinates yourself. So what can be invalidated or drawn?
Even if you really need only one calculator, you can investigate and maybe will find a trick.
I've told you that few years ago we found a more or less generic trick for a more complicated case - we installed new virtual display and launch the application we needed on this display - so they were visible.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...