Snippet
// Takes a snapshot of the screen and saves it to the disk
#include <windows.h>
// by: Wolf Coder
#include <windows.h>
RGBQUAD tempimg[1024*768];
RGBTRIPLE outimg[1024*768];
int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,PSTR szcmdline,int icmdshow)
{
HDC hdc,xdc,mdc;
HWND hwnd;
HBITMAP hbitmap; // For frame locking
HPEN hpen = CreatePen(PS_SOLID,0,RGB(0,0,0));
HBRUSH hbrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
if(LockWindowUpdate(hwnd = GetDesktopWindow()))
{
hdc = GetDCEx(hwnd,NULL,DCX_CACHE | DCX_LOCKWINDOWUPDATE);
xdc = CreateCompatibleDC(hdc);
mdc = CreateCompatibleDC(hdc);
// Flashshot the screen
hbitmap = CreateCompatibleBitmap(hdc,1024,768);
SelectObject(xdc,hbitmap);
BitBlt(xdc,0,0,1024,768,hdc,0,0,SRCCOPY);
// Open a channel to bitmap
DWORD written;
HANDLE hfile = CreateFile("snapshot.bmp",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
// Fill out a bitmap header form
BITMAPFILEHEADER bfh;
bfh.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
bfh.bfSize = sizeof(BITMAPFILEHEADER);
bfh.bfType = 0x4d42;
WriteFile(hfile,&bfh,sizeof(bfh),&written,NULL);
// Fill out an info form
BITMAPINFOHEADER bih;
bih.biBitCount = 24;
bih.biClrImportant = 0;
bih.biClrUsed = 0;
bih.biCompression = BI_RGB;
bih.biHeight = 768;
bih.biPlanes = 1;
bih.biSize = sizeof(bih);
bih.biSizeImage = 3*1024*768;
bih.biWidth = 1024;
bih.biXPelsPerMeter = 2400;
bih.biYPelsPerMeter = 2400;
WriteFile(hfile,&bih,sizeof(bih),&written,NULL);
// Copy the bits to a buffer
GetBitmapBits(hbitmap,1024*768*4,&tempimg);
// Write the bits to a second buffer
for(int y = 0;y < 768;y++)
{
for(int x = 0;x < 1024;x++)
{
int index = ((767-y)*1024)+x;
int from_index = x+y*1024;
outimg[index].rgbtBlue = tempimg[from_index].rgbBlue;
outimg[index].rgbtGreen = tempimg[from_index].rgbGreen;
outimg[index].rgbtRed = tempimg[from_index].rgbRed;
}
}
// Write to file
WriteFile(hfile,&outimg[0],1024*768*3,&written,NULL);
// File is done
CloseHandle(hfile);
// Now pack everything away
DeleteObject(hbitmap);
ReleaseDC(hwnd,hdc);
DeleteDC(hdc);
DeleteDC(xdc);
DeleteDC(mdc);
LockWindowUpdate(hwnd);
}
DeleteObject(hpen);
DeleteObject(hbrush);
MessageBox(hwnd,"SCREENSHOT TAKEN","complete",MB_OK);
return 1;
}
Copy & Paste
|