ScreenShot.h
#include <atlimage.h>
void ScreenShot(LPCTSTR s);
ScreenShot.cpp
#include "ScreenShot.h"
#include <atlimage.h>
void ScreenShot(LPCTSTR s)
{
HDC hdcSrc = GetDC(NULL);
int nBitPerPixel = GetDeviceCaps(hdcSrc, BITSPIXEL);
int nWidth = GetDeviceCaps(hdcSrc, HORZRES);
int nHeight = GetDeviceCaps(hdcSrc, VERTRES);
CImage image;
image.Create(nWidth, nHeight, nBitPerPixel);
BitBlt(image.GetDC(), 0, 0, nWidth, nHeight, hdcSrc, 0, 0, SRCCOPY);
ReleaseDC(NULL, hdcSrc);
image.ReleaseDC();
image.Save(s, Gdiplus::ImageFormatPNG);//ImageFormatJPEG
}
调用
const char *str = "D:\\test11.bmp";//多字节字符集可以这么做,如果是Unicode编码,则需要转换成CString后赋值给LPCTSTR
ScreenShot((LPCTSTR)(CString)str);
ScreenShot(str);
还有一点注意的是,如果在win10里设置了缩放,比如缩放125%,那么截取的就不是完整的屏幕了。因为GetDeviceCaps返回的是除以125%的结果,1920变成了1536。解决方法目前只有手动指定bitmap的大小了.
第二种方法
#include <windows.h>
HBITMAP CopyScreenToBitmap(LPRECT lpRect)
{
HDC hScrDC, hMemDC;
// 屏幕和内存设备描述表
HBITMAP hBitmap, hOldBitmap;
// 位图句柄
int nX, nY, nX2, nY2;

本文介绍了使用C++进行桌面截图的方法,包括涉及的ScreenShot.h和ScreenShot.cpp文件的使用。同时,文章指出在Windows 10中如果设置缩放比例,如125%,会导致截图不完整的问题。为了解决这个问题,需要手动指定Bitmap的大小以获取完整的屏幕截图。
最低0.47元/天 解锁文章
3324

被折叠的 条评论
为什么被折叠?



