Simple Snapshot Tool (2)
;==========================================================================================================================================
; Simple Snapshot Tool, Version 2.
;
; Using COM object "ImageMagickObject.MagickImage.1" for image conversion.
;
; (c) Detlev Dalitz.20091221.20091225.
;==========================================================================================================================================

#DefineFunction udfSnapshot (strFileBMP)
blnPixelWrapExists = @TRUE ; Set this to @TRUE, if created bmp file has pixels wrapped.
Snapshot (0) ; Takes a bitmap snapshot of the screen and pastes it to the clipboard.
intDataSize = BinaryClipGet (0, 8)
hdlBB = BinaryAlloc (intDataSize) ; Allocates a data buffer.
BinaryClipGet (hdlBB, 8) ; Read file format type CF_DIB.
intBmpDataSize = 14
intBmpSize = intDataSize + intBmpDataSize ; Need to add first 14 bytes to make it a BMP file format.
hdlBB2 = BinaryAlloc (intBmpSize)
BinaryPokeStr (hdlBB2, 0, "BM") ; The characters identifying the bitmap 'BM'.
BinaryPoke4 (hdlBB2, 2, intBmpSize)
intTableLoc = BinaryPeek4 (hdlBB, 0) + intBmpDataSize + (blnPixelWrapExists * 12)
BinaryPoke4 (hdlBB2, 10, intTableLoc)
BinaryCopy (hdlBB2, intBmpDataSize, hdlBB, 0, intDataSize)
BinaryWrite (hdlBB2, strFileBMP)
hdlBB2 = BinaryFree (hdlBB2)
hdlBB = BinaryFree (hdlBB)
Return FileExist (strFileBMP)
#EndFunction

#DefineFunction udfSaveSnapshot (strFileName, strFileExt, intFileCounterLen, intNum, objIM)
strFileBMP = strFileName : ".bmp"
If udfSnapshot (strFileBMP) != 1 Then Return intNum
intNum = intNum + 1
strFileSnapshot = strFileName : "." : StrFixLeft (intNum, "0", intFileCounterLen) : "." : strFileExt
strResult = objIM.Convert("", strFileBMP, strFileSnapshot) ; Note: The empty string as the first parameter makes it working!
intResult = FileDelete (strFileBMP)
Return intNum
#EndFunction


; Main.

; Display instructions.
strMsgTitle = "Simple Snapshot Tool"
strMsgText = "Instructions:" : @LF : @LF : "Press [Ctrl] to shot." : @LF : "Press [Shift] to exit."
Pause (strMsgTitle, strMsgText)

; Get home folder.
strFolderHome = DirGet ()

; Get temp folder
strFolderTemp = ShortCutDir ("Local Settings", 0, 0) : "\Temp\" ; User temp folder.

; Set work folder.
strFolderWork = strFolderTemp : "Snapshots\"
Terminate (!DirMake (strFolderWork), "Terminated.", "Cannot create folder: " : @LF : strFolderWork)
Terminate (!DirChange (strFolderWork), "Terminated.", "Cannot access folder: " : @LF : strFolderWork)

; Set file type and mask.
strFileExt = "jpg" ; jpg, png, gif, tiff, pdf, ... read more: http://www.imagemagick.org/script/formats.php
strFileName = "Snapshot"
intFileCounterLen = 5
strFileMask = strFileName : "." : StrFill ("?", intFileCounterLen) : "." : strFileExt

; Get last number.
intNum = 0
strListFiles = FileItemize (strFileMask)
If strListFiles != ""
   strListFiles = ItemSort (strListFiles, @TAB)
   strNum = ItemExtract (-1, strListFiles, @TAB)
   strNum = ItemRemove (-1, strNum, ".")
   strNum = ItemExtract (-1, strNum, ".")
   intNum = Int (strNum)
EndIf

; Invoke IM COM object.
objIM = ObjectCreate ("ImageMagickObject.MagickImage.1")

; Loop.
WinTitle ("", intNum : " snapshots")
While @TRUE
   If IsKeyDown (@CTRL)
      intNum = udfSaveSnapshot (strFileName, strFileExt, intFileCounterLen, intNum, objIM)
      WinTitle ("", intNum : " snapshots")
   EndIf
   If IsKeyDown (@SHIFT) Then Break
   TimeDelay (.1)
EndWhile

; Release IM COM object.
Drop (objIM)

; Back to home folder.
Terminate (!DirChange (strFolderHome), "Terminated.", "Cannot access folder: " : @LF : strFolderHome)

; Say Goodbye.
strMsgText = "Goodbye."
Display (1, strMsgTitle, strMsgText)

; Display snapshots.
; Try to start IrfanView application otherwise Explorer.
strIrfanViewUninstall = RegQueryValue (@REGMACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\IrfanView[UninstallString]")
strIrfanViewExe = FilePath (strIrfanViewUninstall) : "i_view32.exe" ; e. g. "C:\Program Files\IrfanView\i_view32.exe"
If FileExist (strIrfanViewExe) == 1
   ShellExecute (strIrfanViewExe, '"' : strFolderWork : '"' : " /thumbs", strFolderWork, @ZOOMED, "")
Else
   ShellExecute ("explorer.exe", "/e," : '"' : strFolderWork : '"', strFolderWork, @ZOOMED, "")
EndIf

:CANCEL
Exit