03 July, 2014

AutoHotkey Scripts: Take quick screenshots (Snipping Tool) using "Gdip.ahk" library


 I often need to compare some things;  taking a quick screenshot is very useful for this purpose. Windows "Snipping Tool" is OK, but it doesn't run in multiple instances and it's somewhat bothersome to use.
I needed something quick.

Download "Take Screenshot"


• include Gdip.ahk (download) library from http://www.autohotkey.com/board/topic/29449-gdi-standard-library-145-by-tic/
 • To capture a screenshot drag mouse while holding right shift key
• Each screenshot opens its own window from which it can be saved
Windows + h , ctrl + h  take a screenshot from a window (enter the window name in the input box)
• Windows + g , ctrl + g take a screenshot
 double click tray icon to exit

▐▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▌

/*
• includes functions from Gdip.ahk library from http://www.autohotkey.com/board/topic/29449-gdi-standard-library-145-by-tic/
• To capture a screenshot drag mouse while holding right shift key
• Each screenshot opens its own window from which it can be saved
• Windows + g , ctrl + g - take a screenshot
• ctrl + h ; Windows + H - take a screenshot from a window (enter window name in the input box)
#g:: Gosub, full_screen
^g:: Gosub, full_screen
#h:: Gosub, FromWindow
^h:: Gosub, FromWindow
*/

#SingleInstance force
SoundPlay *48
Menu, Tray, Icon,Shell32.dll,241
Menu, Tray, Add , E&dit Scite, Edit_Scite
Menu, Tray, Add , E&dit Notepad, Edit_Notepad

Menu, Tray, Add, Exit Screen Capture, Exit_Gui ; double click tray icon to exit
Menu, Tray, Default, Exit Screen Capture


Work_Dir := "C:\Temp\"
;Work_Dir = %temp%
SetWorkingDir, %Work_Dir%
IfNotExist Screenshots
FileCreateDir, Screenshots

folder_path := A_WorkingDir . "\Screenshots\"
index :=2




OnMessage(0x14, "WM_ERASEBKGND")
Gui, -Caption +ToolWindow
Gui, +LastFound
WinSet, TransColor, Black
; Create the pen here so we don't need to create/delete it every time.
RedPen := DllCall("CreatePen", "int", PS_SOLID:=0, "int", 5, "uint", 0xff)
return






RShift & LButton::
Shift & LButton::
CoordMode, Mouse, Screen
MouseGetPos, begin_x, begin_y
MouseGetPos, xorigin, yorigin
SetTimer, rectangle, 10
KeyWait, LButton
SetTimer, rectangle, Off
Gui, Cancel
MouseGetPos, end_x, end_y

if (end_x > begin_x AND end_y > begin_y){
;file%index% := folder_path "screenshot x" begin_x ",y" begin_y " width" Abs(end_x-begin_x) ", height" Abs(end_y-begin_y) ".png"
;screen := begin_x . "|" . begin_y . "|" . Abs(end_x-begin_x) . "|" Abs(end_y-begin_y) ; X|Y|W|H
Capture_x:=begin_x
Capture_y:=begin_y
}else if (end_x < begin_x AND end_y < begin_y){
Capture_x:=end_x
Capture_y:=end_y
}else if (end_x < begin_x AND end_y > begin_y){
Capture_x:=end_x
Capture_y:=begin_y
}else if (end_x > begin_x AND end_y < begin_y){
Capture_x:=begin_x
Capture_y:=end_y
}


Capture_width:=Abs(end_x-begin_x)
Capture_height:= Abs(end_y-begin_y)


screen := Capture_x . "|" . Capture_y . "|" . Capture_width . "|" Capture_height ; X|Y|W|H

;file%index% := folder_path "screenshot x" Capture_x ",y" Capture_y " width" Capture_width ", height" Capture_height ".png"
;file%index% := folder_path "screenshot " A_Now_Format(A_Now) ", w" Capture_width " h" Capture_height ".png"
file%index% := folder_path "screenshot " A_Now_Format(A_Now) ".png"

x:=file%index%
Screenshot(x,screen)
sleep 300
Gosub, makeGUI
return



rectangle:
CoordMode, Mouse, Screen
MouseGetPos, x2, y2

; Has the mouse moved?
if (x1 y1) = (x2 y2)
return

; Allow dragging to the left of the click point.
if (x2 < xorigin) {
x1 := x2
x2 := xorigin
} else
x1 := xorigin

; Allow dragging above the click point.
if (y2 < yorigin) {
y1 := y2
y2 := yorigin
} else
y1 := yorigin

Gui, Show, % "NA X" x1 " Y" y1 " W" x2-x1 " H" y2-y1
Gui, +LastFound
DllCall("RedrawWindow", "uint", WinExist(), "uint", 0, "uint", 0, "uint", 5)
return

