Screen Capture Program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 28
Reputation: kashmir323 is an unknown quantity at this point 
Solved Threads: 0
kashmir323 kashmir323 is offline Offline
Light Poster

Screen Capture Program

 
0
  #1
May 1st, 2009
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,416
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 114
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

Re: Screen Capture Program

 
0
  #2
May 1st, 2009
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:
  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.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 28
Reputation: kashmir323 is an unknown quantity at this point 
Solved Threads: 0
kashmir323 kashmir323 is offline Offline
Light Poster

Re: Screen Capture Program

 
0
  #3
May 1st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,416
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 114
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

Re: Screen Capture Program

 
0
  #4
May 1st, 2009
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.
  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.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 28
Reputation: kashmir323 is an unknown quantity at this point 
Solved Threads: 0
kashmir323 kashmir323 is offline Offline
Light Poster

Re: Screen Capture Program

 
0
  #5
May 1st, 2009
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:
"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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: thefly is an unknown quantity at this point 
Solved Threads: 0
thefly thefly is offline Offline
Newbie Poster
 
0
  #6
32 Days Ago
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!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,827
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 117
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso
 
0
  #7
32 Days Ago
Originally Posted by thefly View Post
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 ?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: thefly is an unknown quantity at this point 
Solved Threads: 0
thefly thefly is offline Offline
Newbie Poster
 
0
  #8
32 Days Ago
Originally Posted by ithelp View Post
What OS are you using ?
Windows Vista 32bit. Capturing the whole screen works well. I get normal screen shot.
Last edited by thefly; 32 Days Ago at 5:32 am.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC