CImage实现的全屏PNG截图
2016-02-03 13:39
387 查看
本来关于屏幕截图已经写了2篇博文了,但是效果都不是很理想,于是有了这个,直接使用CImage,代码简洁明了,比这个稳定:/article/10914852.html
最后截图生成的PNG文件保存在C:\ScreenShot文件夹下。
最后截图生成的PNG文件保存在C:\ScreenShot文件夹下。
#include <atlimage.h> #include <atltime.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(); }
相关文章推荐
- Android 实用代码片段
- Hadoop 统计一个目录的文件大小
- Objective-C 中 +load 与 +initialize
- ios 运行模式
- 如何用Fiddler对Android应用进行抓包
- mac 配置openfire
- Android基础:翻页控件Viewpager的使用方法,viewpager+fragment混合使
- html 5新特性 --用SVG绘制的微信logo
- HDU 1084 What Is Your Grade?
- python 笔记 break
- python 笔记 break
- LeetCode 260. Single Number III
- eclipse设置的断点上有一个斜杠,正常启动debug不能够跳转到debug页面
- JS简单实现文件上传实例代码(无需插件)
- C语言之基本算法35—数组上三角之积 主对角之积 副对角之积
- Android组件与进程间的通信-Handler
- beego和bee的安装、自动生成API框架
- Android-萤石云绕开登录页后台登陆
- Axis,axis2,Xfire以及cxf对比
- no auto summary的作用--学习笔记