; ;################################################################################################ ; Takes a screenshot, names it and puts it in folder called "ScreenShots" ;This entire script is just an edit of Gdip standard library v1.45 by tic (Tariq Porter) 07/09/11 ; ; Gdip code available from: http://www.autohotkey.net/~tic/Gdip.ahk ; Gdip tutorial: http://www.autohotkey.com/board/topic/29449-gdi-standard-library-145-by-tic/ ;################################################################################################ ;################################################################################################ pToken := Gdip_Startup() OnExit, shutdown folderPath := A_ScriptDir "\ScreenShots\" IfNotExist, %folderPath% { FileCreateDir, %folderPath% } fileName := A_YYYY "-" A_MM "-" A_DD "-" A_Hour "-" A_Min "-" A_Sec ".jpg" ;;NOTE: other formats are supported, just replace "jpg" with "png" or another format in the fileName. CaptureScreen: pBitmap := Gdip_BitmapFromScreen() saveFileTo := folderPath fileName Gdip_SaveBitmapToFile(pBitmap, saveFileTo) Gdip_DisposeImage(pBitmap) return shutdown: Gdip_Shutdown(pToken) Exitapp return ;################################################################################################ ; All that follows are copied from the Gdip standard library v1.45 by tic (Tariq Porter) 07/09/11 ; ;################################################################################################ ;################################################################################################ ; Function Gdip_BitmapFromScreen ; Description Gets a gdi+ bitmap from the screen ; ; Screen 0 = All screens ; Any numerical value = Just that screen ; x|y|w|h = Take specific coordinates with a width and height ; Raster raster operation code ; ; return If the function succeeds, the return value is a pointer to a gdi+ bitmap ; -1: one or more of x,y,w,h not passed properly ; ; notes If no raster operation is specified, then SRCCOPY is used to the returned bitmap 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 } ;##################################################################################### ; Function: Gdip_SaveBitmapToFile ; Description: Saves a bitmap to a file in any supported format onto disk ; ; pBitmap Pointer to a bitmap ; sOutput The name of the file that the bitmap will be saved to. Supported extensions are: .BMP,.DIB,.RLE,.JPG,.JPEG,.JPE,.JFIF,.GIF,.TIF,.TIFF,.PNG ; Quality If saving as jpg (.JPG,.JPEG,.JPE,.JFIF) then quality can be 1-100 with default at maximum quality ; ; return If the function succeeds, the return value is zero, otherwise: ; -1 = Extension supplied is not a supported file format ; -2 = Could not get a list of encoders on system ; -3 = Could not find matching encoder for specified file format ; -4 = Could not get WideChar name of output file ; -5 = Could not save file to disk ; ; notes This function will use the extension supplied from the sOutput parameter to determine the output format Gdip_SaveBitmapToFile(pBitmap, sOutput, Quality=75) { 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, "uint", &ci) if !(nCount && nSize) return -2 Loop, %nCount% { Location := NumGet(ci, 76*(A_Index-1)+44) if !A_IsUnicode { 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 } else { nSize := DllCall("WideCharToMultiByte", "uint", 0, "uint", 0, "uint", Location, "int", -1, "uint", 0, "int", 0, "uint", 0, "uint", 0) sString := "" Loop, %nSize% sString .= Chr(NumGet(Location+0, 2*(A_Index-1), "char")) 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", "uint", pBitmap, "uint", pCodec, "uint*", nSize) VarSetCapacity(EncoderParameters, nSize, 0) DllCall("gdiplus\GdipGetEncoderParameterList", "uint", pBitmap, "uint", pCodec, "uint", nSize, "uint", &EncoderParameters) Loop, % NumGet(EncoderParameters) ;% { if (NumGet(EncoderParameters, (28*(A_Index-1))+20) = 1) && (NumGet(EncoderParameters, (28*(A_Index-1))+24) = 6) { p := (28*(A_Index-1))+&EncoderParameters NumPut(Quality, NumGet(NumPut(4, NumPut(1, p+0)+20))) break } } } } if !A_IsUnicode { nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, "uint", &sOutput, "int", -1, "uint", 0, "int", 0) VarSetCapacity(wOutput, nSize*2) DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, "uint", &sOutput, "int", -1, "uint", &wOutput, "int", nSize) VarSetCapacity(wOutput, -1) if !VarSetCapacity(wOutput) return -4 E := DllCall("gdiplus\GdipSaveImageToFile", "uint", pBitmap, "uint", &wOutput, "uint", pCodec, "uint", p ? p : 0) } else E := DllCall("gdiplus\GdipSaveImageToFile", "uint", pBitmap, "uint", &sOutput, "uint", pCodec, "uint", p ? p : 0) return E ? -5 : 0 } ;##################################################################################### Gdip_DisposeImage(pBitmap) { return DllCall("gdiplus\GdipDisposeImage", "uint", pBitmap) } ;##################################################################################### ; DCX_CACHE = 0x2 ; DCX_CLIPCHILDREN = 0x8 ; DCX_CLIPSIBLINGS = 0x10 ; DCX_EXCLUDERGN = 0x40 ; DCX_EXCLUDEUPDATE = 0x100 ; DCX_INTERSECTRGN = 0x80 ; DCX_INTERSECTUPDATE = 0x200 ; DCX_LOCKWINDOWUPDATE = 0x400 ; DCX_NORECOMPUTE = 0x100000 ; DCX_NORESETATTRS = 0x4 ; DCX_PARENTCLIP = 0x20 ; DCX_VALIDATE = 0x200000 ; DCX_WINDOW = 0x1 GetDCEx(hwnd, flags=0, hrgnClip=0) { return DllCall("GetDCEx", "uint", hwnd, "uint", hrgnClip, "int", flags) } ;##################################################################################### ; Function CreateCompatibleDC ; Description This function creates a memory device context (DC) compatible with the specified device ; ; hdc Handle to an existing device context ; ; return returns the handle to a device context or 0 on failure ; ; notes If this handle is 0 (by default), the function creates a memory device context compatible with the application's current screen CreateCompatibleDC(hdc=0) { return DllCall("CreateCompatibleDC", "uint", hdc) } ;##################################################################################### ; Function CreateDIBSection ; Description The CreateDIBSection function creates a DIB (Device Independent Bitmap) that applications can write to directly ; ; w width of the bitmap to create ; h height of the bitmap to create ; hdc a handle to the device context to use the palette from ; bpp bits per pixel (32 = ARGB) ; ppvBits A pointer to a variable that receives a pointer to the location of the DIB bit values ; ; return returns a DIB. A gdi bitmap ; ; notes ppvBits will receive the location of the pixels in the DIB CreateDIBSection(w, h, hdc="", bpp=32, ByRef ppvBits=0) { hdc2 := hdc ? hdc : GetDC() VarSetCapacity(bi, 40, 0) NumPut(w, bi, 4), NumPut(h, bi, 8), NumPut(40, bi, 0), NumPut(1, bi, 12, "ushort"), NumPut(0, bi, 16), NumPut(bpp, bi, 14, "ushort") hbm := DllCall("CreateDIBSection", "uint" , hdc2, "uint" , &bi, "uint" , 0, "uint*", ppvBits, "uint" , 0, "uint" , 0) if !hdc ReleaseDC(hdc2) return hbm } ;##################################################################################### ; Function SelectObject ; Description The SelectObject function selects an object into the specified device context (DC). The new object replaces the previous object of the same type ; ; hdc Handle to a DC ; hgdiobj A handle to the object to be selected into the DC ; ; return If the selected object is not a region and the function succeeds, the return value is a handle to the object being replaced ; ; notes The specified object must have been created by using one of the following functions ; Bitmap - CreateBitmap, CreateBitmapIndirect, CreateCompatibleBitmap, CreateDIBitmap, CreateDIBSection (A single bitmap cannot be selected into more than one DC at the same time) ; Brush - CreateBrushIndirect, CreateDIBPatternBrush, CreateDIBPatternBrushPt, CreateHatchBrush, CreatePatternBrush, CreateSolidBrush ; Font - CreateFont, CreateFontIndirect ; Pen - CreatePen, CreatePenIndirect ; Region - CombineRgn, CreateEllipticRgn, CreateEllipticRgnIndirect, CreatePolygonRgn, CreateRectRgn, CreateRectRgnIndirect ; ; notes If the selected object is a region and the function succeeds, the return value is one of the following value ; ; SIMPLEREGION = 2 Region consists of a single rectangle ; COMPLEXREGION = 3 Region consists of more than one rectangle ; NULLREGION = 1 Region is empty SelectObject(hdc, hgdiobj) { return DllCall("SelectObject", "uint", hdc, "uint", hgdiobj) } ;##################################################################################### ; Function GetDC ; Description This function retrieves a handle to a display device context (DC) for the client area of the specified window. ; The display device context can be used in subsequent graphics display interface (GDI) functions to draw in the client area of the window. ; ; hwnd Handle to the window whose device context is to be retrieved. If this value is NULL, GetDC retrieves the device context for the entire screen ; ; return The handle the device context for the specified window's client area indicates success. NULL indicates failure GetDC(hwnd=0) { return DllCall("GetDC", "uint", hwnd) } ;##################################################################################### ; Function BitBlt ; Description The BitBlt function performs a bit-block transfer of the color data corresponding to a rectangle ; of pixels from the specified source device context into a destination device context. ; ; dDC handle to destination DC ; dx x-coord of destination upper-left corner ; dy y-coord of destination upper-left corner ; dw width of the area to copy ; dh height of the area to copy ; sDC handle to source DC ; sx x-coordinate of source upper-left corner ; sy y-coordinate of source upper-left corner ; Raster raster operation code ; ; return If the function succeeds, the return value is nonzero ; ; notes If no raster operation is specified, then SRCCOPY is used, which copies the source directly to the destination rectangle ; ; BLACKNESS = 0x00000042 ; NOTSRCERASE = 0x001100A6 ; NOTSRCCOPY = 0x00330008 ; SRCERASE = 0x00440328 ; DSTINVERT = 0x00550009 ; PATINVERT = 0x005A0049 ; SRCINVERT = 0x00660046 ; SRCAND = 0x008800C6 ; MERGEPAINT = 0x00BB0226 ; MERGECOPY = 0x00C000CA ; SRCCOPY = 0x00CC0020 ; SRCPAINT = 0x00EE0086 ; PATCOPY = 0x00F00021 ; PATPAINT = 0x00FB0A09 ; WHITENESS = 0x00FF0062 ; CAPTUREBLT = 0x40000000 ; NOMIRRORBITMAP = 0x80000000 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) } ;##################################################################################### ; Function ReleaseDC ; Description This function releases a device context (DC), freeing it for use by other applications. The effect of ReleaseDC depends on the type of device context ; ; hdc Handle to the device context to be released ; hwnd Handle to the window whose device context is to be released ; ; return 1 = released ; 0 = not released ; ; notes The application must call the ReleaseDC function for each call to the GetWindowDC function and for each call to the GetDC function that retrieves a common device context ; An application cannot use the ReleaseDC function to release a device context that was created by calling the CreateDC function; instead, it must use the DeleteDC function. ReleaseDC(hdc, hwnd=0) { return DllCall("ReleaseDC", "uint", hwnd, "uint", hdc) } ;##################################################################################### Gdip_CreateBitmapFromHBITMAP(hBitmap, Palette=0) { DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", "uint", hBitmap, "uint", Palette, "uint*", pBitmap) return pBitmap } ;##################################################################################### ; Function DeleteObject ; Description This function deletes a logical pen, brush, font, bitmap, region, or palette, freeing all system resources associated with the object ; After the object is deleted, the specified handle is no longer valid ; ; hObject Handle to a logical pen, brush, font, bitmap, region, or palette to delete ; ; return Nonzero indicates success. Zero indicates that the specified handle is not valid or that the handle is currently selected into a device context DeleteObject(hObject) { return DllCall("DeleteObject", "uint", hObject) } ;##################################################################################### ; Function DeleteDC ; Description The DeleteDC function deletes the specified device context (DC) ; ; hdc A handle to the device context ; ; return If the function succeeds, the return value is nonzero ; ; notes An application must not delete a DC whose handle was obtained by calling the GetDC function. Instead, it must call the ReleaseDC function to free the DC DeleteDC(hdc) { return DllCall("DeleteDC", "uint", hdc) } ;##################################################################################### Gdip_Startup() { if !DllCall("GetModuleHandle", "str", "gdiplus") DllCall("LoadLibrary", "str", "gdiplus") VarSetCapacity(si, 16, 0), si := Chr(1) DllCall("gdiplus\GdiplusStartup", "uint*", pToken, "uint", &si, "uint", 0) return pToken } ;##################################################################################### Gdip_Shutdown(pToken) { DllCall("gdiplus\GdiplusShutdown", "uint", pToken) if hModule := DllCall("GetModuleHandle", "str", "gdiplus") DllCall("FreeLibrary", "uint", hModule) return 0 }