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);
相关推荐
wen__xvn28 分钟前
每日一题洛谷P1914 小书童——凯撒密码c++
数据结构·c++·算法
云中飞鸿1 小时前
MFC中CString的Format、与XML中的XML_SETTEXT格式化注意
xml·c++·mfc
小小小白的编程日记2 小时前
List的基本功能(1)
数据结构·c++·算法·stl·list
努力可抵万难2 小时前
C++11新特性
开发语言·c++
ox00802 小时前
C++ 设计模式-策略模式
c++·设计模式·策略模式
egoist20232 小时前
【C++指南】一文总结C++类和对象【上】
开发语言·c++·类和对象·内存对齐·热榜·this指针·c++ 11
月上柳梢头&2 小时前
[C++ ]使用std::string作为函数参数时的注意事项
开发语言·c++·算法
商bol452 小时前
复习dddddddd
数据结构·c++·算法
C语言小火车3 小时前
C/C++高性能Web开发框架全解析:2025技术选型指南
c++·高并发架构·c++ web框架·drogon框架·异步协程模型·c++23模块化编译·异构计算调度
YYJ333_3334 小时前
蓝桥杯 r格式(高精度*低精度)
c++·算法·蓝桥杯