Skip to content

Instantly share code, notes, and snippets.

@cust0m
Created September 25, 2015 17:36
Show Gist options
  • Save cust0m/47e2e2c061500c58b6a6 to your computer and use it in GitHub Desktop.
Save cust0m/47e2e2c061500c58b6a6 to your computer and use it in GitHub Desktop.
// takeashot.cpp : Defines the entry point for the console application.
//
#include
<stdio.h>
#include
<windows.h>
#include
<malloc.h>
#include
<tchar.h>
void
SaveBitmap(char *szFilename,HBITMAP hBitmap)
{
HDC hdc=NULL;
FILE* fp=NULL;
LPVOID pBuf=NULL;
BITMAPINFO bmpInfo;
BITMAPFILEHEADER bmpFileHeader;
hdc=GetDC(NULL);
ZeroMemory(&bmpInfo,
sizeof(BITMAPINFO));
bmpInfo.bmiHeader.biSize=
sizeof(BITMAPINFOHEADER);
GetDIBits(hdc,hBitmap,0,0,NULL,&bmpInfo,DIB_RGB_COLORS);
pBuf=malloc(bmpInfo.bmiHeader.biSizeImage);
bmpInfo.bmiHeader.biCompression=BI_RGB;
GetDIBits(hdc,hBitmap,0,bmpInfo.bmiHeader.biHeight,pBuf,&bmpInfo,DIB_RGB_COLORS);
fp=fopen(szFilename,
"wb");
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);
free(pBuf);
}
int
_tmain(int argc, _TCHAR* argv[])
{
int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
HWND hDesktopWnd = GetDesktopWindow();
HDC hDesktopDC = GetDC(hDesktopWnd);
HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC,
nScreenWidth, nScreenHeight);
SelectObject(hCaptureDC,hCaptureBitmap);
BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight,
hDesktopDC,0,0,SRCCOPY|CAPTUREBLT);
SaveBitmap(
"myshot.bmp",hCaptureBitmap);
system(
"pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment