[转] C++ CImage实现的全屏PNG截图

复制代码
#include <atlimage.h>
#include <atltime.h>
#include <conio.h>

//截取全屏保存为png
CString ScreenShot()
{
    HDC hDCScreen = ::GetDC(NULL);//首先获取到屏幕的句柄    
    int nBitPerPixel = GetDeviceCaps(hDCScreen, BITSPIXEL);//获取到每个像素的bit数目
    int nWidthScreen = GetDeviceCaps(hDCScreen, HORZRES);
    int nHeightScreen = GetDeviceCaps(hDCScreen, VERTRES);
    //创建一个CImage的对象
    CImage m_MyImage;
    //Create实例化CImage,使得其内部的画布大小与屏幕一致
    m_MyImage.Create(nWidthScreen, nHeightScreen, nBitPerPixel);
    //获取到CImage的 HDC,但是需要手动ReleaseDC操作,下面是MSDN的说明
    //Because only one bitmap can be selected into a device context at a time, 
    //you must call ReleaseDC for each call to GetDC.
    HDC hDCImg = m_MyImage.GetDC();
    //使用bitblt 将屏幕的DC画布上的内容 拷贝到CImage上
    BitBlt(hDCImg, 0, 0, nWidthScreen, nHeightScreen, hDCScreen, 0, 0, SRCCOPY);

    //保存到的文件名
    CString strFileName("C:\\");
    strFileName += _T("ScreenShot\\");
    CreateDirectory((LPCTSTR)strFileName, NULL);
    CTime t = CTime::GetCurrentTime();
    CString tt = t.Format(_T("%Y-%m-%d_%H-%M-%S"));
    strFileName += tt;
    strFileName += _T(".PNG");

    //直接保存吧
    m_MyImage.Save(strFileName, Gdiplus::ImageFormatPNG);

    //前面调用了GetDC所以需要调用ReleaseDC释放掉
    //详情请参见MSDN
    m_MyImage.ReleaseDC();
    return strFileName;
}

void main()
{
    ScreenShot();
    _getch();
}
复制代码

 

代码来源: https://blog.csdn.net/sunflover454/article/details/50629063

谢谢分享者 : )

作者:cjdty

出处:https://www.cnblogs.com/cjdty/p/9660966.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   Startu  阅读(1729)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
more_horiz
keyboard_arrow_up light_mode palette
选择主题
点击右上角即可分享
微信分享提示