AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Screen Capture Script - Batch grab design

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
mstorey20



Joined: 21 Sep 2009
Posts: 10

PostPosted: Wed Oct 07, 2009 12:33 pm    Post subject: Screen Capture Script - Batch grab design Reply with quote

Certainly not the first, but I couldn't find one out of the box that did exactly what I wanted so I tweaked some things from one from the board. I think it is unique based on how it runs for multiple quick grabs. Maybe it will help someone here.

Designed to capture screen shots in batches (that is how I do them in life for work). Keystroke is Win-PrintScreen.
Default location is C:\Images - configuration options are labeled in head of script.

Process: Input box appears for label - think folder/file name. Files are then placed in a folder within the default path and named folder0, folder1, folder2 etc until label is changed. So Input: Website and you get C:\Images\Website\Website0... Website1... Website2... captured and organized. It checks for duplicate folder creation so you can't easily overwrite work.

(based on script from the board - can't find exactly where I ripped this from for credit for the lower half of script - my bad there - it is an add-on to a compilation script - if you wrote the hard part - thanks for posting and helping out in advance)

Code:


;
; AutoHotkey Version: 1.x
; Language:       English
; Platform:       Win9x/NT
; Author:         A.N.Other <myemail@nowhere.com>
;
; Script Function:
;   Template script (you can customize this template by editing "ShellNew\Template.ahk" in your Windows folder)
;

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

;*************************************************************************************
; CONFIGURATION OPTIONS
;*************************************************************************************

IMAGECAPTUREFOLDER=c:\Images               ; Default Path
IMAGEPROMPT=Please Input Image Label:            ; Default label prompt

SS_FileType=jpg                     ; Options jpg,png,gif,tif,bmp
SS_ScreenSize=Full                  ; Full, Active, Contents


;****************************************
;Initialize a few Variables each run
;****************************************
SS_FileName=
n=0

;****************************************
;Create a standard path for images
;****************************************
FileCreateDir, %IMAGECAPTUREFOLDER%
if (ErrorLevel)
{
   MsgBox Error Building Default Directory`nPlease verify IMAGECAPTUREFOLDER path in the configuration section.
   return
}
SetWorkingDir, %IMAGECAPTUREFOLDER%
FileCreateDir, %IMAGECAPTUREFOLDER%\Test_Folder
n=0

#Persistent
#SingleInstance Force

;--------------------------------Tray Menu------------------------------------
;Menu, Tray, NoStandard
Menu, Tray, add,
Menu, Tray, add, Image Capture, ScreenCap
Return

;--------------------------Modified ScreenCapture-----------------------------
#PrintScreen::
ScreenCap:
Gui 1:Submit, nohide

Changed = %SS_FileName%
Prompt =
(
Sample Path: %IMAGECAPTUREFOLDER%\Input\Input0


%IMAGEPROMPT%
)

InputBox, SS_FileName, StoreyQuickNotes - Image Capture,%Prompt%,,,175,,,,,%SS_FileName%

if ErrorLevel
   return

if (!SS_FileName)
{
   MsgBox Error: Invalid Patient Name.  Must Contain Valid Characters
   return
}
if (Changed=SS_FileName)
{
   n:=n+1   ;simple increment
}
else
{
   n=0
   FileCopyDir, %IMAGECAPTUREFOLDER%\Test_Folder, %IMAGECAPTUREFOLDER%\%SS_FileName%
   if (!ErrorLevel)
   {
      NewFolder=1
      SS_Folder = %IMAGECAPTUREFOLDER%\%SS_FileName%
   }
   else
   {
      MsgBox Error: Duplicate Folder Found.  Added Extension: _X
      FileCreateDir %IMAGECAPTUREFOLDER%\%SS_FileName%_X
      SS_Folder = %IMAGECAPTUREFOLDER%\%SS_FileName%_X
   }
}

if (SS_FileType="jpg" or SS_FileType="png" or SS_FileType="gif" or SS_FileType="tif" or SS_FileType="bmp")
   SS_FileType = .%SS_FileType%
else
   SS_FileType=.jpg

if (SS_ScreenSize = "Full")
   aRect = 0

Else If (SS_ScreenSize = "Active")
   aRect = 1

Else If (SS_ScreenSize = "Contents")
   aRect = 2

Else
   aRect = 0

Sleep 500        ; Needed delay for some Citrix client screen captures

bCursor = False
sFile = %SS_Folder%\%SS_FileName%%n%%SS_FileType%

; -----------------------Prior code grabbed from AutoHokey Board - Credit ???? - not me and can't find again --------------

{
   If   !aRect
   {
      SysGet, Mon, Monitor, 1
      nL := MonLeft
      nT := MonTop
      nW := MonRight - MonLeft
      nH := MonBottom - MonTop
   }
   Else If   aRect = 1
      WinGetPos, nL, nT, nW, nH, A
   Else If   aRect = 2
   {
      WinGet, hWnd, ID, A
      VarSetCapacity(rt, 16, 0)
      DllCall("GetClientRect" , "Uint", hWnd, "Uint", &rt)
      DllCall("ClientToScreen", "Uint", hWnd, "Uint", &rt)
      nL := NumGet(rt, 0, "int")
      nT := NumGet(rt, 4, "int")
      nW := NumGet(rt, 8)
      nH := NumGet(rt,12)
   }
   Else
   {
      StringSplit, rt, aRect, `,, %A_Space%%A_Tab%
      nL := rt1
      nT := rt2
      nW := rt3 - rt1
      nH := rt4 - rt2
      znW := rt5
      znH := rt6
   }

   mDC := DllCall("CreateCompatibleDC", "Uint", 0)
   hBM := CreateDIBSection(mDC, nW, nH)
   oBM := DllCall("SelectObject", "Uint", mDC, "Uint", hBM)
   hDC := DllCall("GetDC", "Uint", 0)
   DllCall("BitBlt", "Uint", mDC, "int", 0, "int", 0, "int", nW, "int", nH, "Uint", hDC, "int", nL, "int", nT, "Uint", 0x40000000 |

0x00CC0020)
   DllCall("ReleaseDC", "Uint", 0, "Uint", hDC)
   If   bCursor
      CaptureCursor(mDC, nL, nT)
   DllCall("SelectObject", "Uint", mDC, "Uint", oBM)
   DllCall("DeleteDC", "Uint", mDC)
   If   znW && znH
      hBM := Zoomer(hBM, nW, nH, znW, znH)
   If   sFile = 0
      SetClipboardData(hBM)
   Else   Convert(hBM, sFile), DllCall("DeleteObject", "Uint", hBM)
}

