Click to See Complete Forum and Search --> : Code for taking screenshot but doesn't work


newbie421
June 21st, 2007, 12:07 AM
#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.

cilu
June 21st, 2007, 01:14 AM
LPVOIDpBuf is typing error. It should be

LPVOID pBuf=NULL;

LPVOID is an alias for void*.

newbie421
June 21st, 2007, 09:29 PM
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.

Paul McKenzie
June 21st, 2007, 11:01 PM
One thing:

The correct header is <iostream>, not <iostream.h>
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

TheCPUWizard
June 21st, 2007, 11:46 PM
Just a note, that JPEG is a compression algorithm that does lose information. There are others that are "loseless". Depends on your requirements....

Owyn
May 11th, 2009, 10:03 AM
i want to take screens as jpg too, can you suggest an easy way?

btw, this code works only one of two times =\