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);
相关推荐
再睡一夏就好1 小时前
【C++闯关笔记】详解多态
c语言·c++·笔记·学习·语法·1024程序员节
喜欢吃燃面4 小时前
数据结构算法题:list
开发语言·c++·学习·算法·1024程序员节
胡萝卜3.04 小时前
C++ list核心接口与实战技巧
数据结构·c++·list·list使用
。TAT。4 小时前
C++ - 多态
开发语言·c++·学习·1024程序员节
mit6.8245 小时前
[cpprestsdk] JSON类--数据处理 (`json::value`, `json::object`, `json::array`)
c++·1024程序员节
武当豆豆5 小时前
C++编程学习(第42天)
开发语言·c++·学习
咬_咬5 小时前
C++仿muduo库高并发服务器项目:Channel模块
linux·c++·channel·1024程序员节·muduo·高并发服务器
jdlxx_dongfangxing6 小时前
C++ STL 容器与算法详解
开发语言·c++·1024程序员节
m0_748233646 小时前
C++ 模板初阶:从函数重载到泛型编程的优雅过渡
java·c++·算法·1024程序员节
Hyt的笔记本6 小时前
【C++】异步操作
c++·1024程序员节