CaptureCursor(hDC, nL, nT)
{
   VarSetCapacity(mi, 20, 0)
   mi := Chr(20)
   DllCall("GetCursorInfo", "Uint", &mi)
   bShow   := NumGet(mi, 4)
   hCursor := NumGet(mi, 8)
   xCursor := NumGet(mi,12)
   yCursor := NumGet(mi,16)

   VarSetCapacity(ni, 20, 0)
   DllCall("GetIconInfo", "Uint", hCursor, "Uint", &ni)
   xHotspot := NumGet(ni, 4)
   yHotspot := NumGet(ni, 8)
   hBMMask  := NumGet(ni,12)
   hBMColor := NumGet(ni,16)

   If   bShow
      DllCall("DrawIcon", "Uint", hDC, "int", xCursor - xHotspot - nL, "int", yCursor - yHotspot - nT, "Uint", hCursor)
   If   hBMMask
      DllCall("DeleteObject", "Uint", hBMMask)
   If   hBMColor
      DllCall("DeleteObject", "Uint", hBMColor)
}

Zoomer(hBM, nW, nH, znW, znH)
{
   mDC1 := DllCall("CreateCompatibleDC", "Uint", 0)
   mDC2 := DllCall("CreateCompatibleDC", "Uint", 0)
   zhBM := CreateDIBSection(mDC2, znW, znH)
   oBM1 := DllCall("SelectObject", "Uint", mDC1, "Uint",  hBM)
   oBM2 := DllCall("SelectObject", "Uint", mDC2, "Uint", zhBM)
   DllCall("SetStretchBltMode", "Uint", mDC2, "int", 4)
   DllCall("StretchBlt", "Uint", mDC2, "int", 0, "int", 0, "int", znW, "int", znH, "Uint", mDC1, "int", 0, "int", 0, "int", nW, "int", nH,

"Uint", 0x00CC0020)
   DllCall("SelectObject", "Uint", mDC1, "Uint", oBM1)
   DllCall("SelectObject", "Uint", mDC2, "Uint", oBM2)
   DllCall("DeleteDC", "Uint", mDC1)
   DllCall("DeleteDC", "Uint", mDC2)
   DllCall("DeleteObject", "Uint", hBM)
   Return   zhBM
}

