SavePicture() : Save hBitmap as BMP, JPG, GIF, PNG, TIF

Post your working scripts, libraries and tools
User avatar
SKAN
Posts: 384
Joined: 29 Sep 2013, 16:58

SavePicture() : Save hBitmap as BMP, JPG, GIF, PNG, TIF

04 Sep 2019, 13:54

SavePicture( hBM, sFile )

Note: This function offers limited functionality: JPEG quality is fixed at 75% and transparency will not be preserved for PNG.
Saved file will be a BMP if you pass any file extension other than: BMP, JPG, JPEG, GIF, PNG, TIF, TIFF

Code: Select all

SavePicture(hBM, sFile) {                                            ; By SKAN on D293 @ bit.ly/2krOIc9
Local V,  pBM := VarSetCapacity(V,16,0)>>8,  Ext := LTrim(SubStr(sFile,-3),"."),  E := [0,0,0,0]
Local Enc := 0x557CF400 | Round({"bmp":0, "jpg":1,"jpeg":1,"gif":2,"tif":5,"tiff":5,"png":6}[Ext])
  E[1] := DllCall("gdi32\GetObjectType", "Ptr",hBM ) <> 7
  E[2] := E[1] ? 0 : DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", "Ptr",hBM, "UInt",0, "PtrP",pBM)
  NumPut(0x2EF31EF8,NumPut(0x0000739A,NumPut(0x11D31A04,NumPut(Enc+0,V,"UInt"),"UInt"),"UInt"),"UInt")
  E[3] := pBM ? DllCall("gdiplus\GdipSaveImageToFile", "Ptr",pBM, "WStr",sFile, "Ptr",&V, "UInt",0) : 1
  E[4] := pBM ? DllCall("gdiplus\GdipDisposeImage", "Ptr",pBM) : 1
Return E[1] ? 0 : E[2] ? -1 : E[3] ? -2 : E[4] ? -3 : 1  
}
  • ErrorLevel


SavePicture() requires GDI+ initialized prior to calling it.
GDI+ can be Start.Shut with GDIP("Startup") and GDIP("Shutdown")
Thanks to @tmplinshi for a bug report. Now fixed.

Code: Select all

GDIP(C:="Startup") {                                      ; By SKAN on D293 @ bit.ly/2krOIc9
  Static SI:=Chr(!(VarSetCapacity(Si,24,0)>>16)), pToken:=0, hMod:=0, Res:=0, AOK:=0
  If (AOK := (C="Startup" and pToken=0) Or (C<>"Startup" and pToken<>0))  {
      If (C="Startup") {
               hMod := DllCall("LoadLibrary", "Str","gdiplus.dll", "Ptr")
               Res  := DllCall("gdiplus\GdiplusStartup", "PtrP",pToken, "Ptr",&SI, "UInt",0)
      } Else { 
               Res  := DllCall("gdiplus\GdiplusShutdown", "Ptr",pToken)
               DllCall("FreeLibrary", "Ptr",hMod),   hMod:=0,   pToken:=0
   }}  
Return (AOK ? !Res : Res:=0)    
}

Usage example

The cryptic nested NumPut explained

The following script will automatically Save picture (JPEG) when PrintScreen / Alt+PrintScreen is used for screen capture.

Code: Screen capture demo
tmplinshi
Posts: 1502
Joined: 01 Oct 2013, 14:57

Re: SavePicture() : Save hBitmap as BMP, JPG, GIF, PNG, TIF

07 Sep 2019, 00:29

Hi, just a bug report for 64-bit AutoHotkey:

The return type of LoadLibrary function should be Ptr, otherwise the calling to FreeLibrary function would be fail.

Code: Select all

MsgBox % DllCall("FreeLibrary", "Ptr",hMod) ? "success" : "fail"
User avatar
SKAN
Posts: 384
Joined: 29 Sep 2013, 16:58

Re: SavePicture() : Save hBitmap as BMP, JPG, GIF, PNG, TIF

07 Sep 2019, 03:11

Thank you @tmplinshi :)
Fixed.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: c600g, yjs778 and 18 guests