[转] 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
谢谢分享者 : )
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库