[Function] Resize and Convert Images

Post your working scripts, libraries and tools for AHK v1.1 and older
trust_me
Posts: 98
Joined: 29 Jul 2017, 10:46

Re: [Function] Resize and Convert Images

Post by trust_me » 10 Nov 2020, 09:47

max_size will set well .... max size , set to 200 in the code example :

Code: Select all

F3::                                                 ;Hold F3 to make a selection, then release
{
CoordMode,mouse
    
max_size:=200

MouseGetPos, x1, y1
SoundBeep, 1500, 20
KeyWait, %A_ThisHotkey%
SoundBeep, 1000, 20

MouseGetPos, x2, y2

width := x2 - x1, height := y2 - y1
If (width < 4 && height < 4)
 Return
pToken := Gdip_Startup(), snap := Gdip_BitmapFromScreen(x1 "|" y1 "|" width "|" height)

Gdip_SetBitmapToClipboard(Gdip_ResizeBitmap(snap, max_size))

Gdip_DisposeImage(snap), Gdip_Shutdown(pToken)
}
Return


Gdip_ResizeBitmap(pBitmap, max_size) {	
Gdip_GetImageDimensions(pBitmap, origW, origH)
if (origw>origh)
      {
        newW:=max_size
        newH:=origH*(max_size/origW)
      }
    if (origw<origh)
      {
        newW:=origw*(max_size/origH)
        newH:=max_size
      }
   pBitmap2 := Gdip_CreateBitmap(newW, newH)
   G2 := Gdip_GraphicsFromImage(pBitmap2), Gdip_SetSmoothingMode(G2, 4), Gdip_SetInterpolationMode(G2, 7)
   Gdip_DrawImage(G2, pBitmap, 0, 0, NewW, NewH)
   Gdip_DeleteGraphics(G2)
return pBitmap2
}

User avatar
iilabs
Posts: 296
Joined: 07 Jun 2020, 16:57

Re: [Function] Resize and Convert Images

Post by iilabs » 10 Nov 2020, 12:00

Thank you! That works. A little blurry and the canvas size does not change but image does. Anyway to change the overall size of canvas and keep quality? If not no worries. This will work and thanks for the explanation.

trust_me
Posts: 98
Joined: 29 Jul 2017, 10:46

Re: [Function] Resize and Convert Images

Post by trust_me » 10 Nov 2020, 12:37

Hi iilabs

Can you give some more info about the problem ? I do not see what you mean by canvas size .I tried it and the small image is not blurry but quite good.

Maybe the application your pasting it into is not rendering it quite well ?

User avatar
iilabs
Posts: 296
Joined: 07 Jun 2020, 16:57

Re: [Function] Resize and Convert Images

Post by iilabs » 10 Nov 2020, 17:37

I think you are right. The application I was sending it to was Slack. But when I sent it to paint it was just fine. Any chance you could figure out how to draw the lines with the command? No worries if cant. This adjustment was key!!

trust_me
Posts: 98
Joined: 29 Jul 2017, 10:46

Re: [Function] Resize and Convert Images

Post by trust_me » 11 Nov 2020, 02:03

"Any chance you could figure out how to draw the lines with the command?"

Can you explain what you mean by this , i cannot figure it out :?:

User avatar
iilabs
Posts: 296
Joined: 07 Jun 2020, 16:57

Re: [Function] Resize and Convert Images

Post by iilabs » 11 Nov 2020, 09:14

Basically when I use this script there are no lines being drawn like typical screen clipper. It’s not a big deal but would make it complete.

Also instead of F3 being held down. Is there a way to convert the hotkey to hold Ctrl and drag with right click and mouse?

User avatar
iilabs
Posts: 296
Joined: 07 Jun 2020, 16:57

Re: [Function] Resize and Convert Images

Post by iilabs » 11 Nov 2020, 22:50

I've tried ^RButton as hotkey but doesn't seem to wait for the drag and release to capture. Just seems to function as a Ctrl-Right Click and not hold?

trust_me
Posts: 98
Joined: 29 Jul 2017, 10:46

Re: [Function] Resize and Convert Images

Post by trust_me » 12 Nov 2020, 02:58

Using the same keys inside the code as the hotkey is tricky ......

This seems to work but using some workarounds is also making it unreliable ?

Code: Select all

^Rbutton::
max_size:=200

Hotkey,^Rbutton ,off

send {Rbutton down}
CoordMode,mouse
if !(pToken:=Gdip_Startup())
   {
      msgbox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
      ExitApp
   }

Gui, +LastFound -Caption +AlwaysOnTop +E0x00080000
Gui, Show, NA
hwnd := WinExist()
    hbm := CreateDIBSection(A_ScreenWidth, A_ScreenHeight)
    hdc := CreateCompatibleDC()
    obm := SelectObject(hdc, hbm)
    
pGraphics:= Gdip_GraphicsFromHDC(hdc)
pPen := Gdip_CreatePen(0xff303030, 2)
Gdip_GraphicsClear(pGraphics)
UpdateLayeredWindow(hwnd, hdc, 0, 0, A_ScreenWidth, A_ScreenHeight)

KeyWait, RButton, D
MouseGetPos, x1,y1

while GetKeyState("RButton")
    {
    sleep 100
    Gdip_GraphicsClear(pGraphics)
    MouseGetPos, new_x, new_y

    points:=x1 "," y1 "|" new_x "," y1 "|" new_x "," new_y "|" x1 "," new_y "|" x1 "," y1 

        Gdip_DrawLines(pGraphics, pPen, Points)
        UpdateLayeredWindow(hwnd, hdc, 0, 0, A_ScreenWidth, A_ScreenHeight)
    }

w:=Abs(new_x - x1)-1
h:=Abs(new_y - y1)-1

if w>3
{
    x:=((x1>new_x) ? new_x : x1 )+1
    y:=((y1>new_y) ? new_y : y1 )+1
    
    screen:=x "|" y "|" w "|" h

snap := Gdip_BitmapFromScreen(screen)
resized_snap:=Gdip_ResizeBitmap(snap, max_size)
Gdip_SetBitmapToClipboard(resized_snap)


Gdip_DisposeImage(snap)
Gdip_DisposeImage(resized_snap)
SelectObject(hdc, obm)
DeleteObject(hbm)
DeleteDC(hdc)
Gdip_DeleteGraphics(pGraphics)
Gdip_Shutdown(pToken)
Gui,destroy
}
;send {RButton Up}
hotkey,^RButton,on
Return

esc::ExitApp

Gdip_ResizeBitmap(pBitmap, max_size) {	
Gdip_GetImageDimensions(pBitmap, origW, origH)
if (origw>origh)
      {
        newW:=max_size
        newH:=origH*(max_size/origW)
      }
    if (origw<origh)
      {
        newW:=origw*(max_size/origH)
        newH:=max_size
      }
   pBitmap2 := Gdip_CreateBitmap(newW, newH)
   G2 := Gdip_GraphicsFromImage(pBitmap2), Gdip_SetSmoothingMode(G2, 4), Gdip_SetInterpolationMode(G2, 7)
   Gdip_DrawImage(G2, pBitmap, 0, 0, NewW, NewH)
   Gdip_DeleteGraphics(G2)
return pBitmap2
}

User avatar
iilabs
Posts: 296
Joined: 07 Jun 2020, 16:57

Re: [Function] Resize and Convert Images

Post by iilabs » 12 Nov 2020, 11:35

Awesome. I actually switched it to LButton using your code and worked great! :clap:

Post Reply

Return to “Scripts and Functions (v1)”