码迷,mamicode.com
首页 > Windows程序 > 详细

windows gdi截图保存bmp

时间:2017-10-13 12:36:28      阅读:21      评论:0      收藏:0      [点我收藏+]

标签:eof   mpi   height   highlight   window   form   tee   des   href   

/*
 * windows gdi截屏保存bmp文件
 */
#include "stdio.h"
#include "windows.h"

void save_bgrdata_to_bmp(void *data, int size, int width, int height,char *bmpPath,int bpp)  
{  
	BITMAPFILEHEADER bmpheader;
	BITMAPINFOHEADER bmpinfo;
	FILE *fp = fopen(bmpPath,"wb+");

	bmpheader.bfType = 0x4d42;  
	bmpheader.bfReserved1 = 0;  
	bmpheader.bfReserved2 = 0;  
	bmpheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);  
	bmpheader.bfSize = bmpheader.bfOffBits + width*height*bpp/8;  

	bmpinfo.biSize = sizeof(BITMAPINFOHEADER);  
	bmpinfo.biWidth = width;  
	bmpinfo.biHeight = -height;  
	bmpinfo.biPlanes = 1;  
	bmpinfo.biBitCount = bpp;  
	bmpinfo.biCompression = BI_RGB;  
	bmpinfo.biSizeImage = (width*bpp+31)/32*4*height;  
	bmpinfo.biXPelsPerMeter = 100;  
	bmpinfo.biYPelsPerMeter = 100;  
	bmpinfo.biClrUsed = 0;  
	bmpinfo.biClrImportant = 0;  

	fwrite(&bmpheader, sizeof(bmpheader), 1, fp);  
	fwrite(&bmpinfo, sizeof(bmpinfo), 1, fp);  
	fwrite(data, size, 1, fp);  

	fclose(fp);  
}  

int main(int argc,char *argv)
{
	HWND dtHwnd = GetDesktopWindow();
	HDC dtHdc = GetDC(dtHwnd);

	int dtWidth = GetSystemMetrics(SM_CXSCREEN);
	int dtHeight = GetSystemMetrics(SM_CYSCREEN);

	HDC dtHdcBuf = CreateCompatibleDC(NULL);
	HBITMAP hBitmap = CreateCompatibleBitmap(dtHdc,dtWidth,dtHeight);
	SelectObject(dtHdcBuf, hBitmap);
	BitBlt(dtHdcBuf, 0, 0, dtWidth, dtHeight, dtHdc, 0, 0, SRCCOPY);

	int bitSize = dtWidth*dtHeight*4;
	BYTE *bit = new BYTE[bitSize];
	LONG re = GetBitmapBits(hBitmap, bitSize, bit);
	if(bit && re)
	{
		save_bgrdata_to_bmp(bit, bitSize, dtWidth, dtHeight,"gdi.bmp",32);
		free(bit);
	}

	DeleteObject(hBitmap);
	DeleteDC(dtHdcBuf);

	ReleaseDC(dtHwnd,dtHdc);
	return 1;
}

gdi_bmp.cpp

windows gdi截图保存bmp

标签:eof   mpi   height   highlight   window   form   tee   des   href   

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!