Board logo

标题: [文件操作] [已解决]批处理有没有方法或工具根据提供的屏幕坐标截图? [打印本页]

作者: zhanglei1371    时间: 2015-6-20 10:36     标题: [已解决]批处理有没有方法或工具根据提供的屏幕坐标截图?

网上搜了好长时间,发现截图都是截全屏的,我想用变量计算出需要截屏的矩形区域,提供出屏幕上四个角的坐标,或者左上角的坐标,区域的宽度和高度,这四个值,能否实现截图呢?

网上搜到个类似的解决方案:但是不知怎么用:
  1. Private Declare Function icePub_saveScreen Lib "icePubDll.dll" (ByVal bmpFile As String) As Integer
  2. Dim str1 As String
  3. Dim a2 As Integer
  4. str1 = App.Path + "\1.bmp"
  5. a2 = icePub_saveScreen(str1)
  6. Private Declare Function icePub_saveScreenJpg Lib "icePubDll.dll" (ByVal jpgFile As String) As Integer
  7. Dim str1 As String
  8. Dim a2 As Integer
  9. str1 = App.Path + "\1.jpg"
  10. a2 = icePub_saveScreenJpg(str1)
  11. Private Declare Function icePub_saveSubScreen Lib "icePubDll.dll" (ByVal bmpFile As String, ByVal startX As Integer,ByVal startY As Integer,ByVal endX As Integer,ByVal endY As Integer) As Integer
  12. Dim str1 As String
  13. Dim a2 As Integer
  14. str1 = App.Path + "\1.bmp"
  15. a2 = icePub_saveSubScreen(str1,0,0,100,60)
  16. Private Declare Function icePub_saveSubScreenJpg Lib "icePubDll.dll" (ByVal jpgFile As String, ByVal startX As Integer,ByVal startY As Integer,ByVal endX As Integer,ByVal endY As Integer) As Integer
  17. Dim str1 As String
  18. Dim a2 As Integer
  19. str1 = App.Path + "\1.jpg"
  20. a2 = icePub_saveScreenJpg(str1,0,0,100,60)
  21. download:
  22. http://dl.icese.net/dev.php?f=icePubDll.rar
复制代码
我也下载了这个dll,想放在vba中使用,发现不知怎么用。有高手知道的话,或者有更好的方法,万望告之,非常感谢。
作者: yu2n    时间: 2015-6-20 11:44

你需要注册这个DLL文件
──────────────────────────────────────────────────
如何注册dll文件:http://jingyan.baidu.com/article/08b6a591f472f814a80922fd.html
64位系统下注册32位dll文件:http://succu.blog.163.com/blog/static/193917174201210625642312/
作者: bailong360    时间: 2015-6-20 13:02

本帖最后由 bailong360 于 2015-6-20 17:32 编辑

第三方PrtScr
  1. Usage:PrtScr x y width height [drive:][path]filename
  2. 例:PrtScr 0 0 200 200 save.bmp (截取左上角200*200的区域保存为save.bmp)
  3. x y从屏幕左上角算起
复制代码
用了一些比较极端的编译参数来压缩体积,如果有bug请报告
作者: yu2n    时间: 2015-6-20 17:15

截屏保存到 c:\1.bmp ,可指定区域,vb6 / vba 通用
  1. Option Explicit
  2. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  3. ' ##
  4. Private Type PointAPI
  5.   X As Long
  6.   Y As Long
  7. End Type
  8. Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
  9. Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
  10. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
  11. Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
  12. Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
  13. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  14. Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
  15. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
  16. Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal crColor As Long) As Long
  17. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
  18. Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BitmapInfo, ByVal wUsage As Long) As Long
  19. Private Declare Function GetLastError Lib "kernel32" () As Long
  20. Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As PointAPI) As Long
  21. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  22. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
  23. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  24. Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Long, ByVal iModeNum As Long, lpDevMode As Any) As Long
  25. Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
  26. Private Const WM_RBUTTONDOWN = &H204
  27. Private Const WM_RBUTTONUP = &H205
  28. Private Const WM_LBUTTONDOWN = &H201
  29. Private Const WM_LBUTTONUP = &H202
  30. Private Const SM_CXSCREEN = 0
  31. Private Const SM_CYSCREEN = 1
  32. Private Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
  33. Private Const DIB_RGB_COLORS = 0 '  color table in RGBs
  34. Private Type RGBQUAD
  35.   rgbBlue As Byte
  36.   rgbGreen As Byte
  37.   rgbRed As Byte
  38.   rgbReserved As Byte
  39. End Type
  40. Private Type BitmapInfoHeader '40 bytes
  41.   biSize As Long
  42.   biWidth As Long
  43.   biHeight As Long
  44.   biPlanes As Integer
  45.   biBitCount As Integer
  46.   biCompression As Long
  47.   biSizeImage As Long
  48.   biXPelsPerMeter As Long
  49.   biYPelsPerMeter As Long
  50.   biClrUsed As Long
  51.   biClrImportant As Long
  52. End Type
  53. Private Type BitmapInfo
  54.   bmiHeader As BitmapInfoHeader
  55.   bmiColors As RGBQUAD
  56. End Type
  57. Private Type BitmapFileHeader
  58.   bfType As Integer
  59.   bfSize As Long
  60.   bfReserved1 As Integer
  61.   bfReserved2 As Integer
  62.   bfOffBits As Long
  63. End Type
  64. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  65. ' ##
  66. Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
  67. Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal l As Long) As Long
  68. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  69. Private Const FILE_ATTRIBUTE_NORMAL = &H80
  70. Private Const FILE_FLAG_SEQUENTIAL_SCAN = &H8000000
  71. Private Const FILE_SHARE_WRITE = &H2
  72. Private Const CREATE_ALWAYS = 2
  73. Private Const GENERIC_WRITE = &H40000000
  74. 'Download by http://www.codefans.net
  75. Private Type SECURITY_ATTRIBUTES
  76.   nLength As Long
  77.   lpSecurityDescriptor As Long
  78.   bInheritHandle As Long
  79. End Type
  80. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  81. ' ##
  82. Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
  83. Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
  84. Private Const HWND_TOPMOST = -1
  85. Private Const SWP_SHOWWINDOW = &H40
  86. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  87. Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, lpPoint As PointAPI) As Long
  88. Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
  89. Private Declare Function Rectangle Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
  90. Private Declare Function Polyline Lib "gdi32" (ByVal hdc As Long, lpPoint As PointAPI, ByVal nCount As Long) As Long
  91. Private Declare Function RoundRect Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long
  92. Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
  93. Private Declare Function Ellipse Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
  94. Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long
  95. Private Type RECT
  96.   Left As Long
  97.   Top As Long
  98.   Right As Long
  99.   Bottom As Long
  100. End Type
  101. Private Const PS_DOT = 2                     '  .......
  102. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  103. ' ########### ########### ########### ########### ########### ########### ########### ###########
  104. ' ########### ########### ########### ########### ########### ########### ########### ###########
  105. ' ########### ########### ########### ########### ########### ########### ########### ###########
  106. Private Sub GetScreenResolution(ByRef nWidth As Integer, ByRef nHeight As Integer)
  107.   Dim a(1 To 61) As Integer
  108.   EnumDisplaySettings 0, -1, a(1)
  109.   'a(53) Deep / a(55) Width / a(57) Height / a(61) Refresh frequency
  110.   nWidth = a(55)
  111.   nHeight = a(57)
  112. End Sub
  113. Sub Main()
  114.   TestSaveScreen
  115.   MsgBox "Done"
  116.   End
  117. End Sub
  118. Sub TestSaveScreen()
  119.   Dim beginPoint As PointAPI
  120.   Dim endPoint As PointAPI
  121.   Dim X As Long, Y As Long
  122.   Y = GetSystemMetrics(SM_CYSCREEN)
  123.   X = GetSystemMetrics(SM_CXSCREEN)
  124.   beginPoint.X = 0
  125.   beginPoint.Y = 0
  126.   endPoint.X = X
  127.   endPoint.Y = Y
  128.   Screen2Bmp "c:\1.bmp", beginPoint, endPoint
  129. End Sub
  130. Function Screen2Bmp(ByVal bmpFile As String, ByRef beginPoint As PointAPI, ByRef endPoint As PointAPI)
  131.   Dim X&, Y&, nSrcDC&, nSrcBmp&, nMemDC&
  132.   'Init Screen DC
  133.   nSrcDC = GetDC(0)
  134.   
  135.   'Create a bitmap object on the screen
  136.   X = GetSystemMetrics(SM_CXSCREEN)
  137.   Y = GetSystemMetrics(SM_CYSCREEN)
  138.   nSrcBmp = CreateCompatibleBitmap(nSrcDC, X, Y)
  139.   nMemDC = CreateCompatibleDC(nSrcDC)    'Create a memory DC on the screen
  140.   
  141.   'Move the screen content to the memory DC
  142.   SelectObject nMemDC, nSrcBmp
  143.   BitBlt nMemDC, 0, 0, X, Y, nSrcDC, 0, 0, SRCCOPY
  144.   DeleteObject nSrcBmp
  145.   
  146.   'Correct starting coordinates
  147.   Dim nTmpDC&, nTmpBmp&, k&, g&, tmpInt%
  148.   If endPoint.X < beginPoint.X Then
  149.     tmpInt = endPoint.X
  150.     endPoint.X = beginPoint.X
  151.     beginPoint.X = tmpInt
  152.   End If
  153.   If endPoint.Y < beginPoint.Y Then
  154.     tmpInt = endPoint.Y
  155.     endPoint.Y = beginPoint.Y
  156.     beginPoint.Y = tmpInt
  157.   End If
  158.   
  159.   k = endPoint.X - beginPoint.X
  160.   g = endPoint.Y - beginPoint.Y
  161.   
  162.   nTmpDC = CreateCompatibleDC(nSrcDC)
  163.   nTmpBmp = CreateCompatibleBitmap(nSrcDC, k, g)
  164.   SelectObject nTmpDC, nTmpBmp
  165.   
  166.   BitBlt nTmpDC, 0, 0, k, g, nMemDC, beginPoint.X, beginPoint.Y, SRCCOPY
  167.   DeleteDC nMemDC
  168.   
  169.   Dim bih As BitmapInfoHeader
  170.   With bih
  171.     .biBitCount = 32
  172.     .biClrImportant = 0
  173.     .biClrUsed = 0
  174.     .biCompression = 0
  175.     .biHeight = g
  176.     .biPlanes = 1
  177.     .biSize = 40
  178.     .biSizeImage = k * g * 4
  179.     .biWidth = k
  180.     .biXPelsPerMeter = 0
  181.     .biYPelsPerMeter = 0
  182.   End With
  183.   
  184.   Dim bfh As BitmapFileHeader
  185.   With bfh
  186.     .bfOffBits = 14 + bih.biSize
  187.     .bfReserved1 = 0
  188.     .bfReserved2 = 0
  189.     .bfSize = .bfOffBits + bih.biSizeImage
  190.     .bfType = 19778 'BM
  191.   End With
  192.   
  193.   Dim sa As SECURITY_ATTRIBUTES
  194.   With sa
  195.     .bInheritHandle = 0
  196.     .lpSecurityDescriptor = 0
  197.     .nLength = 0
  198.   End With
  199.   
  200.   If k > 0 And g > 0 Then
  201.     ReDim bits(4, 0 To k - 1, 0 To g - 1)
  202.   Else
  203.     ReDim bits(4, 0, 0)
  204.   End If
  205.   
  206.   Dim bitinfo As BitmapInfo
  207.   With bitinfo.bmiHeader
  208.     .biBitCount = 32
  209.     .biCompression = 0
  210.     .biHeight = g
  211.     .biPlanes = 1
  212.     .biSize = Len(bitinfo.bmiHeader)
  213.     .biWidth = k
  214.   End With
  215.   
  216.   GetDIBits nTmpDC, nTmpBmp, 0&, g, bits(0, 0, 0), bitinfo, DIB_RGB_COLORS
  217.   DeleteObject nTmpBmp
  218.   DeleteDC nTmpDC
  219.   
  220.   Dim fh As Long
  221.   Dim Writtennum As Long
  222.   fh = CreateFile(bmpFile, GENERIC_WRITE, 0, sa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL + FILE_FLAG_SEQUENTIAL_SCAN, 0)
  223.   Writtennum = 0
  224.   WriteFile fh, bfh.bfType, 2, Writtennum, 0
  225.   WriteFile fh, bfh.bfSize, 12, Writtennum, 0
  226.   WriteFile fh, bih, bih.biSize, Writtennum, 0
  227.   WriteFile fh, bits(0, 0, 0), bih.biSizeImage, Writtennum, 0
  228.   
  229.   'Debug.Print WriteFile(fh, bfh, Len(BITMAPFILEHEADER), 0, 0)
  230.   'Debug.Print WriteFile(fh, bih, Len(BITMAPINFOHEADER), Len(BITMAPFILEHEADER), 0)
  231.   'Debug.Print WriteFile(fh, nTmpBmp, k * g * 3, Len(BITMAPFILEHEADER) + Len(BITMAPINFOHEADER), 0)
  232.   
  233.   CloseHandle fh
  234. End Function
复制代码

作者: zhanglei1371    时间: 2015-6-20 21:59

回复 3# bailong360


    我下载prtsc。exe测试下,prtsc 0 0 500 800 c:\123546.bmp
结果发现C盘下什么也没。只是剪贴板内多个全屏的截图。难道是我哪里操作有误?
作者: zhanglei1371    时间: 2015-6-20 22:09

回复 4# yu2n


    测试了下,word直接崩溃了
C盘根目录倒是生成了 一个7.91M的bmp,不知怎么回事。此外,我按照二楼的方法,发现还是无法注册dll,不知那个dll您用过没?
附件:http://pan.baidu.com/s/1mglA42G
作者: bailong360    时间: 2015-6-20 22:12

本帖最后由 bailong360 于 2015-6-20 22:14 编辑

回复 5# zhanglei1371
是PrtScr而不是PrtSc哦
看了一下,忘记更新列表了,现在应该可以搜索到PrtScr了
作者: zhanglei1371    时间: 2015-6-20 22:18

本帖最后由 zhanglei1371 于 2015-6-20 22:23 编辑

回复 7# bailong360


是不是64位无法使用?我下载了,结果运行错误,弹出对话框:应用程序无法正常启动0xc00000018
作者: zhanglei1371    时间: 2015-6-20 22:25

回复 9# bailong360


    我用的win7 64,提示应用程序无法正常启动....
作者: bailong360    时间: 2015-6-20 22:34

本帖最后由 bailong360 于 2015-6-20 22:51 编辑

回复 9# zhanglei1371
LZ用32位的cmd启动试一下,我去试试重新编译
作者: zhanglei1371    时间: 2015-6-21 07:33

回复 10# bailong360


    发现32位的可以用,64位的提示错误。若是64位能用,将是非常优秀和完美的一个软件。
此外,我上面上传了个icePubDll.dll的下载地址,似乎有C语言源码,但是我发现一旦横向宽度坐标超过1080,就会截一直到屏幕右下角而成自己指定的右下角的区域。您若能修改下这个bug,能否顺便也修复下这个dll,编译下?这个32.64皆可用,谢谢了
作者: bailong360    时间: 2015-6-21 22:25

回复 11# zhanglei1371
已重新编译;;dll下载不了,无法访问
作者: zhanglei1371    时间: 2015-6-21 23:09

本帖最后由 zhanglei1371 于 2015-6-21 23:13 编辑

回复 12# bailong360


  多谢,64位已经可以完美使用!
icepubdll:
http://download.csdn.net/download/xiaomer/3733352
作者: yu2n    时间: 2015-6-22 08:26

回复 6# zhanglei1371

VB简单截图工具(源码)
http://www.softhy.net/soft/32142.htm

请自行修改。
作者: zhanglei1371    时间: 2015-6-22 09:21

回复 13# zhanglei1371


    提点建议:
工具64位下可行。但是存在些不完美的地方:
我最终是想通过vba代码: Selection.InlineShapes.AddPicture "C:\mc.jpg"将生成的图片插入到word文档,但是发现这样生成的图片无法插入到word文档。而且体积奇大无比
两个同样大小的截图,一个是90.3k,一个是1.5M(本工具生成),能改善下,能插入到word文档就是最好了。体积大小无所谓
作者: bailong360    时间: 2015-6-22 22:30

回复 15# zhanglei1371
保存的图片实际上是bmp格式的,lz可以下载第三方ToPng进行转换
  1. 例:ToPng C:\a.bmp jpg
复制代码

作者: yu2n    时间: 2015-6-23 08:20

回复 15# zhanglei1371

截图简单点,调用命令行截图工具 NirCmd 即可。生成 png 图片,文件不大,可指定截图范围。

NirCmd:  http://www.nirsoft.net/utils/nircmd.html
Download NirCmd:  http://www.nirsoft.net/utils/nircmd.zip

示例:
全屏截图:
  1. D:\downloads\nircmd\nircmd.exe savescreenshot "d:\1.png"
复制代码
截取指定区域:
  1. D:\downloads\nircmd\nircmd.exe savescreenshot "d:\2.png" 50 50 300 200
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2