c++ Program to take a screenshot
Asked Answered
S

2

8

I am making a program that will click the printscreen key of the keyboard. The code I am using is the following:

INPUT myInput;

myInput.type = INPUT_KEYBOARD;

KEYBDINPUT keyboardInput;
keyboardInput.wScan = 0;
keyboardInput.dwFlags = 0;
keyboardInput.time = 0;
keyboardInput.dwExtraInfo = 0;
keyboardInput.wVk = VK_SNAPSHOT; 
myInput.ki = keyboardInput;

SendInput(1, &myInput, sizeof(INPUT));//pressing the printscreen key

keyboardInput.dwFlags = KEYEVENTF_KEYUP;
myInput.ki = keyboardInput;

SendInput(1, &myInput, sizeof(INPUT));//releasing the printscreen key

for some reason the code doesn't work what so ever. If I go to paint and try to paist from clipboard, it will only past whatever printscreen I had done before I had used my program. Also my keyboard doesn't need me to press the "alt" with the print screen in order for it to work..

I had tryed to included the pressing of the Alt key beeeforee the pressing of the printscreen key, as well as the release of the Alt key afffftterr the releasing of the printscreen key, and the difference I got was that when I tried to past it on paint, I paist some kind of a full black screen... This was just a test I did to see if it makes a difference, but my actual keyboard takes screenshots with only the hit of the print screen button.

Any ideas on what I am doing wrong guys?

Edited: just to let you guys know, the program does in fact compile. I have also added other code that saves the clipboard file to a directory, and I do save a file correctly if I manually hit the print screen button... but if I keep looping this code with the saving to a directory, the same picture of the manually obtained screenshot appears... so that is how I know for sure that there is a problem with the hitting of the printscreen button.

Sandal answered 27/7, 2014 at 20:23 Comment(7)
possible duplicate of What is the best way to take screenshots of a Window with C++ in Windows?Pup
Did those function calls succeed? If you didn't bother checking, that's the first thing you should have done. The documentation clearly states the conditions. Anyway, given that it's possible to programmatically take a screenshot, you shouldn't be pressing a key.Borghese
yes they did succed. The program compiles perfectly. Infact I have another code that follows this program that actually makes a png file... which works perfectly... but it keeps making a png file of what screenshot I do manually... for instance, if I take a screenshot.... then i run te program on a different colored screen... the exported file will be of my first manually done screenshot... and it will keep being that manually taken screenshot until I do another manual screenshot press.Sandal
@MrEricSir: thank you for the comment Eric. Regretably it is not a duplicate, I wish it were so that I could find my answer there :). That one goes about using other functions to get a printed screen, while I would like to know why the method I am using is incorrect. Thank you regardless for your answer.Sandal
"yes they did succeed. The program compiles perfectly." - compiling doesn't mean something succeeds at runtime... you should be checking the value returned ala int result = SendInput(...); if (result != 1) { ... use GetLastError() to find out why it failed ...};Cookson
@Tony D: Oh interesting, I will definitly check that out, thanks!Sandal
@rsthegreat12: And that is the point where you should shelf any more ambitious projects (like, uh, taking a screenshot) and return to basic tutorial stuff, like, uh, checking return values... No offense intended, it's quite common for people to jump at "interesting" before they got the basics down pat.Perfectionist
P
4

On the windows platform: You have to follow a certain sequence of simulated key presses.

The code below is a simulates keybd_event() keyboard events and puts the captured screen into the clipboard.

#include <iostream>
#include <windows.h>
using namespace std;



int main()
{
    keybd_event(VK_MENU, 0, 0, 0); //Alt Press
    keybd_event(VK_SNAPSHOT, 0, 0, 0); //PrntScrn Press


    keybd_event(VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0); //PrntScrn Release
    keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0); //Alt Release

return 0;
}
Pneumogastric answered 2/8, 2014 at 20:42 Comment(0)
K
3

That is code for taking a screenshot in BMP and to convert it to JPG:

void TakeScreenShot(const std::string& path)
{
//setting to the screen shot
keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);

//handler of the bitmap that save the screen shot
HBITMAP hBitmap;

//I have to give for it time to make it work
Sleep(100);

//take the screen shot
OpenClipboard(NULL);

//save the screen shot in the bitmap handler 
hBitmap = (HBITMAP)GetClipboardData(CF_BITMAP);

//relese the screen shot
CloseClipboard();

std::vector<BYTE> buf;
IStream *stream = NULL;
HRESULT hr = CreateStreamOnHGlobal(0, TRUE, &stream);
CImage image;
ULARGE_INTEGER liSize;

// screenshot to jpg and save to stream
image.Attach(hBitmap);
image.Save(stream, Gdiplus::ImageFormatJPEG);
IStream_Size(stream, &liSize);
DWORD len = liSize.LowPart;
IStream_Reset(stream);
buf.resize(len);
IStream_Read(stream, &buf[0], len);
stream->Release();

// put the imapge in the file
std::fstream fi;
fi.open(path, std::fstream::binary | std::fstream::out);
fi.write(reinterpret_cast<const char*>(&buf[0]), buf.size() * sizeof(BYTE));
fi.close();
}
Kulturkampf answered 1/5, 2019 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.