找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 16596|回复: 2

命令行截屏工具kping发布

[复制链接]
发表于 2017-5-17 21:35:54 | 显示全部楼层 |阅读模式
Kping [最新升级版 v1.1]修复个别漏洞,请下载附件7KB。
命令行截屏工具,最大支持9参数截屏,参数输入多少不限,智能高速截屏,自动位深处理,支持多种格式输出,强悍无与伦比。代码经过高度优化,比QQ截图更清晰,速度更快,虽然不是鼠标类截图,但适合批处理高速调用,高质量截屏。

附件链接: https://pan.baidu.com/s/1eS12zcnI4ULVKsOaRfQFog?pwd=dcin


摘要:
=============================================================================
命令行截图工具,支持截图位深1、4、8、16、32、48、64,可选保存格式有bmp、jpg、
png、gif、tif等;支持多种附件参数,如截图延时、截图间隔、截图尺寸、截图起始位
置等。
=============================================================================

用法:
kping [-选项] [选项对应的参数] ...
   -n  截图数
   -o  输出图像名称
   -b  图像位深度
   -d  延迟时间
   -t  屏幕截图间隔时间
   -x  截图开始的x坐标
   -y  截图开始的y坐标
   -w  屏幕截图的宽度
   -h  屏幕截图的高度

举例:

  1. REM 延迟100毫秒之后开始截图,每隔8毫秒截屏一次,共截图64张,保存为bmp八位图,截图开始坐标点(100,200), 截图宽度:800、截图高度:320。
  2. kping -d100 -t8 -n64 -otest.bmp -b8 -x100 -y200 -w800 -h320

  3. REM 当然也支持省参数截屏,写几个参数都行
  4. kping -o.bmp
复制代码


源码:(支持各类编译器编译,支持宽窄字符各版本编译;由于GDI是win下的库,暂时不支持linux平台,但后期升级将会支持linux和arm手机平台)

  1. /*
  2.         CONSOLE SCREENSHOT TOOL, COPYRIGHT@2017~2019 BY LEO, VERSION 1.1
  3.         KPING.EXE
  4.         LINK GDI32 GDIPLUS SHLWAPI USER32

  5.         UNICODE COMPILATION:
  6.         --> G++ kping.cpp -lgdi32 -lgdiplus -D _UNICODE -D UNICODE -municode -O2 -static
  7.         --> CL  kping.cpp /O2 /Oy- /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /MD

  8.         ANSI COMPILATION:
  9.         --> G++ kping.cpp -lgdi32 -lgdiplus -O2 -static
  10.         --> CL  kping.cpp /O2 /Oy- /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /MD
  11. */
  12. #include <stdio.h>
  13. #include <windows.h>
  14. #include <tchar.h>
  15. #include <io.h>

  16. #ifndef _FILE_EXIST
  17. #define _FILE_EXIST 0
  18. #endif

  19. #if defined _MSC_VER
  20. #include <gdiplus.h>
  21. #pragma comment(lib, "gdi32.lib")
  22. #pragma comment(lib, "gdiplus.lib")
  23. #pragma comment(lib, "user32.lib")
  24. #else
  25. //兼容MINGW32
  26. #include <gdiplus\gdiplus.h>
  27. #endif

  28. #ifndef _UNICODE
  29. #define TCHARFORMAT CHAR
  30. #else
  31. #define TCHARFORMAT WCHAR
  32. #endif

  33. //GDI+命名空间
  34. using namespace Gdiplus;

  35. //定义帮助说明
  36. #define HELP_INFORMATION "\
  37. kping v1.1 - Screenshot Tool - Copyright (C) 2017-2019 By Leo\n\
  38. Usage: kping {[option] [parameter]} \n\
  39. \n\
  40. General options:\n\
  41.   -n   The number of screenshots\n\
  42.   -o   Output image name\n\
  43.   -b   Image bit depth\n\
  44.   -d   Delay time\n\
  45.   -t   Screenshot interval time\n\
  46.   -x   The x-coordinate of the beginning of the screenshot\n\
  47.   -y   The y-coordinate of the beginning of the screenshot\n\
  48.   -w   The width of the screenshots\n\
  49.   -h   The height of the screenshots\n\
  50. \n\
  51. Official website:\n\
  52.        http://www.bathome.net/thread-44149-1-1.html\n"

  53. #define FILE_EXIST 0

  54. //定义位深度枚举
  55. INT BITDEEP[65] = {0,PixelFormat1bppIndexed,0,0,PixelFormat4bppIndexed,0,0,0,PixelFormat8bppIndexed,0,0,0,0,0,0,0,PixelFormat16bppRGB555,0,0,0,0,0,0,0,PixelFormat24bppRGB,0,0,0,0,0,0,0,PixelFormat32bppARGB,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,PixelFormat48bppRGB,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,PixelFormat64bppARGB};

  56. //定义截图区域结构体
  57. typedef struct
  58. {
  59.         int startX;
  60.         int startY;
  61.         int capWITH;
  62.         int capHIGH;
  63. } PAREA;

  64. //开关解析
  65. int OPTIND=1, OPTOPT;
  66. TCHAR* OPTARG;
  67. int _tgetopt(int nargc, TCHAR* nargv[], TCHAR* ostr)
  68. {
  69.         static TCHAR* place=(TCHAR*)_T("");
  70.         static TCHAR* lastostr=NULL;
  71.         register TCHAR* oli;

  72.         if(ostr!=lastostr)
  73.         {
  74.                 lastostr=ostr;
  75.                 place=(TCHAR*)_T("");
  76.         }

  77.         if(!*place)
  78.         {
  79.                 if(
  80.                     (OPTIND>=nargc)                  ||
  81.                     (*(place=nargv[OPTIND])!=(TCHAR)_T('-'))||
  82.                     (!*(++place))
  83.                 )
  84.                 {
  85.                         place=(TCHAR*)_T("");
  86.                         return _TEOF;
  87.                 }
  88.                 if (*place == (TCHAR)_T('-') && *(place+1) == (TCHAR)_T('\0'))
  89.                 {
  90.                         ++OPTIND;
  91.                         return _TEOF;
  92.                 }
  93.         }

  94.         if (
  95.             (OPTOPT=*place++)==(TCHAR)_T(':') ||
  96.             !(oli=(TCHAR*)_tcschr((TCHARFORMAT*)ostr, (TCHAR)OPTOPT))
  97.         )
  98.         {
  99.                 if(!*place)
  100.                 {
  101.                         ++OPTIND;
  102.                 }
  103.         }

  104.         if (oli != NULL && *(++oli) != (TCHAR)_T(':'))
  105.         {
  106.                 OPTARG=NULL;
  107.                 if(!*place)
  108.                 {
  109.                         ++OPTIND;
  110.                 }
  111.         }
  112.         else
  113.         {
  114.                 if(*place)
  115.                 {
  116.                         OPTARG=place;
  117.                 }
  118.                 else if(nargc<=++OPTIND)
  119.                 {
  120.                         place=(TCHAR*)_T("");
  121.                 }
  122.                 else
  123.                 {
  124.                         OPTARG=nargv[OPTIND];
  125.                 }
  126.                 place=(TCHAR*)_T("");
  127.                 ++OPTIND;
  128.         }
  129.         return OPTOPT;
  130. }

  131. //获取编码器CLSID
  132. BOOL GetEncoderClsid(TCHAR* expNAME, CLSID* pClsid)
  133. {
  134.         UINT n=0, s=0;
  135.         ImageCodecInfo* pInfo=NULL;
  136.         GetImageEncodersSize(&n, &s);
  137.         if(s==0)
  138.         {
  139.                 return FALSE;
  140.         }
  141.         pInfo=(ImageCodecInfo*)(malloc(s));
  142.         if(pInfo==NULL)
  143.         {
  144.                 return FALSE;
  145.         }
  146.         GetImageEncoders(n, s, pInfo);
  147.         for(UINT i=0; i<n; i++)
  148.         {
  149.                 if(lstrlen(expNAME)<2 || *expNAME==_T('\0'))
  150.                 {
  151.                         ;
  152.                 }
  153.                 else if(
  154.                     *(pInfo[i].MimeType+6) == *expNAME     &&
  155.                     *(pInfo[i].MimeType+7) == *(expNAME+1)
  156.                 )
  157.                 {
  158.                         *pClsid=pInfo[i].Clsid;
  159.                         free(pInfo);
  160.                         return TRUE;
  161.                 }
  162.         }
  163.         free(pInfo);
  164.         return FALSE;
  165. }

  166. //截图核心
  167. int CaptureScreenToImage(TCHAR* srcNAME, PAREA* inpa, int imgBIT, int intervalTIME, int countNUM)
  168. {
  169.         //切分文件名
  170.         TCHAR *imgNAME=NULL, *outDIR=NULL, *expNAME=NULL, *lpwstr=(TCHAR*)_tcsrchr((TCHARFORMAT*)srcNAME, _T('\\'));
  171.         if(lpwstr==NULL)
  172.         {
  173.                 outDIR =(TCHAR*)_T(".\");
  174.                 imgNAME = srcNAME;
  175.         }
  176.         else
  177.         {
  178.                 *lpwstr=_T('\0'), outDIR=srcNAME, imgNAME=++lpwstr;
  179.                 if(_taccess((TCHARFORMAT*)outDIR, _FILE_EXIST) != 0)
  180.                 {
  181.                         _ftprintf(stderr, _T("The outdir don't exist\n"));
  182.                         exit(1);
  183.                 }
  184.         }
  185.         lpwstr=(TCHAR*)_tcsrchr((TCHARFORMAT*)imgNAME, _T('.'));
  186.         if(lpwstr==NULL)
  187.         {
  188.                 _ftprintf(stderr, _T("Wrong image output format\n"));
  189.                 exit(1);
  190.         }
  191.         else
  192.         {
  193.                 *lpwstr=_T('\0'), expNAME = ++lpwstr;
  194.         }

  195.         //获取桌面HWND句柄
  196.         HWND hDSK=GetDesktopWindow();
  197.         //获取桌面DC
  198.         HDC  hDC =GetDC(hDSK);
  199.         //创建兼容桌面DC
  200.         HDC  pDC =CreateCompatibleDC(hDC);
  201.         //创建兼容位图
  202.         HBITMAP pHBITMAP =CreateCompatibleBitmap(hDC, inpa->capWITH, inpa->capHIGH);
  203.         //绑定兼容DC与位图
  204.         HGDIOBJ preOBJ   =SelectObject(pDC, pHBITMAP);

  205.         //获取编码器
  206.         CLSID clsid;
  207.         if(! GetEncoderClsid(expNAME, &clsid))
  208.         {
  209.                 _ftprintf(stderr, _T("Encode image failed\n"));
  210.                 exit(1);
  211.         }

  212.         //申请动态内存
  213.         TCHAR* outNAME =new TCHAR[_MAX_PATH];

  214. #ifndef UNICODE
  215.         WCHAR* outNAMEW=new WCHAR[_MAX_PATH];
  216. #endif

  217.         //进入截图主循环
  218.         for(int i=1; i<= countNUM; i++)
  219.         {
  220.                 //编号文件名
  221.                 _stprintf((TCHARFORMAT*)outNAME, _T("%s\\%s%03d.%s"), outDIR, imgNAME, i, expNAME);

  222.                 //开始捕获屏幕
  223.                 BitBlt(pDC, 0, 0, inpa->capWITH, inpa->capHIGH, hDC, inpa->startX, inpa->startY, SRCCOPY);

  224.                 //HBITMAP转Bitmap
  225.                 Bitmap* bitIMG=new Bitmap(pHBITMAP, NULL);

  226.                 //位化深度
  227.                 Bitmap* covIMG=bitIMG->Clone(0, 0, inpa->capWITH, inpa->capHIGH, imgBIT);

  228.                 //保存截屏
  229. #ifndef UNICODE
  230.                 int wLen=MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, outNAME, -1, NULL, 0);
  231.                 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, outNAME, -1, outNAMEW, wLen);
  232.                 outNAMEW[wLen]='\0';
  233.                 if(covIMG->Save((LPCWSTR)outNAMEW, &clsid, NULL) != S_OK)
  234. #else
  235.                 if(covIMG->Save((LPCWSTR)outNAME,  &clsid, NULL) != S_OK)
  236. #endif
  237.                 {
  238.                         _ftprintf(stderr, _T("Save image failed\n"));
  239.                         exit(1);
  240.                 }

  241.                 //局部释放
  242.                 delete bitIMG;
  243.                 delete covIMG;

  244.                 //间隔时间
  245.                 Sleep(intervalTIME);
  246.         }

  247.         //释放动态内存
  248.         delete outNAME;

  249. #ifndef UNICODE
  250.         delete outNAMEW;
  251. #endif

  252.         //释放DC与绑定,恢复之前模式
  253.         DeleteDC(pDC);
  254.         DeleteDC(hDC);
  255.         SelectObject(pDC, preOBJ);

  256.         return 0;
  257. }

  258. #if defined _MSC_VER
  259. #else
  260. //兼容MINGW32
  261. extern "C"
  262. #endif

  263. //主函数入口
  264. int _tmain(int argc, TCHAR** argv)
  265. {
  266.         if(argc<2)
  267.         {
  268.                 //无参数则退出
  269.                 fprintf(stdout, HELP_INFORMATION);
  270.                 exit(0);
  271.         }

  272.         //设置传入参数
  273.         TCHAR* srcNAME=NULL;
  274.         INT K=-1, countNUM=1, imgBIT=PixelFormat32bppARGB, delayTIME=0, intervalTIME=0;
  275.         PAREA pmi= {0}, *inpa=&pmi;

  276.         //开关解析
  277.         while((K=_tgetopt(argc, argv, (TCHAR*)_T("n:b:t:d:r:o:x:y:w:h:N:B:T:D:R:O:X:Y:W:H:")))!=_TEOF)
  278.         {
  279.                 switch(K)
  280.                 {
  281.                 case 'n':
  282.                 case 'N':
  283.                         if(OPTARG !=NULL)
  284.                         {
  285.                                 countNUM=_ttoi((TCHARFORMAT*)OPTARG);
  286.                                 if(countNUM <1)
  287.                                 {
  288.                                         _ftprintf(stderr, _T("The number of screenshots can't be less than 1\n"));
  289.                                         exit(1);
  290.                                 }
  291.                         }
  292.                         break;

  293.                 case 't':
  294.                 case 'T':
  295.                         if(OPTARG !=NULL)
  296.                         {
  297.                                 intervalTIME=_ttoi((TCHARFORMAT*)OPTARG);
  298.                                 if(intervalTIME <0)
  299.                                 {
  300.                                         _ftprintf(stderr, _T("The interval time can't be less than 0\n"));
  301.                                         exit(1);
  302.                                 }
  303.                         }
  304.                         break;

  305.                 case 'd':
  306.                 case 'D':
  307.                         if(OPTARG !=NULL)
  308.                         {
  309.                                 delayTIME =_ttoi((TCHARFORMAT*)OPTARG);
  310.                                 if(delayTIME <0)
  311.                                 {
  312.                                         _ftprintf(stderr, _T("The waiting time can't be less than 0\n"));
  313.                                         exit(1);
  314.                                 }
  315.                         }
  316.                         break;

  317.                 case 'o':
  318.                 case 'O':
  319.                         srcNAME=OPTARG;
  320.                         break;

  321.                 case 'b':
  322.                 case 'B':
  323.                         if(OPTARG !=NULL)
  324.                         {
  325.                                 int i =_ttoi((TCHARFORMAT*)OPTARG);
  326.                                 if(i<0 || 64<i)
  327.                                 {
  328.                                         _ftprintf(stderr, _T("Wrong bit depth\n"));
  329.                                         exit(1);
  330.                                 }
  331.                                 imgBIT=BITDEEP[i];
  332.                         }
  333.                         break;

  334.                 case 'x':
  335.                 case 'X':
  336.                         if(OPTARG !=NULL)
  337.                         {
  338.                                 inpa->startX =_ttoi((TCHARFORMAT*)OPTARG);
  339.                                 if(inpa->startX <0)
  340.                                 {
  341.                                         _ftprintf(stderr, _T("The swith '-x' Need a positive number\n"));
  342.                                         exit(1);
  343.                                 }
  344.                         }
  345.                         break;

  346.                 case 'y':
  347.                 case 'Y':
  348.                         if(OPTARG !=NULL)
  349.                         {
  350.                                 inpa->startY =_ttoi((TCHARFORMAT*)OPTARG);
  351.                                 if(inpa->startY <0)
  352.                                 {
  353.                                         _ftprintf(stderr, _T("The swith '-y' Need a positive number\n"));
  354.                                         exit(1);
  355.                                 }
  356.                         }
  357.                         break;

  358.                 case 'w':
  359.                 case 'W':
  360.                         if(OPTARG !=NULL)
  361.                         {
  362.                                 inpa->capWITH =_ttoi((TCHARFORMAT*)OPTARG);
  363.                                 if(inpa->capWITH <0)
  364.                                 {
  365.                                         _ftprintf(stderr, _T("The swith '-w' Need a positive number\n"));
  366.                                         exit(1);
  367.                                 }
  368.                         }
  369.                         break;

  370.                 case 'h':
  371.                 case 'H':
  372.                         if(OPTARG !=NULL)
  373.                         {
  374.                                 inpa->capHIGH =_ttoi((TCHARFORMAT*)OPTARG);
  375.                                 if(inpa->capHIGH <0)
  376.                                 {
  377.                                         _ftprintf(stderr, _T("The swith '-h' Need a positive number"));
  378.                                         exit(1);
  379.                                 }
  380.                         }
  381.                         break;

  382.                 default:
  383.                         _ftprintf(stderr, _T("Unknown switch '-%c'\n"), K);
  384.                         exit(1);
  385.                 }
  386.         }

  387.         if(srcNAME == NULL)
  388.         {
  389.                 _ftprintf(stderr, _T("Needs output image name\n"));
  390.                 exit(1);
  391.         }

  392.         //初始化GDI+
  393.         GdiplusStartupInput gdiplusstartupinput;
  394.         ULONG_PTR gdiplustoken;
  395.         GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);

  396.         //获取屏幕尺寸
  397.         PAREA srcPAREA=
  398.         {
  399.                 GetSystemMetrics(SM_XVIRTUALSCREEN),
  400.                 GetSystemMetrics(SM_YVIRTUALSCREEN),
  401.                 GetSystemMetrics(SM_CXVIRTUALSCREEN),
  402.                 GetSystemMetrics(SM_CYVIRTUALSCREEN)

  403.         }, *ps=&srcPAREA;

  404.         //当参数缺省时 修正截图区域
  405.         if(inpa->capWITH==0)
  406.         {
  407.                 inpa->capWITH = ps->capWITH - inpa->startX;
  408.         }
  409.         if(inpa->capHIGH==0)
  410.         {
  411.                 inpa->capHIGH = ps->capHIGH - inpa->startY;
  412.         }

  413.         //执行等待
  414.         Sleep(delayTIME);

  415.         //截图核心
  416.         CaptureScreenToImage((LPTSTR)srcNAME, inpa, imgBIT, intervalTIME, countNUM);

  417.         //关闭GDI+
  418.         GdiplusShutdown(gdiplustoken);
  419.         return 0;
  420. }
复制代码

评分

参与人数 3技术 +3 收起 理由
hztccy + 1 感谢分享
freesoft00 + 1 +1
ivor + 1 开源必须顶

查看全部评分

发表于 2017-5-21 21:54:09 | 显示全部楼层
开源,   666
发表于 2017-5-23 11:30:41 | 显示全部楼层
不错,不错
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-16 22:40 , Processed in 0.021502 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表