Convert(sFileFr = "", sFileTo = "")
{
   If   sFileTo  =
      sFileTo := A_ScriptDir . "\screen.bmp"
   SplitPath, sFileTo, , sDirTo, sExtTo, sNameTo

   If Not   hGdiPlus := DllCall("LoadLibrary", "str", "gdiplus.dll")
      Return   sFileFr+0 ? SaveHBITMAPToFile(sFileFr, sDirTo . "\" . sNameTo . ".bmp") : ""
   VarSetCapacity(si, 16, 0), si := Chr(1)
   DllCall("gdiplus\GdiplusStartup", "UintP", pToken, "Uint", &si, "Uint", 0)

   If   !sFileFr
   {
      DllCall("OpenClipboard", "Uint", 0)
      If    DllCall("IsClipboardFormatAvailable", "Uint", 2) && (hBM:=DllCall("GetClipboardData", "Uint", 2))
      DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", "Uint", hBM, "Uint", 0, "UintP", pImage)
      DllCall("CloseClipboard")
   }
   Else If   sFileFr Is Integer
      DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", "Uint", sFileFr, "Uint", 0, "UintP", pImage)
   Else   DllCall("gdiplus\GdipLoadImageFromFile", "Uint", Unicode4Ansi(wFileFr,sFileFr), "UintP", pImage)

   DllCall("gdiplus\GdipGetImageEncodersSize", "UintP", nCount, "UintP", nSize)
   VarSetCapacity(ci, nSize)
   DllCall("gdiplus\GdipGetImageEncoders", "Uint", nCount, "Uint", nSize, "Uint", &ci)
   Loop,   %nCount%
   {
      If   !InStr(Ansi4Unicode(NumGet(ci, 76 * (A_Index - 1) + 44)), "." . sExtTo)
         Continue
      pCodec := &ci + 76 * (A_Index - 1)
         Break
   }

   If   pImage
      pCodec   ? DllCall("gdiplus\GdipSaveImageToFile", "Uint", pImage, "Uint", Unicode4Ansi(wFileTo,sFileTo), "Uint", pCodec, "Uint", 0) :

DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", "Uint", pImage, "UintP", hBitmap, "Uint", 0) . SetClipboardData(hBitmap), DllCall

("gdiplus\GdipDisposeImage", "Uint", pImage)

   DllCall("gdiplus\GdiplusShutdown" , "Uint", pToken)
   DllCall("FreeLibrary", "Uint", hGdiPlus)
}

CreateDIBSection(hDC, nW, nH, bpp = 32, ByRef pBits = "")
{
   NumPut(VarSetCapacity(bi, 40, 0), bi)
   NumPut(nW, bi, 4)
   NumPut(nH, bi, 8)
   NumPut(bpp, NumPut(1, bi, 12, "UShort"), 0, "Ushort")
   NumPut(0,  bi,16)
   Return   DllCall("gdi32\CreateDIBSection", "Uint", hDC, "Uint", &bi, "Uint", 0, "UintP", pBits, "Uint", 0, "Uint", 0)
}

SaveHBITMAPToFile(hBitmap, sFile)
{
   DllCall("GetObject", "Uint", hBitmap, "int", VarSetCapacity(oi,84,0), "Uint", &oi)
   hFile:=   DllCall("CreateFile", "Uint", &sFile, "Uint", 0x40000000, "Uint", 0, "Uint", 0, "Uint", 2, "Uint", 0, "Uint", 0)
   DllCall("WriteFile", "Uint", hFile, "int64P", 0x4D42|14+40+NumGet(oi,44)<<16, "Uint", 6, "UintP", 0, "Uint", 0)
   DllCall("WriteFile", "Uint", hFile, "int64P", 54<<32, "Uint", 8, "UintP", 0, "Uint", 0)
   DllCall("WriteFile", "Uint", hFile, "Uint", &oi+24, "Uint", 40, "UintP", 0, "Uint", 0)
   DllCall("WriteFile", "Uint", hFile, "Uint", NumGet(oi,20), "Uint", NumGet(oi,44), "UintP", 0, "Uint", 0)
   DllCall("CloseHandle", "Uint", hFile)
}

SetClipboardData(hBitmap)
{
   DllCall("GetObject", "Uint", hBitmap, "int", VarSetCapacity(oi,84,0), "Uint", &oi)
   hDIB :=   DllCall("GlobalAlloc", "Uint", 2, "Uint", 40+NumGet(oi,44))
   pDIB :=   DllCall("GlobalLock", "Uint", hDIB)
   DllCall("RtlMoveMemory", "Uint", pDIB, "Uint", &oi+24, "Uint", 40)
   DllCall("RtlMoveMemory", "Uint", pDIB+40, "Uint", NumGet(oi,20), "Uint", NumGet(oi,44))
   DllCall("GlobalUnlock", "Uint", hDIB)
   DllCall("DeleteObject", "Uint", hBitmap)
   DllCall("OpenClipboard", "Uint", 0)
   DllCall("EmptyClipboard")
   DllCall("SetClipboardData", "Uint", 8, "Uint", hDIB)
   DllCall("CloseClipboard")
}

Unicode4Ansi(ByRef wString, sString)
{
   nSize := DllCall("MultiByteToWideChar", "Uint", 0, "Uint", 0, "Uint", &sString, "int", -1, "Uint", 0, "int", 0)
   VarSetCapacity(wString, nSize * 2)
   DllCall("MultiByteToWideChar", "Uint", 0, "Uint", 0, "Uint", &sString, "int", -1, "Uint", &wString, "int", nSize)
   Return   &wString
}

Ansi4Unicode(pString)
{
   nSize := DllCall("WideCharToMultiByte", "Uint", 0, "Uint", 0, "Uint", pString, "int", -1, "Uint", 0, "int",  0, "Uint", 0, "Uint", 0)
   VarSetCapacity(sString, nSize)
   DllCall("WideCharToMultiByte", "Uint", 0, "Uint", 0, "Uint", pString, "int", -1, "str", sString, "int", nSize, "Uint", 0, "Uint", 0)
   Return   sString
}


;-----------------------Give Simple Feedback Msg--------------------------
if (NewFolder)
{
Display =
(
New Folder Created

ImageCaptured: %sFile%
)
NewFolder=0
}
else
   Display = Image Captured: %sFile%

MsgBox 0,SQN - Image Capture, %Display%, 1.5
return


_________________
StoreyQuickNotes - An AutoHotkey Project for Radiation Oncology
Back to top
View user's profile Send private message
visualthing



Joined: 17 Mar 2008
Posts: 1
Location: USA

PostPosted: Mon Nov 16, 2009 7:21 pm    Post subject: Re: Screen Capture Script - Batch grab design Reply with quote

I'm a beginner to put it mildly, so I may be missing something, but...
I believe the following code forces all grabs after the first to be jpg format.

mstorey20 wrote:

Code:

if (SS_FileType="jpg" or SS_FileType="png" or SS_FileType="gif" or SS_FileType="tif" or SS_FileType="bmp")
   SS_FileType = .%SS_FileType%
else
   SS_FileType=.jpg


Behavior: I set SS_FileType to "png" at the top of the script and reload the script. The first grab file is png format; no problem. The second grab defaults back to jpg format (because ".png" != "png").

Anyway, thanks very much for posting this script. I've been trying to understand how to do screen grabs in AHK and this is a big help.

Cheers!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group