View previous topic :: View next topic |
Author |
Message |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Mon Dec 27, 2010 7:45 pm Post subject: [C++] Saving a screenshot? |
|
|
Basic psuedocode of my program:
Code: | #include<string>
#include<Windows.h>
#include<iostream>
using namespace std;
int main(){
FreeConsole();
for(;;){
if (8 is pressed)
savePicture();
}
return 0;
}
|
I want my program running in the background.
When I press a hotkey, lets say F9, I want to take a screenshot of the entire screen (or the active window if that'd be easier), and then save it somewhere.
Basically my question is, what API's am I gonna need for if (8 is pressed) , and for the actual saving of the picture as an actual file (bmp?).
Advice/tips/API's/psuedocode/anything appreciated <3
_________________
|
|
Back to top |
|
 |
atom0s Moderator
Reputation: 204
Joined: 25 Jan 2006 Posts: 8580 Location: 127.0.0.1
|
|
Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Mon Dec 27, 2010 8:10 pm Post subject: |
|
|
Wow, everything I need. Thank you. I'll be going the Clipboard route then. lol
_________________
|
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Mon Dec 27, 2010 9:50 pm Post subject: |
|
|
you can also:
select a DIB section into a memoryDC then blit the screen into it.
- pass 0 to GetDC() to return a context for the entire screen.
- create your offscreen buffer with CreateCompatibleDC()
- create a DIB section with the aptly named CreateDIBSection()
- use SelectObject() to select the DIB section into the memory dc
- simply BitBlt(), use the original context as the source and the memory dc as the destination
like magic, you have raw pixel data for the entire screen. append your bitmap header and save. can't remember off the top of my head if you need to flip the byte ordering, bgr / rgb.
fast and easy and if you really feel so inclined, easy to manipulate the resulting image right then and there.
|
|
Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Sun Jan 09, 2011 8:33 pm Post subject: |
|
|
Alright, this is my code at the moment:
Code: | #include <Windows.h>
#include <iostream>
void printscreen(void){
keybd_event(VK_SNAPSHOT, 0, 0, 0);
keybd_event(VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0);
//Normal Full Screenshot
}
void printscreenwindow(void){
keybd_event(VK_SNAPSHOT, 1, 0, 0);
keybd_event(VK_SNAPSHOT, 1, KEYEVENTF_KEYUP, 0);
//Screenshots the active window only
}
void savePic(void){
OpenClipboard(NULL);
HANDLE clipHandle = GetClipboardData(CF_BITMAP);
//Convert the clipboard data to a bmp and save it ??
CloseClipboard();
}
int main(){
for(;;25){
if ( GetAsyncKeyState(0x7B) ){ // 0x7B = F12
printscreen();
savePic();
}
if ( GetAsyncKeyState(0x7A) ){ // 0x7A = F11
printscreenwindow();
savePic();
}
}
return 0;
} |
This might be totally fucking wrong. I know . _ .
But my question is, how exactly do I use the handle of the clipboard data to save it ? Am I missing something here?
_________________
|
|
Back to top |
|
 |
atom0s Moderator
Reputation: 204
Joined: 25 Jan 2006 Posts: 8580 Location: 127.0.0.1
|
Posted: Mon Jan 10, 2011 10:01 am Post subject: |
|
|
Use SetClipboardData with CF_BITMAP after you opened the clipboard.
_________________
- Retired. |
|
Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Mon Jan 10, 2011 12:27 pm Post subject: |
|
|
Code: | void savePic(void){
OpenClipboard(NULL);
HANDLE clipHandle = GetClipboardData(CF_BITMAP);
HANDLE dataHandle = SetClipboardData(CF_BITMAP, clipHandle);
// ??
CloseClipboard();
} |
Thank you, I have that now.
According to MSDN, Quote: | If the function succeeds, the return value is the handle to the data. |
So I guess my question is, how do I use the HANDLE (dataHandle) to the data to create the 'physical' file at my designated directory?
_________________
|
|
Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Mon Jan 10, 2011 2:11 pm Post subject: |
|
|
You should get Programming Windows 5th edition. It has this example in it using the bitblt method.
_________________
|
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Mon Jan 10, 2011 6:55 pm Post subject: |
|
|
BitBlt() is more or less a fancy memcpy(), not really that difficult
|
|
Back to top |
|
 |
atom0s Moderator
Reputation: 204
Joined: 25 Jan 2006 Posts: 8580 Location: 127.0.0.1
|
Posted: Mon Jan 10, 2011 10:06 pm Post subject: |
|
|
manc wrote: | Code: | void savePic(void){
OpenClipboard(NULL);
HANDLE clipHandle = GetClipboardData(CF_BITMAP);
HANDLE dataHandle = SetClipboardData(CF_BITMAP, clipHandle);
// ??
CloseClipboard();
} |
Thank you, I have that now.
According to MSDN, Quote: | If the function succeeds, the return value is the handle to the data. |
So I guess my question is, how do I use the HANDLE (dataHandle) to the data to create the 'physical' file at my designated directory? |
Wow sorry completely misread the post before, thought you were asking how to set the clipboard with a bitmap.
If you want to read from it, in your case a bitmap, call OpenClipboard then GetClipboardData passing CF_BITMAP. The handle returned from GetClipboardData can be used to call GetObject to obtain the BITMAP information from the image. Then you can reconstruct a valid bitmap header from that data and save the file.
You'll have to rebuild the header manually, I don't think there is a way to obtain it from the clipboard or if it even makes one. The clipboard just has the raw BITMAP data I believe.
You'll need to build:
- BITMAPFILEHEADER structure.
- BITMAPINFOHEADER structure.
- Raw data of image from the BITMAP information.
_________________
- Retired. |
|
Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Mon Jan 10, 2011 11:42 pm Post subject: |
|
|
Thanks for all the help you are giving, by the way. 'Tis appreciated.
Wiccaan wrote: |
You'll need to build:
- BITMAPFILEHEADER structure.
- BITMAPINFOHEADER structure. |
This might sound ridiculous, but how do I make my own structures?
If I name them the same thing, then I get redefinition errors since they already exist. I cant just declare an object of BITMAPFILEHEADER type and access it like I would with a class either, (ex: object.datamember ) because intellisense doesnt pick it up, and entering it manually gives errors.
I meant like this,
Code: |
BITMAPFILEHEADER bmfh;
bmfh.bfType = BM;
bmfh.bfSize = 900;
bmfh.bfReserved1= 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits = 3; |
Google isnt yielding very relevant answers at the moment, I did search already.
_________________
|
|
Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Mon Jan 10, 2011 11:46 pm Post subject: |
|
|
manc wrote: | Thanks for all the help you are giving, by the way. 'Tis appreciated.
Wiccaan wrote: |
You'll need to build:
- BITMAPFILEHEADER structure.
- BITMAPINFOHEADER structure. |
This might sound ridiculous, but how do I make my own structures?
If I name them the same thing, then I get redefinition errors since they already exist. I cant just declare an object of BITMAPFILEHEADER type and access it like I would with a class either, (ex: object.datamember ) because intellisense doesnt pick it up, and entering it manually gives errors.
I meant like this,
Code: |
BITMAPFILEHEADER bmfh;
bmfh.bfType = BM;
bmfh.bfSize = 900;
bmfh.bfReserved1= 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits = 3; |
Google isnt yielding very relevant answers at the moment, I did search already.  |
There are tons of examples on google
http://www.gamedev.net/topic/395078-c-win32-how-to-save-a-dc-to-a-bitmap/
Found that after like 2 searches. There is source code on there for exactly what you want. As stated above, find (ahem arrrarrmatey) the book i mentioned. It is found easily in ebook chm format.
_________________
|
|
Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Tue Jan 11, 2011 12:32 am Post subject: |
|
|
HomerSexual wrote: | find (ahem arrrarrmatey) the book i mentioned. It is found easily in ebook chm format. |
Alright thanks matey, I "found" it. I think I found the relevant part, Ill take a closer took tomorrow and post if any complications arise.
_________________
|
|
Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Tue Jan 11, 2011 10:33 am Post subject: |
|
|
manc wrote: | HomerSexual wrote: | find (ahem arrrarrmatey) the book i mentioned. It is found easily in ebook chm format. |
Alright thanks matey, I "found" it. I think I found the relevant part, Ill take a closer took tomorrow and post if any complications arise. |
They distributed the ebook along with the book, was very convenient haha. I did buy the book though since it's only like 10$ on ebay and makes me look nerdy on my bookshelf. On topic sort of, read it all! It's such a nice reference for win32 programming.
_________________
|
|
Back to top |
|
 |
|