|
-
June 21st, 2007, 12:07 AM
#1
Code for taking screenshot but doesn't work
Code:
#include <iostream.h>
#include <windows.h>
#include <stdio.h>
int main()
{
TakeScreenShot("c:\\Screenshot.bmp");
return 0;
}
//
// Side Effects:N/A
//
//This code is copyrighted and has// limited warranties.Please see http://
// www.Planet-Source-Code.com/vb/scripts/Sh
// owCode.asp?txtCodeId=10754&lngWId=3//for details.//**************************************
//
void TakeScreenShot(char* filename)
{
keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
HBITMAP h;
OpenClipboard(NULL);
h = (HBITMAP)GetClipboardData(CF_BITMAP);
CloseClipboard();
HDC hdc=NULL;
FILE*fp=NULL;
LPVOIDpBuf=NULL;
BITMAPINFO bmpInfo;
BITMAPFILEHEADER bmpFileHeader;
do
{
hdc=GetDC(NULL);
ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
GetDIBits(hdc,h,0,0,NULL,&bmpInfo,DIB_RGB_COLORS);
if(bmpInfo.bmiHeader.biSizeImage<=0)
bmpInfo.bmiHeader.biSizeImage=bmpInfo.bmiHeader.biWidth*abs(bmpInfo.bmiHeader.biHeight)*(bmpInfo.bmiHeader.biBitCount+7)/8;
if((pBuf = malloc(bmpInfo.bmiHeader.biSizeImage))==NULL)
{
MessageBox( NULL, "Unable to Allocate Bitmap Memory", "Error", MB_OK|MB_ICONERROR);
break;
}
bmpInfo.bmiHeader.biCompression=BI_RGB;
GetDIBits(hdc,h,0,bmpInfo.bmiHeader.biHeight,pBuf, &bmpInfo, DIB_RGB_COLORS);
if((fp = fopen(filename,"wb"))==NULL)
{
MessageBox( NULL, "Unable to Create Bitmap File", "Error", MB_OK|MB_ICONERROR);
break;
}
bmpFileHeader.bfReserved1=0;
bmpFileHeader.bfReserved2=0;
bmpFileHeader.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+bmpInfo.bmiHeader.biSizeImage;
bmpFileHeader.bfType='MB';
bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
fwrite(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
fwrite(&bmpInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
fwrite(pBuf,bmpInfo.bmiHeader.biSizeImage,1,fp);
}
while(false);
if(hdc)ReleaseDC(NULL,hdc);
if(pBuf) free(pBuf);
if(fp)fclose(fp);
}
Okay, I have this code I found online, but it just doesn't work. Like the first error I get is about iostream.h so I remove the 'h' part and that problem is fixed. Then it says function is undeclared so I put main below the function and that is fixed. But the next error I get is 26 'LPVOIDpBuf' undeclared (first use this function) and I have no idea what to do here, so if anyone has used this code please tell me how you fixed it, or if you have some other complete code to capture screen please post, thanks a lot.
-
June 21st, 2007, 01:14 AM
#2
Re: Code for taking screenshot but doesn't work
LPVOIDpBuf is typing error. It should be
LPVOID is an alias for void*.
-
June 21st, 2007, 09:29 PM
#3
Re: Code for taking screenshot but doesn't work
Okay, thanks a lot, it works now. I guess the only complain I have is that the file it creates is around 3.8 MB. I have a program Screenshot Utility, the image format is JPG and the file is always less than 100 KB, sometimes even 60 KB, and the quality is very good also. If there's something I can change in this code to produce images with smaller size that will help a lot so if someone knows please let me know or if there's some other code that creates the pics in jpg format etc, but if not I guess I can live with the code I already have.
-
June 21st, 2007, 11:01 PM
#4
Re: Code for taking screenshot but doesn't work
One thing:
The correct header is <iostream>, not <iostream.h>
 Originally Posted by newbie421
I guess the only complain I have is that the file it creates is around 3.8 MB. I have a program Screenshot Utility, the image format is JPG and the file is always less than 100 KB,
JPEG is designed to do this. It is actually a compression scheme, and that is why the images are small.
So the quick answer is not to save them as BMP, but to save them as JPEG files. That requires you to use GDI+ or some other third-party library to save to JPEG (or if you're a JPEG expert, writing your own code). Such libraries are from IJG or Intel.
Regards,
Paul McKenzie
-
June 21st, 2007, 11:46 PM
#5
Re: Code for taking screenshot but doesn't work
Just a note, that JPEG is a compression algorithm that does lose information. There are others that are "loseless". Depends on your requirements....
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
May 11th, 2009, 10:03 AM
#6
Re: Code for taking screenshot but doesn't work
i want to take screens as jpg too, can you suggest an easy way?
btw, this code works only one of two times =\
Last edited by Owyn; May 11th, 2009 at 10:13 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|