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)
}
The following script will automatically Save picture (JPEG) when PrintScreen / Alt+PrintScreen is used for screen capture.