full_screen:
file%index% := folder_path "screenshot " A_Now_Format(A_Now) ".png"
x:=file%index%
screen := 0 . "|" . 0 . "|" . A_ScreenWidth . "|" A_ScreenHeight ; X|Y|W|H
Screenshot(x,screen)
sleep 300
Gosub, makeGUI
return

makeGUI:
;Gui,%index% : +LastFound +Resize +Minsize400x330 ; +AlwaysOnTop
Gui,%index%: Color, 6B8590
Gui,%index%: Margin,0,0
Gui,%index%: Add, Picture, x10 y4 w16 h16 gSave Icon259 AltSubmit, shell32.dll ;
Gui,%index%: Add, Picture, x40 y4 w16 h16 gSave_2 Icon24 AltSubmit, imageres.dll ;

file_GUI:= file%index%
;MsgBox, , , % "makeGUI: " index " - " file1
Gui,%index%: Add, Picture, x0 y24 , %file_GUI%
Gui,%index%: Show,, Screenshots - Drozd
index+=1
return



Save:
;File_num:= "C:\a\My File" A_Gui ".png"
File_num:= file%A_Gui%
;FileSelectFile, FileName , S, %File_num%.png , Save As, Image Files (*.png) ;C:\a\My File.png
FileSelectFile, FileName , S, %File_num% , Save As, Image Files (*.png)
file:=file%A_Gui%

;FileCopy, %file%, %FileName%
FileCopy, %file%, %FileName%.png ;Feb5 2018
return

Save_2:
File_num:= file%A_Gui%
;FileSelectFile, FileName , S, %File_num%.png , Save As, Image Files (*.png)
FileSelectFile, FileName , S, %File_num% , Save As, Image Files (*.png)
file:=file%A_Gui%

;======================== compress with pngquanti
CompressPNG := "C:\Program Files\Misc\PNGquant\libs\pngquanti\pngquanti.exe" ; path to pngquant
RunWait , %CompressPNG% "%file%"

RegExMatch(file,"^(.*)\.png$", f)
file:= f1 . "-fs8.png"

;MsgBox, , , % file "`n" FileName
;FileCopy, %file%, %FileName%
FileCopy, %file%, %FileName%.png
;========================
return

#g:: Gosub, full_screen

#h:: Gosub, FromWindow

^g:: Gosub, full_screen

^h:: Gosub, FromWindow

FromWindow:
InputBox, window , Window Title, Enter Window Title, , 300, 100,,,,,ahk_class OperaWindowClass

dat:= A_Now_Format(A_Now)
;file := folder_path "screenshot ”" window "” " A_Now_Format(A_Now) ".png"
file%index%:= folder_path "screenshot ”" window "” " A_Now_Format(A_Now) ".png"
ID:= WinExist(window)
y:=file%index%
ClipFromWindow(y,ID)
Gosub, makeGUI
index+=1
return


ClipFromWindow(outfile, ID) {
pToken := Gdip_Startup()
pBitmap:=Gdip_BitmapFromHWND(ID)
Gdip_SaveBitmapToFile(pBitmap, outfile, 80)


Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
}


Screenshot(outfile, screen) {
pToken := Gdip_Startup()
raster := 0x40000000 + 0x00CC0020

pBitmap := Gdip_BitmapFromScreen(screen,raster)

Gdip_SaveBitmapToFile(pBitmap, outfile, 80)
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
}



A_Now_Format(raw){
RegExMatch(raw,"^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})", d)
date:=d1 "-" d2 "-" d3 " " d4 "-" d5 "-" d6
return date
}

WM_ERASEBKGND(wParam, lParam)
{
global x1, y1, x2, y2, RedPen
Critical 50
if A_Gui = 1
{
; Retrieve stock brush.
blackBrush := DllCall("GetStockObject", "int", BLACK_BRUSH:=0x4)
; Select pen and brush.
oldPen := DllCall("SelectObject", "uint", wParam, "uint", RedPen)
oldBrush := DllCall("SelectObject", "uint", wParam, "uint", blackBrush)
; Draw rectangle.
DllCall("Rectangle", "uint", wParam, "int", 0, "int", 0, "int", x2-x1, "int", y2-y1)
; Reselect original pen and brush (recommended by MS).
DllCall("SelectObject", "uint", wParam, "uint", oldPen)
DllCall("SelectObject", "uint", wParam, "uint", oldBrush)
return 1
}
}


