anyways , my first post will demonstrate how to take a screen shot and dump it to a .bmp file.
it has very little annotations or comments , because i have no time. but its here for people who want to use it, but if people want to learn from it / me, then i will spread my knowledge in the near future.
one should include windows.h in their program when using this fucntion and make sure Gdi32.lib is linked in as well. If your not sure how this is done, it is
- Code: Select all
-lGDI32
- Code: Select all
void TakeScreenShot(char* filename)
{
keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY, 0); //emulate printscreen key
keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
HBITMAP h; //declare a variable of type hbitmap ( under windows.h i think )
/* the way print screen works under windows is that it dumps the contents of your screen to the clipboard, put simply. we will get the contents of the clipboard and put it into our buffer. but first we must open the clipboard*/
OpenClipboard(NULL);
h = (HBITMAP)GetClipboardData(CF_BITMAP);//self explanatory, puts the contents of the clipboard into our respective buffer
CloseClipboard(); // good programming
HDC hdc=NULL;
FILE*fp=NULL;
LPVOID pBuf=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); // func under windows.h
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); // error
break;
}
bmpInfo.bmiHeader.biCompression=BI_RGB;
GetDIBits(hdc,h,0,bmpInfo.bmiHeader.biHeight,pBuf, &bmpInfo, DIB_RGB_COLORS);
if((fp = fopen(filename,"wb"))==NULL) // checks to see if you provided a valid path and file name
{
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);
//begin write procedure
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);
}
should have no problems , although i didnt extensivly test it. feel free to modify.
it should also be noted that this function expects a pointer of type char. supplying just a file name will dump the file to the directory of the program , HOWEVER if you wish to send it to another path, you should always use double black slashes. Example: C://Dir1//hi.bmp
and dont forget the .bmp extention. im sure you all know this , but this is sp00nfed after all (:
have fun with it , expect more - higher level fun snippetes in the future for those of you who want to get their hands a bit dirty.

