C++桌面截图

本文介绍了使用C++进行桌面截图的方法,包括涉及的ScreenShot.h和ScreenShot.cpp文件的使用。同时,文章指出在Windows 10中如果设置缩放比例,如125%,会导致截图不完整的问题。为了解决这个问题,需要手动指定Bitmap的大小以获取完整的屏幕截图。
摘要由CSDN通过智能技术生成

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;
    
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值