Advertisement
twiz

9Scale.ahk

Jun 2nd, 2022
1,534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #SingleInstance, Force  ; Allow only one running instance, relaunches on new instance.
  2. #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
  3. SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
  4. OnExit("ExitFunc")
  5. pToken      := Gdip_Startup()
  6.  
  7. Gdip_9Scale(A_Desktop "\9Scale.png", 500, 500, 5)
  8. Return
  9.  
  10.  
  11. Gdip_9Scale(_InputFile, _NewW, _NewH, _X1, _Y1:="", _X2:="", _Y2:="") {
  12.     Static
  13.     If (_Y1 = "")
  14.         _Y1 := _X1
  15.     If (_X2 = "")
  16.         _X2 := _X1
  17.     If (_Y2 = "")
  18.         _Y2 := _Y1
  19.  
  20.     ;   Input file
  21.     pBitmapIn   := Gdip_CreateBitmapFromFile(_InputFile)
  22.     _imgW       := Gdip_GetImageWidth(pBitmapIn)
  23.     _imgH       := Gdip_GetImageHeight(pBitmapIn)
  24.    
  25.     If (_X1 + _X2 >= _imgW) OR (_Y1 + _Y2 >= _imgH) {
  26.         MsgBox, Too big.
  27.         Goto, Cleanup
  28.     }
  29.  
  30.     ;   Split image into 9 parts:
  31.     ;       1   2   3
  32.     ;       4   5   6
  33.     ;       7   8   9
  34.     _W1     := _imgW - (_X1 + _X2)
  35.     _W2     := _imgW - _X2
  36.     _H1     := _imgH - (_Y1 + _Y2)
  37.     _H2     := _imgH - _Y2
  38.     pBitmap1 := Gdip_CloneBitmapArea(pBitmapIn  , 0     , 0     , _X1   , _Y1)
  39.     pBitmap2 := Gdip_CloneBitmapArea(pBitmapIn  , _X1   , 0     , _W1   , _Y1)
  40.     pBitmap3 := Gdip_CloneBitmapArea(pBitmapIn  , _W2   , 0     , _X2   , _Y1)
  41.     pBitmap4 := Gdip_CloneBitmapArea(pBitmapIn  , 0     , _Y1   , _X1   , _H1)
  42.     pBitmap5 := Gdip_CloneBitmapArea(pBitmapIn  , _X1   , _Y1   , _W1   , _H1)
  43.     pBitmap6 := Gdip_CloneBitmapArea(pBitmapIn  , _W2   , _Y1   , _X2   , _H1)
  44.     pBitmap7 := Gdip_CloneBitmapArea(pBitmapIn  , 0     , _H2   , _X1   , _Y2)
  45.     pBitmap8 := Gdip_CloneBitmapArea(pBitmapIn  , _X1   , _H2   , _W1   , _Y2)
  46.     pBitmap9 := Gdip_CloneBitmapArea(pBitmapIn  , _W2   , _H2   , _X2   , _Y2)
  47.  
  48.     ;   Draw the output file
  49.     _CW         := _NewW - (_X1 + _X2)
  50.     _CH         := _NewH - (_Y1 + _Y2)
  51.     pBitmapOut  := Gdip_CreateBitmap(_NewW, _NewH)
  52.     gBitmapOut  := Gdip_GraphicsFromImage(pBitmapOut)
  53.     Gdip_SetInterpolationMode(gBitmapOut, 5)
  54.     Gdip_SetSmoothingMode(gBitmapOut, 3)
  55.  
  56.     gdip_DrawImage(gBitmapOut, pBitmap1 , 0         , 0         , _X1   , _Y1)
  57.     gdip_DrawImage(gBitmapOut, pBitmap2 , _X1       , 0         , _CW   , _Y1)  ; This line X
  58.     gdip_DrawImage(gBitmapOut, pBitmap3 , _X1 + _CW , 0         , _X2   , _Y1)
  59.     gdip_DrawImage(gBitmapOut, pBitmap4 , 0         , _Y1       , _X1   , _CH)  ; This line Y
  60.     gdip_DrawImage(gBitmapOut, pBitmap5 , _X1       , _Y1       , _CW   , _CH)  ; This line X, Y
  61.     gdip_DrawImage(gBitmapOut, pBitmap6 , _X1 + _CW , _Y1       , _X2   , _CH)  ; This line Y
  62.     gdip_DrawImage(gBitmapOut, pBitmap7 , 0         , _Y1 + _CH , _X1   , _Y2)
  63.     gdip_DrawImage(gBitmapOut, pBitmap8 , _X1       , _Y1 + _CH , _CW   , _Y2)  ; This line X
  64.     gdip_DrawImage(gBitmapOut, pBitmap9 , _X1 + _CW , _Y1 + _CH , _X2   , _Y2)
  65.  
  66.     Gui, -MinimizeBox -MaximizeBox +LastFound
  67.     Gui, Add, Picture, 0xE vpNew        x0      y0      w%_NewW%    h%_NewH%
  68.     GuiControlGet, hwndpNew, hwnd, pNew
  69.     hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmapOut) ; Convert Pointer to Handle
  70.     SetImage(hwndpNew, hBitmap) ; Set tool handle as image in GUI
  71.     Gdip_DisposeImage(hBitmap)
  72.     Gui, Show, x360 y240 w%_NewW% h%_NewH%
  73.  
  74. Cleanup:
  75.     ;   Delete Resources
  76.     Gdip_DisposeImage(pBitmapIn)
  77.     Gdip_DisposeImage(pBitmap1)
  78.     Gdip_DisposeImage(pBitmap2)
  79.     Gdip_DisposeImage(pBitmap3)
  80.     Gdip_DisposeImage(pBitmap4)
  81.     Gdip_DisposeImage(pBitmap5)
  82.     Gdip_DisposeImage(pBitmap6)
  83.     Gdip_DisposeImage(pBitmap7)
  84.     Gdip_DisposeImage(pBitmap8)
  85.     Gdip_DisposeImage(pBitmap9)
  86.     Gdip_DisposeImage(pBitmapOut)
  87.     Gdip_DeleteGraphics(gBitmapOut)
  88. }
  89.  
  90. GuiClose:
  91. ExitApp
  92.  
  93. ExitFunc() {
  94.     Global
  95.     Gdip_Shutdown(pToken)
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement