943,074 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 10536
  • C++ RSS
May 1st, 2009
0

Screen Capture Program

Expand Post »
I am creating this simple program that will capture screen shots. I want to make it so the name of the image that is saved is what the user enters into a text field before the picture is taken. and if at all possible is there a way to make this window pop up above the game screen when a key combination is pressed?

thanks! this is the code i have so far btw.

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<windows.h>
  3. using namespace std;
  4.  
  5. inline int GetFilePointer(HANDLE FileHandle){
  6. return SetFilePointer(FileHandle, 0, 0, FILE_CURRENT);
  7. }
  8.  
  9. bool SaveBMPFile(char *filename, HBITMAP bitmap, HDC bitmapDC, int width, int height){
  10. bool Success=0;
  11. HDC SurfDC=NULL;
  12. HBITMAP OffscrBmp=NULL;
  13. HDC OffscrDC=NULL;
  14. LPBITMAPINFO lpbi=NULL;
  15. LPVOID lpvBits=NULL;
  16. HANDLE BmpFile=INVALID_HANDLE_VALUE;
  17. BITMAPFILEHEADER bmfh;
  18. if ((OffscrBmp = CreateCompatibleBitmap(bitmapDC, width, height)) == NULL)
  19. return 0;
  20. if ((OffscrDC = CreateCompatibleDC(bitmapDC)) == NULL)
  21. return 0;
  22. HBITMAP OldBmp = (HBITMAP)SelectObject(OffscrDC, OffscrBmp);
  23. BitBlt(OffscrDC, 0, 0, width, height, bitmapDC, 0, 0, SRCCOPY);
  24. if ((lpbi = (LPBITMAPINFO)(new char[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)])) == NULL)
  25. return 0;
  26. ZeroMemory(&lpbi->bmiHeader, sizeof(BITMAPINFOHEADER));
  27. lpbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  28. SelectObject(OffscrDC, OldBmp);
  29. if (!GetDIBits(OffscrDC, OffscrBmp, 0, height, NULL, lpbi, DIB_RGB_COLORS))
  30. return 0;
  31. if ((lpvBits = new char[lpbi->bmiHeader.biSizeImage]) == NULL)
  32. return 0;
  33. if (!GetDIBits(OffscrDC, OffscrBmp, 0, height, lpvBits, lpbi, DIB_RGB_COLORS))
  34. return 0;
  35. if ((BmpFile = CreateFile(filename,
  36. GENERIC_WRITE,
  37. 0, NULL,
  38. CREATE_ALWAYS,
  39. FILE_ATTRIBUTE_NORMAL,
  40. NULL)) == INVALID_HANDLE_VALUE)
  41. return 0;
  42. DWORD Written;
  43. bmfh.bfType = 19778;
  44. bmfh.bfReserved1 = bmfh.bfReserved2 = 0;
  45. if (!WriteFile(BmpFile, &bmfh, sizeof(bmfh), &Written, NULL))
  46. return 0;
  47. if (Written < sizeof(bmfh))
  48. return 0;
  49. if (!WriteFile(BmpFile, &lpbi->bmiHeader, sizeof(BITMAPINFOHEADER), &Written, NULL))
  50. return 0;
  51. if (Written < sizeof(BITMAPINFOHEADER))
  52. return 0;
  53. int PalEntries;
  54. if (lpbi->bmiHeader.biCompression == BI_BITFIELDS)
  55. PalEntries = 3;
  56. else PalEntries = (lpbi->bmiHeader.biBitCount <= 8) ?
  57. (int)(1 << lpbi->bmiHeader.biBitCount) : 0;
  58. if(lpbi->bmiHeader.biClrUsed)
  59. PalEntries = lpbi->bmiHeader.biClrUsed;
  60. if(PalEntries){
  61. if (!WriteFile(BmpFile, &lpbi->bmiColors, PalEntries * sizeof(RGBQUAD), &Written, NULL))
  62. return 0;
  63. if (Written < PalEntries * sizeof(RGBQUAD))
  64. return 0;
  65. }
  66. bmfh.bfOffBits = GetFilePointer(BmpFile);
  67. if (!WriteFile(BmpFile, lpvBits, lpbi->bmiHeader.biSizeImage, &Written, NULL))
  68. return 0;
  69. if (Written < lpbi->bmiHeader.biSizeImage)
  70. return 0;
  71. bmfh.bfSize = GetFilePointer(BmpFile);
  72. SetFilePointer(BmpFile, 0, 0, FILE_BEGIN);
  73. if (!WriteFile(BmpFile, &bmfh, sizeof(bmfh), &Written, NULL))
  74. return 0;
  75. if (Written < sizeof(bmfh))
  76. return 0;
  77. return 1;
  78. }
  79. bool ScreenCapture(int x, int y, int width, int height, char *filename){
  80. HDC hDc = CreateCompatibleDC(0);
  81. HBITMAP hBmp = CreateCompatibleBitmap(GetDC(0), width, height);
  82. SelectObject(hDc, hBmp);
  83. BitBlt(hDc, 0, 0, width, height, GetDC(0), x, y, SRCCOPY);
  84. bool ret = SaveBMPFile(filename, hBmp, hDc, width, height);
  85. DeleteObject(hBmp);
  86. return ret;
  87. }
  88.  
  89. int main() {
  90. int x1 = 0;
  91. int y1 = 0;
  92. int x2 = GetSystemMetrics(SM_CXSCREEN);
  93. int y2 = GetSystemMetrics(SM_CYSCREEN);
  94. ScreenCapture(x1, y1, x2 - x1, y2 - y1, "rename_me_before_upload.bmp");
  95. cin.ignore();
  96. return 0;
  97. }
Last edited by kashmir323; May 1st, 2009 at 5:02 pm.
Similar Threads
Reputation Points: 31
Solved Threads: 0
Light Poster
kashmir323 is offline Offline
28 posts
since Jan 2008
May 1st, 2009
0

Re: Screen Capture Program

If all you want to do is let allow the user to input the bitmap name, use std::cin to get the name, and strcat ".bmp" on the end. Change your main code like this:
C++ Syntax (Toggle Plain Text)
  1. int main() {
  2. int x1 = 0;
  3. int y1 = 0;
  4. int x2 = GetSystemMetrics(SM_CXSCREEN);
  5. int y2 = GetSystemMetrics(SM_CYSCREEN);
  6.  
  7. char bmp_name[100];
  8. cin.getline( bmp_name, 96 );
  9. strcat( bmp_name, ".bmp" );
  10.  
  11.  
  12. ScreenCapture(x1, y1, x2 - x1, y2 - y1, bmp_name);
  13.  
  14. cin.ignore();
  15. return 0;
  16. }

>if at all possible is there a way to make this window pop up above the game screen when a key combination is pressed?
Not quite sure what you mean by this.
Last edited by William Hemsworth; May 1st, 2009 at 6:25 pm.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
May 1st, 2009
0

Re: Screen Capture Program

Since this program is a anti-cheat parameter for the league, the picture wont be needed till once the player is in game. so once in game i would like the players to push lets say Shift+Alt+S to bring up the console app over top the game. Then once the picture is taken and saved then push Shift+Alt+S to minimize the console behind the top screen again.
Reputation Points: 31
Solved Threads: 0
Light Poster
kashmir323 is offline Offline
28 posts
since Jan 2008
May 1st, 2009
0

Re: Screen Capture Program

Okay, that's tricky but possible. In this small example, I check to see if those keys are down. If they are down, it will toggle the windows visibility. I tried using windows hooks to give this example, but I couldn't set the hook without having a hInstance, so I tried this alternative method which works on console windows.
C++ Syntax (Toggle Plain Text)
  1. #define _WIN32_WINNT 0x0500
  2. #include <windows.h>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int main() {
  7. bool visible = false;
  8. ShowWindow( GetConsoleWindow(), SW_HIDE );
  9.  
  10. for ( ; ; ) {
  11.  
  12. if ( GetAsyncKeyState( VK_SHIFT ) &&
  13. GetAsyncKeyState( VK_MENU ) &&
  14. GetAsyncKeyState( VkKeyScanA('s') ) )
  15. {
  16. if ( visible )
  17. ShowWindow( GetConsoleWindow(), SW_HIDE );
  18. else
  19. ShowWindow( GetConsoleWindow(), SW_SHOW );
  20.  
  21. visible = !visible;
  22. Sleep( 500 );
  23. }
  24.  
  25. Sleep( 100 );
  26. }
  27.  
  28. cin.ignore();
  29. }
Whether or not ShowWindow will be enough to bring the console window in front of the game, I don't know, but that would be the next stage if the above doesn't work.

Hope this helps.

Code tags don't seem to work correctly, and can't get the C++ syntax, not sure why.
Last edited by William Hemsworth; May 1st, 2009 at 8:21 pm.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
May 1st, 2009
0

Re: Screen Capture Program

Alright. So i havent compiled any programs for a while and im compiling this using the Microsoft Visual C++ Toolkit 2003. When i click build it gives me an error saying:
Quote ...
"LRSscreenshooter - Release" uses an invalid compiler. Skipping...
Nothing to be done.
Any idea what is wrong? And im building this in CodeBlocks 8.02.
Last edited by kashmir323; May 1st, 2009 at 9:47 pm.
Reputation Points: 31
Solved Threads: 0
Light Poster
kashmir323 is offline Offline
28 posts
since Jan 2008
Oct 28th, 2009
0
Re: Screen Capture Program
Hello,

I'm trying to use the code in the first post to capture a smaller area than the whole screen. I have a point that is at the center of the area I need to capture. In my main() function I call ScreenCapture(xPos - 25, yPos - 25, 50, 50, "image.bmp"), where xPos is the x-position of the center and yPos is the y-position of the center point.

Everything is running OK, but the saved image is black. It has the right size, but it's black. What am I doing wrong?

Thanx!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thefly is offline Offline
2 posts
since Oct 2009
Oct 28th, 2009
0
Re: Screen Capture Program
Click to Expand / Collapse  Quote originally posted by thefly ...
Hello,

I'm trying to use the code in the first post to capture a smaller area than the whole screen. I have a point that is at the center of the area I need to capture. In my main() function I call ScreenCapture(xPos - 25, yPos - 25, 50, 50, "image.bmp"), where xPos is the x-position of the center and yPos is the y-position of the center point.

Everything is running OK, but the saved image is black. It has the right size, but it's black. What am I doing wrong?

Thanx!
What OS are you using ?
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Oct 28th, 2009
0
Re: Screen Capture Program
Click to Expand / Collapse  Quote originally posted by ithelp ...
What OS are you using ?
Windows Vista 32bit. Capturing the whole screen works well. I get normal screen shot.
Last edited by thefly; Oct 28th, 2009 at 5:32 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thefly is offline Offline
2 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: column resizable
Next Thread in C++ Forum Timeline: What is conditional operators





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC