Anyone can fix this script (screenshot paint related)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Himmilayah

Anyone can fix this script (screenshot paint related)

18 Nov 2017, 13:33

I found this script by hqql https://autohotkey.com/board/topic/5665 ... ewbie-way/

All I want to do is get it to save as PNG and not BMP. Just changing the BMP to PNG in the script does nothing because it really still saves it as the size of a BMP.

Anyone can take a look please and fix it?

Run the script and hit the left mouse key, it will be like clicking Alt print screen

Code: Select all

DetectHiddenWindows On
#SingleInstance Force
DetectHiddenText, On
SetTitleMatchMode 2
#WinActivateForce
SetBatchLines -1
#NoTrayIcon

IfNotExist, C:\Program Files\AutoHotkey\Screens
FileCreateDir, C:\Program Files\AutoHotkey\Screens
IfNotExist, C:\Program Files\AutoHotkey\Screens\Counter.txt
FileAppend,, C:\Program Files\AutoHotkey\Screens\Counter.txt
FileRead, cntr, C:\Program Files\AutoHotkey\Screens\Counter.txt
cntr += 1
FileDelete, C:\Program Files\AutoHotkey\Screens\Counter.txt
FileAppend, %cntr%, C:\Program Files\AutoHotkey\Screens\Counter.txt
nwscrn := "C:\Program Files\AutoHotkey\Screens\scrn" . cntr . ".bmp"
pntttl := "scrn" . cntr . ".bmp - Paint"
FileAppend,, %nwscrn%
nwscrn = "%nwscrn%"
clipboard := ""
lbl := ""
Return

LButton Up::
MouseGetPos,,, id
WinActivate, ahk_id %id%
BlockInput, Off
Send !{PrintScreen}
Run, mspaint.exe %nwscrn%,, Hide, OutputVarPID
WinWait, Paint
lbl := "lbl1"
Send ^!{i}
lbl1:
WinWaitActive, %pntttl%
Send ^v
Send ^s
WinWait, Paint
Send {Space}
WinActivate, %pntttl%
WinClose, ahk_pid %OutputVarPID%
lbl := "lbl2"
Send ^!{i}
lbl2:
WinWaitClose, ahk_pid %OutputVarPID%
ExitApp

^!i::
WinActivate, ahk_pid %OutputVarPID%
Sleep 50
WinActivate, %pntttl%
Sleep 50
GoTo, %lbl%
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Anyone can fix this script (screenshot paint related)

18 Nov 2017, 16:46

It looks like it's just calling Save rather than Save As, so you'll have to change that and add a few lines. My version of Paint (in Windows 10) saves to PNG by default, so I'm not sure exactly what the commands should be for you, but, to give you an idea, here's what you might replace the "Send ^s" line with:

Code: Select all

Send {F12}
WinWait, Save As
Send {tab}{down 5}{enter}
I haven't tested this, but that ought to open the Save As window, wait for it, tab down from the name field to the file type drop down box, open it (1 press of the down arrow opens it) and then go down 4 times in the list (since, on my version, PNG is 4 down from 24-bit BMP). You may need to modify it a little, especially the number.
Himmilayah

Re: Anyone can fix this script (screenshot paint related)

18 Nov 2017, 17:11

Osprey, I am on windows 10 as well and the default is png. The thing is, I am trying to keep everything without any windows showing up. Once I call the same as I think the window shows :thumbdown:
teadrinker
Posts: 4344
Joined: 29 Mar 2015, 09:41
Contact:

Re: Anyone can fix this script (screenshot paint related)

18 Nov 2017, 17:28

Hi, Himmilayah,

You don't need to use mspaint. Try:

Code: Select all

CreateScreenshotFile(0, 0, A_ScreenWidth, A_ScreenHeight, A_Desktop . "\test.png")
Run, % A_Desktop . "\test.png"

CreateScreenshotFile(x, y, w, h, filePath)  {
   hBitmap := GetHBitmapFromScreen(x, y, w, h)
   gdip := new GDIplus
   pBitmap := gdip.BitmapFromHBitmap(hBitmap)
   DllCall("DeleteObject", Ptr, hBitmap)
   gdip.SaveBitmapToFile(pBitmap, filePath)
   gdip.DisposeImage(pBitmap)
}

GetHBitmapFromScreen(x, y, w, h)  {
   hDC := DllCall("GetDC", Ptr, 0, Ptr)
   hBM := DllCall("CreateCompatibleBitmap", Ptr, hDC, Int, w, Int, h, Ptr)
   pDC := DllCall("CreateCompatibleDC", Ptr, hDC, Ptr)
   oBM := DllCall("SelectObject", Ptr, pDC, Ptr, hBM, Ptr)
   DllCall("BitBlt", Ptr, pDC, Int, 0, Int, 0, Int, w, Int, h, Ptr, hDC, Int, x, Int, y, UInt, 0x00CC0020)
   DllCall("SelectObject", Ptr, pDC, Ptr, oBM)
   DllCall("DeleteDC", Ptr, pDC)
   DllCall("ReleaseDC", Ptr, 0, Ptr, hDC)
   Return hBM
}

class GDIplus   {
   __New()  {
      if !DllCall("GetModuleHandle", Str, "gdiplus", Ptr)
         DllCall("LoadLibrary", Str, "gdiplus")
      VarSetCapacity(si, A_PtrSize = 8 ? 24 : 16, 0), si := Chr(1)
      DllCall("gdiplus\GdiplusStartup", PtrP, pToken, Ptr, &si, Ptr, 0)
      this.token := pToken
   }
   
   __Delete()  {
      DllCall("gdiplus\GdiplusShutdown", Ptr, this.token)
      if hModule := DllCall("GetModuleHandle", Str, "gdiplus", Ptr)
         DllCall("FreeLibrary", Ptr, hModule)
   }
   
   BitmapFromHBitmap(hBitmap, Palette := 0)  {
      DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", Ptr, hBitmap, Ptr, Palette, PtrP, pBitmap)
      return pBitmap
   }
   
   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

      DllCall("gdiplus\GdipGetImageEncodersSize", UIntP, nCount, UIntP, nSize)
      VarSetCapacity(ci, nSize)
      DllCall("gdiplus\GdipGetImageEncoders", UInt, nCount, UInt, nSize, Ptr, &ci)
      if !(nCount && nSize)
         return -2
      
      Loop, % nCount  {
         sString := StrGet(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
      }
      
      if !pCodec
         return -3

      if RegExMatch(Extension, "i)^J(PG|PEG|PE|FIF)$") && Quality != 75  {
         DllCall("gdiplus\GdipGetEncoderParameterListSize", Ptr, pBitmap, Ptr, pCodec, UintP, 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_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
         pOutput := &sOutput
      else  {
         VarSetCapacity(wOutput, StrPut(sOutput, "UTF-16")*2, 0)
         StrPut(sOutput, &wOutput, "UTF-16")
         pOutput := &wOutput
      }
      E := DllCall("gdiplus\GdipSaveImageToFile", Ptr, pBitmap, Ptr, pOutput, Ptr, pCodec, UInt, p ? p : 0)
      return E ? -5 : 0
   }
   
   DisposeImage(pBitmap)
   {
      return DllCall("gdiplus\GdipDisposeImage", Ptr, pBitmap)
   }
}
Himmilayah

Re: Anyone can fix this script (screenshot paint related)

18 Nov 2017, 17:45

that works very good and fast, always thought to do that ill need to #include gdip or gdip_all etc

thank you

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Aqualest, downstairs, mikeyww, yanjujino1 and 309 guests