;=======
OnExit:
Exit_Gui:
MsgBox,,, ` Exit ,1
FileDelete, %folder_path%*.png
exitapp
return

;=======

;Esc:: Gosub, Exit


Edit_Notepad:
Run, "C:\Program Files\Misc\Notepad2\Notepad2.exe" "%A_ScriptFullPath%"
return

Edit_Scite:
Run, "C:\Program Files\AutoHotkey\SciTE\SciTE.exe" "%A_ScriptFullPath%"
return

Exit:
FileDelete, %folder_path%*.png
;FileRecycle, %folder_path%*.png
;FileRemoveDir,%folder_path%, 1
ExitApp
return


;#Include Gdip_All.ahk






Gdip_Startup()
{
Ptr := A_PtrSize ? "UPtr" : "UInt"
if !DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
DllCall("LoadLibrary", "str", "gdiplus")
VarSetCapacity(si, A_PtrSize = 8 ? 24 : 16, 0), si := Chr(1)
DllCall("gdiplus\GdiplusStartup", A_PtrSize ? "UPtr*" : "uint*", pToken, Ptr, &si, Ptr, 0)
return pToken
}

Gdip_Shutdown(pToken)
{
Ptr := A_PtrSize ? "UPtr" : "UInt"
DllCall("gdiplus\GdiplusShutdown", Ptr, pToken)
if hModule := DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
DllCall("FreeLibrary", Ptr, hModule)
return 0
}


Gdip_BitmapFromHWND(hwnd)
{
WinGetPos,,, Width, Height, ahk_id %hwnd%
hbm := CreateDIBSection(Width, Height), hdc := CreateCompatibleDC(), obm := SelectObject(hdc, hbm)
PrintWindow(hwnd, hdc)
pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
SelectObject(hdc, obm), DeleteObject(hbm), DeleteDC(hdc)
return pBitmap
}



Gdip_SaveBitmapToFile(pBitmap, sOutput, Quality=75)
{
Ptr := A_PtrSize ? "UPtr" : "UInt"

SplitPath, sOutput,,, Extension
if Extension not in BMP,DIB,RLE,JPG,JPEG,JPE,JFIF,GIF,TIF,TIFF,PNG
return -1
Extension := "." Extension

DllCall("gdiplus\GdipGetImageEncodersSize", "uint*", nCount, "uint*", nSize)
VarSetCapacity(ci, nSize)
DllCall("gdiplus\GdipGetImageEncoders", "uint", nCount, "uint", nSize, Ptr, &ci)
if !(nCount && nSize)
return -2

If (A_IsUnicode){
StrGet_Name := "StrGet"
Loop, %nCount%
{
sString := %StrGet_Name%(NumGet(ci, (idx := (48+7*A_PtrSize)*(A_Index-1))+32+3*A_PtrSize), "UTF-16")
if !InStr(sString, "*" Extension)
continue

pCodec := &ci+idx
break
}
} else {
Loop, %nCount%
{
Location := NumGet(ci, 76*(A_Index-1)+44)
nSize := DllCall("WideCharToMultiByte", "uint", 0, "uint", 0, "uint", Location, "int", -1, "uint", 0, "int", 0, "uint", 0, "uint", 0)
VarSetCapacity(sString, nSize)
DllCall("WideCharToMultiByte", "uint", 0, "uint", 0, "uint", Location, "int", -1, "str", sString, "int", nSize, "uint", 0, "uint", 0)
if !InStr(sString, "*" Extension)
continue

pCodec := &ci+76*(A_Index-1)
break
}
}

if !pCodec
return -3

if (Quality != 75)
{
Quality := (Quality < 0) ? 0 : (Quality > 100) ? 100 : Quality
if Extension in .JPG,.JPEG,.JPE,.JFIF
{
DllCall("gdiplus\GdipGetEncoderParameterListSize", Ptr, pBitmap, Ptr, pCodec, "uint*", nSize)
VarSetCapacity(EncoderParameters, nSize, 0)
DllCall("gdiplus\GdipGetEncoderParameterList", Ptr, pBitmap, Ptr, pCodec, "uint", nSize, Ptr, &EncoderParameters)
Loop, % NumGet(EncoderParameters, "UInt") ;%
{
elem := (24+(A_PtrSize ? A_PtrSize : 4))*(A_Index-1) + 4 + (pad := A_PtrSize = 8 ? 4 : 0)
if (NumGet(EncoderParameters, elem+16, "UInt") = 1) && (NumGet(EncoderParameters, elem+20, "UInt") = 6)
{
p := elem+&EncoderParameters-pad-4
NumPut(Quality, NumGet(NumPut(4, NumPut(1, p+0)+20, "UInt")), "UInt")
break
}
}
}
}

if (!A_IsUnicode)
{
nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sOutput, "int", -1, Ptr, 0, "int", 0)
VarSetCapacity(wOutput, nSize*2)
DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sOutput, "int", -1, Ptr, &wOutput, "int", nSize)
VarSetCapacity(wOutput, -1)
if !VarSetCapacity(wOutput)
return -4
E := DllCall("gdiplus\GdipSaveImageToFile", Ptr, pBitmap, Ptr, &wOutput, Ptr, pCodec, "uint", p ? p : 0)
}
else
E := DllCall("gdiplus\GdipSaveImageToFile", Ptr, pBitmap, Ptr, &sOutput, Ptr, pCodec, "uint", p ? p : 0)
return E ? -5 : 0
}

Gdip_DisposeImage(pBitmap)
{
return DllCall("gdiplus\GdipDisposeImage", "uint", pBitmap)
}


Gdip_BitmapFromScreen(Screen=0, Raster="")
{
if (Screen = 0)
{
Sysget, x, 76
Sysget, y, 77
Sysget, w, 78
Sysget, h, 79
}
else if (SubStr(Screen, 1, 5) = "hwnd:")
{
Screen := SubStr(Screen, 6)
if !WinExist( "ahk_id " Screen)
return -2
WinGetPos,,, w, h, ahk_id %Screen%
x := y := 0
hhdc := GetDCEx(Screen, 3)
}
else if (Screen&1 != "")
{
Sysget, M, Monitor, %Screen%
x := MLeft, y := MTop, w := MRight-MLeft, h := MBottom-MTop
}
else
{
StringSplit, S, Screen, |
x := S1, y := S2, w := S3, h := S4
}

if (x = "") || (y = "") || (w = "") || (h = "")
return -1

chdc := CreateCompatibleDC(), hbm := CreateDIBSection(w, h, chdc), obm := SelectObject(chdc, hbm), hhdc := hhdc ? hhdc : GetDC()
BitBlt(chdc, 0, 0, w, h, hhdc, x, y, Raster)
ReleaseDC(hhdc)

pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
SelectObject(chdc, obm), DeleteObject(hbm), DeleteDC(hhdc), DeleteDC(chdc)
return pBitmap
}


CreateDIBSection(w, h, hdc="", bpp=32, ByRef ppvBits=0)
{
Ptr := A_PtrSize ? "UPtr" : "UInt"

hdc2 := hdc ? hdc : GetDC()
VarSetCapacity(bi, 40, 0)

NumPut(w, bi, 4, "uint")
, NumPut(h, bi, 8, "uint")
, NumPut(40, bi, 0, "uint")
, NumPut(1, bi, 12, "ushort")
, NumPut(0, bi, 16, "uInt")
, NumPut(bpp, bi, 14, "ushort")

hbm := DllCall("CreateDIBSection"
, Ptr, hdc2
, Ptr, &bi
, "uint", 0
, A_PtrSize ? "UPtr*" : "uint*", ppvBits
, Ptr, 0
, "uint", 0, Ptr)

if !hdc
ReleaseDC(hdc2)
return hbm
}

CreateCompatibleDC(hdc=0)
{
return DllCall("CreateCompatibleDC", "uint", hdc)
}

SelectObject(hdc, hgdiobj)
{
return DllCall("SelectObject", "uint", hdc, "uint", hgdiobj)
}

PrintWindow(hwnd, hdc, Flags=0)
{
return DllCall("PrintWindow", "uint", hwnd, "uint", hdc, "uint", Flags)
}

Gdip_CreateBitmapFromHBITMAP(hBitmap, Palette=0)
{
Ptr := A_PtrSize ? "UPtr" : "UInt"

DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", Ptr, hBitmap, Ptr, Palette, A_PtrSize ? "UPtr*" : "uint*", pBitmap)
return pBitmap
}


DeleteObject(hObject)
{
return DllCall("DeleteObject", "uint", hObject)
}

DeleteDC(hdc)
{
return DllCall("DeleteDC", "uint", hdc)
}

GetDCEx(hwnd, flags=0, hrgnClip=0)
{
return DllCall("GetDCEx", "uint", hwnd, "uint", hrgnClip, "int", flags)
}

GetDC(hwnd=0)
{
return DllCall("GetDC", "uint", hwnd)
}

BitBlt(ddc, dx, dy, dw, dh, sdc, sx, sy, Raster="")
{
return DllCall("gdi32\BitBlt", "uint", dDC, "int", dx, "int", dy, "int", dw, "int", dh
, "uint", sDC, "int", sx, "int", sy, "uint", Raster ? Raster : 0x00CC0020)
}

ReleaseDC(hdc, hwnd=0)
{
return DllCall("ReleaseDC", "uint", hwnd, "uint", hdc)
}






;==========================


 







 

▐▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▌

No comments:

Post a Comment