windows gdi截图保存bmp

2021-05-06 15:27

阅读:433

标签: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   

原文地址:http://www.cnblogs.com/hatsusakana/p/7660300.html


评论


亲,登录后才可以留言!