C++ implements window screenshot

foreword

Students who are interested in learning C/C++ can read this article: C/C++ Tutorial

This article mainly introduces the method of realizing window screenshot in VC++ environment

1. Class library introduction

Since the image format invented by others is used, it is natural to use the corresponding class library to operate the image

The class library used in this article is a C++template library <atlimage.h>, as long as it is installed VS(visual studio)and C++the development kit is installed, there is this library

insert image description here

2. How to use

There is an image class in this libraryCImage

As long as you copy the device context ( Device context, for short DC) of a window to this class, you can save the image of the window as a picture, which is very simple to use

Mainly used classes and functions:

GetDeviceCaps(m_hDc, BITSPIXEL); //获取窗口DC像素的大小

GetDeviceCaps(m_hDc, HORZRES);  //获取窗口DC宽度

GetDeviceCaps(m_hDc, VERTRES);  //获取窗口DC高度

GetDpiForWindow(m_hWnd); //获取窗口单位英寸像素个数,一般电脑像素过大,windows为正常显示图标,会放大该数值,所以需要依靠该数值调整DC大小,否则截图会出现大小不适配的问题

CImage image;//用于图片操作的类

image.Create(m_width, m_hight, m_bitOfPix);//为该类创建与原窗口一样大小的DC

BitBlt(m_hImgDc, 0, 0, m_width, m_hight, m_hDc, 0, 0, SRCCOPY); //将窗口DC图像复制到image

 m_image.Save(name, Gdiplus::ImageFormatBMP); //将图像数据保存为对应文件

3. Code explanation

#include<string>
#include<atlimage.h>
using namespace std;
//name:保存的文件名
//hWnd:要截图的窗口句柄,NULL表示对桌面截图
bool SavePic(wstring name,HWND hWnd) {
    
    
	HDC hDc=NULL;
	hWnd = (hWnd == NULL) ? GetDesktopWindow() : hWnd; //判断窗口句柄是否为NULL,如果为NULL则默认获取桌面DC
	hDc = GetDC(hWnd); //获取DC
	if (hDc == NULL) return false;
	int bitOfPix = GetDeviceCaps(hDc, BITSPIXEL); //获取DC像素的大小
	int width = GetDeviceCaps(hDc, HORZRES);  //获取DC宽度
	int hight = GetDeviceCaps(hDc, VERTRES);  //获取DC高度
	UINT dpi = GetDpiForWindow(hWnd); //获取dpi
	float fold; //根据dpi计算放大倍数
	switch (dpi) {
    
     
	case 96:
		fold = 1;
		break;
	case 120:
		fold = 1.25;
		break;
	case 144:
		fold = 1.5;
		break;
	case 192:
		fold = 2;
		break;
	case 216:
		fold = 2.25;
		break;
	default:
		fold = 1;
		break;
	}
	width *= fold; //复原宽度
	hight *= fold; //复原高度
	CImage image;
	image.Create(width, hight, bitOfPix); //为图像类创建与窗口DC相同大小的DC
	BitBlt(image.GetDC(), 0, 0, width, hight, hDc, 0, 0, SRCCOPY); //将窗口DC图像复制到image
	image.Save(name.data(), Gdiplus::ImageFormatPNG); //保存为png格式图片文件
	image.ReleaseDC(); //释放DC
	ReleaseDC(hWnd, hDc); //释放DC资源
}
int main() {
    
    
	SavePic(L"1.png",NULL);//对桌面截图,保存为1.png文件
}

The image format parameters that support saving are:

Gdiplus::ImageFormatUndefined
Gdiplus::ImageFormatMemoryBMP 
Gdiplus::ImageFormatBMP
Gdiplus::ImageFormatEMF 
Gdiplus::ImageFormatWMF 
Gdiplus::ImageFormatJPEG 
Gdiplus::ImageFormatPNG 
Gdiplus::ImageFormatGIF 
Gdiplus::ImageFormatTIFF 
Gdiplus::ImageFormatEXIF 
Gdiplus::ImageFormatIcon 
Gdiplus::ImageFormatHEIF
Gdiplus::ImageFormatWEBP

The save file type can be determined according to the parameter suffix

After running, you can view the picture in the current program directory

insert image description here

Guess you like

Origin blog.csdn.net/weixin_50964512/article/details/123321723