MFC 截取对话框生成图片、截取整个屏幕(可取黑白反色或者整体图片取反色)

cpp 复制代码
HWND hwnd = ::GetDesktopWindow();//截整个屏幕,用从这往下4句
	HDC hdc = ::GetDC(hwnd);
	CDC dc;
	dc.Attach(hdc);
	CRect rc,rcw;
	GetWindowRect(&rcw);
	GetClientRect(&rc);//只截对话框,用这句
	//rc.SetRect(0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));//截整个屏幕,用这句
	int iBitPerPixel = dc.GetDeviceCaps(BITSPIXEL);
	int iWidth = rc.Width();
	int iHeight = rc.Height();
	int pointx = rcw.left;
	int pointy = rcw.top;
	CDC memDC;
	memDC.CreateCompatibleDC(&dc);

	CBitmap memBitmap, *oldBitmap;

	memBitmap.CreateCompatibleBitmap(&dc, iWidth, iHeight);
	oldBitmap = memDC.SelectObject(&memBitmap);

	memDC.BitBlt(0, 0, iWidth, iHeight, &dc, pointx, pointy, SRCCOPY);

	BITMAP bmp;
	memBitmap.GetBitmap(&bmp);

	FILE *fp = fopen("test22222.bmp", "wb");
	BITMAPINFOHEADER bih;
	memset(&bih, 0, sizeof(bih));
	bih.biBitCount = bmp.bmBitsPixel;
	bih.biCompression = BI_RGB;//表示不压缩
	bih.biHeight = bmp.bmHeight;
	bih.biPlanes = 1;//位平面数,必须为1
	bih.biSize = sizeof(BITMAPINFOHEADER);
	bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;
	bih.biWidth = bmp.bmWidth;
	BITMAPFILEHEADER bfh;
	memset(&bfh, 0, sizeof(bfh));
	bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

	bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;
	bfh.bfType = (WORD)0x4d42;//必须表示"BM"

	fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp);
	fwrite(&bih, 1, sizeof(bih), fp);

	byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight];
	GetDIBits(memDC.m_hDC, (HBITMAP)memBitmap.m_hObject, 0, iHeight, p, (LPBITMAPINFO)&bih, DIB_RGB_COLORS);
	fwrite(p, 1, bmp.bmWidthBytes * bmp.bmHeight, fp);
	delete[] p;

	fclose(fp);

	memDC.SelectObject(oldBitmap);

取反色代码

cpp 复制代码
HWND hwnd = ::GetDesktopWindow();//截整个屏幕,用从这往下4句
	HDC hdc = ::GetDC(hwnd);
	CDC dc;
	dc.Attach(hdc);
	CRect rc,rcw;
	GetWindowRect(&rcw);
	GetClientRect(&rc);//只截对话框,用这句
	//rc.SetRect(0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));//截整个屏幕,用这句
	int iBitPerPixel = dc.GetDeviceCaps(BITSPIXEL);
	int iWidth = rc.Width();
	int iHeight = rc.Height();
	int pointx = rcw.left;
	int pointy = rcw.top;
	CDC memDC;
	memDC.CreateCompatibleDC(&dc);
	memDC.SetROP2(R2_NOT);
	CBitmap memBitmap, *oldBitmap;

	memBitmap.CreateCompatibleBitmap(&dc, iWidth, iHeight);
	oldBitmap = memDC.SelectObject(&memBitmap);

	memDC.BitBlt(0,0 , iWidth, iHeight, &dc, pointx, pointy, SRCCOPY);

	BITMAP bmp;
	memBitmap.GetBitmap(&bmp);
	
	
	FILE *fp = fopen("Data\\test22222.bmp", "wb");
	BITMAPINFOHEADER bih;
	memset(&bih, 0, sizeof(bih));
	bih.biBitCount = bmp.bmBitsPixel;
	bih.biCompression = BI_RGB;//表示不压缩
	bih.biHeight = bmp.bmHeight;
	bih.biPlanes = 1;//位平面数,必须为1
	bih.biSize = sizeof(BITMAPINFOHEADER);
	bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;
	bih.biWidth = bmp.bmWidth;
	BITMAPFILEHEADER bfh;
	memset(&bfh, 0, sizeof(bfh));
	bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

	bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;
	bfh.bfType = (WORD)0x4d42;//必须表示"BM"

	fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp);
	fwrite(&bih, 1, sizeof(bih), fp);

	byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight];
	

	GetDIBits(memDC.m_hDC, (HBITMAP)memBitmap.m_hObject, 0, iHeight, p, (LPBITMAPINFO)&bih, DIB_RGB_COLORS);
	int pixels = bmp.bmWidth *bmp.bmHeight;
	//for (int i = 0; i < pixels; i++)// 整体取反色
	//{
	//	BYTE red = p[i * 4];
	//	BYTE green = p[i * 4 + 1];
	//	BYTE blue = p[i * 4 + 2];
	//	p[i * 4] = 255 - red;
	//	p[i * 4 + 1] = 255 - green;
	//	p[i * 4 + 2] = 255 - blue;

	//}
	// 黑取白
	for (int i = 0; i < pixels; i++)
	{
		BYTE red = p[i * 4];
		BYTE green = p[i * 4 + 1];
		BYTE blue = p[i * 4 + 2];
		if (red == 0 && green == 0 && blue == 0) {
			p[i * 4] = 255 - red;
			p[i * 4 + 1] = 255 - green;
			p[i * 4 + 2] = 255 - blue;
		}
	}

	fwrite(p, 1, bmp.bmWidthBytes * bmp.bmHeight, fp);
	delete[] p;

	fclose(fp);

	memDC.SelectObject(oldBitmap);
相关推荐
君鼎3 小时前
C++设计模式——单例模式
c++·单例模式·设计模式
刚入门的大一新生4 小时前
C++初阶-string类的模拟实现与改进
开发语言·c++
小冯的编程学习之路5 小时前
【软件测试】:推荐一些接口与自动化测试学习练习网站(API测试与自动化学习全攻略)
c++·selenium·测试工具·jmeter·自动化·测试用例·postman
C++ 老炮儿的技术栈6 小时前
什么是函数重载?为什么 C 不支持函数重载,而 C++能支持函数重载?
c语言·开发语言·c++·qt·算法
猪八戒1.06 小时前
C++ 回调函数和Lambda表达式
c++
源远流长jerry7 小时前
匿名函数lambda、STL与正则表达式
c++
tan180°8 小时前
Linux进程信号处理(26)
linux·c++·vscode·后端·信号处理
一只鱼^_8 小时前
牛客练习赛138(首篇万字题解???)
数据结构·c++·算法·贪心算法·动态规划·广度优先·图搜索算法
李匠20248 小时前
C++GO语言微服务之Dockerfile && docker-compose②
c++·容器
2301_803554529 小时前
c++和c的不同
java·c语言·c++