Snow_Flake

GdipAll

Aug 25th, 2014
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Gdip standard library v1.45 by tic (Tariq Porter) 07/09/11
  2. ; Modifed by Rseding91 using fincs 64 bit compatible Gdip library 5/1/2013
  3. ; Supports: Basic, _L ANSi, _L Unicode x86 and _L Unicode x64
  4. ;
  5. ;#####################################################################################
  6. ;#####################################################################################
  7. ; STATUS ENUMERATION
  8. ; Return values for functions specified to have status enumerated return type
  9. ;#####################################################################################
  10. ;
  11. ; Ok =                      = 0
  12. ; GenericError              = 1
  13. ; InvalidParameter          = 2
  14. ; OutOfMemory               = 3
  15. ; ObjectBusy                = 4
  16. ; InsufficientBuffer        = 5
  17. ; NotImplemented            = 6
  18. ; Win32Error                = 7
  19. ; WrongState                = 8
  20. ; Aborted                   = 9
  21. ; FileNotFound              = 10
  22. ; ValueOverflow             = 11
  23. ; AccessDenied              = 12
  24. ; UnknownImageFormat        = 13
  25. ; FontFamilyNotFound        = 14
  26. ; FontStyleNotFound         = 15
  27. ; NotTrueTypeFont           = 16
  28. ; UnsupportedGdiplusVersion = 17
  29. ; GdiplusNotInitialized     = 18
  30. ; PropertyNotFound          = 19
  31. ; PropertyNotSupported      = 20
  32. ; ProfileNotFound           = 21
  33. ;
  34. ;#####################################################################################
  35. ;#####################################################################################
  36. ; FUNCTIONS
  37. ;#####################################################################################
  38. ;
  39. ; UpdateLayeredWindow(hwnd, hdc, x="", y="", w="", h="", Alpha=255)
  40. ; BitBlt(ddc, dx, dy, dw, dh, sdc, sx, sy, Raster="")
  41. ; StretchBlt(dDC, dx, dy, dw, dh, sDC, sx, sy, sw, sh, Raster="")
  42. ; SetImage(hwnd, hBitmap)
  43. ; Gdip_BitmapFromScreen(Screen=0, Raster="")
  44. ; CreateRectF(ByRef RectF, x, y, w, h)
  45. ; CreateSizeF(ByRef SizeF, w, h)
  46. ; CreateDIBSection
  47. ;
  48. ;#####################################################################################
  49.  
  50. ; Function:                 UpdateLayeredWindow
  51. ; Description:              Updates a layered window with the handle to the DC of a gdi bitmap
  52. ;
  53. ; hwnd                      Handle of the layered window to update
  54. ; hdc                       Handle to the DC of the GDI bitmap to update the window with
  55. ; Layeredx                  x position to place the window
  56. ; Layeredy                  y position to place the window
  57. ; Layeredw                  Width of the window
  58. ; Layeredh                  Height of the window
  59. ; Alpha                     Default = 255 : The transparency (0-255) to set the window transparency
  60. ;
  61. ; return                    If the function succeeds, the return value is nonzero
  62. ;
  63. ; notes                     If x or y omitted, then layered window will use its current coordinates
  64. ;                           If w or h omitted then current width and height will be used
  65.  
  66. UpdateLayeredWindow(hwnd, hdc, x="", y="", w="", h="", Alpha=255)
  67. {
  68.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  69.    
  70.     if ((x != "") && (y != ""))
  71.         VarSetCapacity(pt, 8), NumPut(x, pt, 0, "UInt"), NumPut(y, pt, 4, "UInt")
  72.  
  73.     if (w = "") ||(h = "")
  74.         WinGetPos,,, w, h, ahk_id %hwnd%
  75.    
  76.     return DllCall("UpdateLayeredWindow"
  77.                     , Ptr, hwnd
  78.                     , Ptr, 0
  79.                     , Ptr, ((x = "") && (y = "")) ? 0 : &pt
  80.                     , "int64*", w|h<<32
  81.                     , Ptr, hdc
  82.                     , "int64*", 0
  83.                     , "uint", 0
  84.                     , "UInt*", Alpha<<16|1<<24
  85.                     , "uint", 2)
  86. }
  87.  
  88. ;#####################################################################################
  89.  
  90. ; Function              BitBlt
  91. ; Description           The BitBlt function performs a bit-block transfer of the color data corresponding to a rectangle
  92. ;                       of pixels from the specified source device context into a destination device context.
  93. ;
  94. ; dDC                   handle to destination DC
  95. ; dx                    x-coord of destination upper-left corner
  96. ; dy                    y-coord of destination upper-left corner
  97. ; dw                    width of the area to copy
  98. ; dh                    height of the area to copy
  99. ; sDC                   handle to source DC
  100. ; sx                    x-coordinate of source upper-left corner
  101. ; sy                    y-coordinate of source upper-left corner
  102. ; Raster                raster operation code
  103. ;
  104. ; return                If the function succeeds, the return value is nonzero
  105. ;
  106. ; notes                 If no raster operation is specified, then SRCCOPY is used, which copies the source directly to the destination rectangle
  107. ;
  108. ; BLACKNESS             = 0x00000042
  109. ; NOTSRCERASE           = 0x001100A6
  110. ; NOTSRCCOPY            = 0x00330008
  111. ; SRCERASE              = 0x00440328
  112. ; DSTINVERT             = 0x00550009
  113. ; PATINVERT             = 0x005A0049
  114. ; SRCINVERT             = 0x00660046
  115. ; SRCAND                = 0x008800C6
  116. ; MERGEPAINT            = 0x00BB0226
  117. ; MERGECOPY             = 0x00C000CA
  118. ; SRCCOPY               = 0x00CC0020
  119. ; SRCPAINT              = 0x00EE0086
  120. ; PATCOPY               = 0x00F00021
  121. ; PATPAINT              = 0x00FB0A09
  122. ; WHITENESS             = 0x00FF0062
  123. ; CAPTUREBLT            = 0x40000000
  124. ; NOMIRRORBITMAP        = 0x80000000
  125.  
  126. BitBlt(ddc, dx, dy, dw, dh, sdc, sx, sy, Raster="")
  127. {
  128.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  129.    
  130.     return DllCall("gdi32\BitBlt"
  131.                     , Ptr, dDC
  132.                     , "int", dx
  133.                     , "int", dy
  134.                     , "int", dw
  135.                     , "int", dh
  136.                     , Ptr, sDC
  137.                     , "int", sx
  138.                     , "int", sy
  139.                     , "uint", Raster ? Raster : 0x00CC0020)
  140. }
  141.  
  142. ;#####################################################################################
  143.  
  144. ; Function              StretchBlt
  145. ; Description           The StretchBlt function copies a bitmap from a source rectangle into a destination rectangle,
  146. ;                       stretching or compressing the bitmap to fit the dimensions of the destination rectangle, if necessary.
  147. ;                       The system stretches or compresses the bitmap according to the stretching mode currently set in the destination device context.
  148. ;
  149. ; ddc                   handle to destination DC
  150. ; dx                    x-coord of destination upper-left corner
  151. ; dy                    y-coord of destination upper-left corner
  152. ; dw                    width of destination rectangle
  153. ; dh                    height of destination rectangle
  154. ; sdc                   handle to source DC
  155. ; sx                    x-coordinate of source upper-left corner
  156. ; sy                    y-coordinate of source upper-left corner
  157. ; sw                    width of source rectangle
  158. ; sh                    height of source rectangle
  159. ; Raster                raster operation code
  160. ;
  161. ; return                If the function succeeds, the return value is nonzero
  162. ;
  163. ; notes                 If no raster operation is specified, then SRCCOPY is used. It uses the same raster operations as BitBlt    
  164.  
  165. StretchBlt(ddc, dx, dy, dw, dh, sdc, sx, sy, sw, sh, Raster="")
  166. {
  167.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  168.    
  169.     return DllCall("gdi32\StretchBlt"
  170.                     , Ptr, ddc
  171.                     , "int", dx
  172.                     , "int", dy
  173.                     , "int", dw
  174.                     , "int", dh
  175.                     , Ptr, sdc
  176.                     , "int", sx
  177.                     , "int", sy
  178.                     , "int", sw
  179.                     , "int", sh
  180.                     , "uint", Raster ? Raster : 0x00CC0020)
  181. }
  182.  
  183. ;#####################################################################################
  184.  
  185. ; Function              SetStretchBltMode
  186. ; Description           The SetStretchBltMode function sets the bitmap stretching mode in the specified device context
  187. ;
  188. ; hdc                   handle to the DC
  189. ; iStretchMode          The stretching mode, describing how the target will be stretched
  190. ;
  191. ; return                If the function succeeds, the return value is the previous stretching mode. If it fails it will return 0
  192. ;
  193. ; STRETCH_ANDSCANS      = 0x01
  194. ; STRETCH_ORSCANS       = 0x02
  195. ; STRETCH_DELETESCANS   = 0x03
  196. ; STRETCH_HALFTONE      = 0x04
  197.  
  198. SetStretchBltMode(hdc, iStretchMode=4)
  199. {
  200.     return DllCall("gdi32\SetStretchBltMode"
  201.                     , A_PtrSize ? "UPtr" : "UInt", hdc
  202.                     , "int", iStretchMode)
  203. }
  204.  
  205. ;#####################################################################################
  206.  
  207. ; Function              SetImage
  208. ; Description           Associates a new image with a static control
  209. ;
  210. ; hwnd                  handle of the control to update
  211. ; hBitmap               a gdi bitmap to associate the static control with
  212. ;
  213. ; return                If the function succeeds, the return value is nonzero
  214.  
  215. SetImage(hwnd, hBitmap)
  216. {
  217.     SendMessage, 0x172, 0x0, hBitmap,, ahk_id %hwnd%
  218.     E := ErrorLevel
  219.     DeleteObject(E)
  220.     return E
  221. }
  222.  
  223. ;#####################################################################################
  224.  
  225. ; Function              SetSysColorToControl
  226. ; Description           Sets a solid colour to a control
  227. ;
  228. ; hwnd                  handle of the control to update
  229. ; SysColor              A system colour to set to the control
  230. ;
  231. ; return                If the function succeeds, the return value is zero
  232. ;
  233. ; notes                 A control must have the 0xE style set to it so it is recognised as a bitmap
  234. ;                       By default SysColor=15 is used which is COLOR_3DFACE. This is the standard background for a control
  235. ;
  236. ; COLOR_3DDKSHADOW              = 21
  237. ; COLOR_3DFACE                  = 15
  238. ; COLOR_3DHIGHLIGHT             = 20
  239. ; COLOR_3DHILIGHT               = 20
  240. ; COLOR_3DLIGHT                 = 22
  241. ; COLOR_3DSHADOW                = 16
  242. ; COLOR_ACTIVEBORDER            = 10
  243. ; COLOR_ACTIVECAPTION           = 2
  244. ; COLOR_APPWORKSPACE            = 12
  245. ; COLOR_BACKGROUND              = 1
  246. ; COLOR_BTNFACE                 = 15
  247. ; COLOR_BTNHIGHLIGHT            = 20
  248. ; COLOR_BTNHILIGHT              = 20
  249. ; COLOR_BTNSHADOW               = 16
  250. ; COLOR_BTNTEXT                 = 18
  251. ; COLOR_CAPTIONTEXT             = 9
  252. ; COLOR_DESKTOP                 = 1
  253. ; COLOR_GRADIENTACTIVECAPTION   = 27
  254. ; COLOR_GRADIENTINACTIVECAPTION = 28
  255. ; COLOR_GRAYTEXT                = 17
  256. ; COLOR_HIGHLIGHT               = 13
  257. ; COLOR_HIGHLIGHTTEXT           = 14
  258. ; COLOR_HOTLIGHT                = 26
  259. ; COLOR_INACTIVEBORDER          = 11
  260. ; COLOR_INACTIVECAPTION         = 3
  261. ; COLOR_INACTIVECAPTIONTEXT     = 19
  262. ; COLOR_INFOBK                  = 24
  263. ; COLOR_INFOTEXT                = 23
  264. ; COLOR_MENU                    = 4
  265. ; COLOR_MENUHILIGHT             = 29
  266. ; COLOR_MENUBAR                 = 30
  267. ; COLOR_MENUTEXT                = 7
  268. ; COLOR_SCROLLBAR               = 0
  269. ; COLOR_WINDOW                  = 5
  270. ; COLOR_WINDOWFRAME             = 6
  271. ; COLOR_WINDOWTEXT              = 8
  272.  
  273. SetSysColorToControl(hwnd, SysColor=15)
  274. {
  275.    WinGetPos,,, w, h, ahk_id %hwnd%
  276.    bc := DllCall("GetSysColor", "Int", SysColor, "UInt")
  277.    pBrushClear := Gdip_BrushCreateSolid(0xff000000 | (bc >> 16 | bc & 0xff00 | (bc & 0xff) << 16))
  278.    pBitmap := Gdip_CreateBitmap(w, h), G := Gdip_GraphicsFromImage(pBitmap)
  279.    Gdip_FillRectangle(G, pBrushClear, 0, 0, w, h)
  280.    hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
  281.    SetImage(hwnd, hBitmap)
  282.    Gdip_DeleteBrush(pBrushClear)
  283.    Gdip_DeleteGraphics(G), Gdip_DisposeImage(pBitmap), DeleteObject(hBitmap)
  284.    return 0
  285. }
  286.  
  287. ;#####################################################################################
  288.  
  289. ; Function              Gdip_BitmapFromScreen
  290. ; Description           Gets a gdi+ bitmap from the screen
  291. ;
  292. ; Screen                0 = All screens
  293. ;                       Any numerical value = Just that screen
  294. ;                       x|y|w|h = Take specific coordinates with a width and height
  295. ; Raster                raster operation code
  296. ;
  297. ; return                If the function succeeds, the return value is a pointer to a gdi+ bitmap
  298. ;                       -1:     one or more of x,y,w,h not passed properly
  299. ;
  300. ; notes                 If no raster operation is specified, then SRCCOPY is used to the returned bitmap
  301.  
  302. Gdip_BitmapFromScreen(Screen=0, Raster="")
  303. {
  304.     if (Screen = 0)
  305.     {
  306.         Sysget, x, 76
  307.         Sysget, y, 77  
  308.         Sysget, w, 78
  309.         Sysget, h, 79
  310.     }
  311.     else if (SubStr(Screen, 1, 5) = "hwnd:")
  312.     {
  313.         Screen := SubStr(Screen, 6)
  314.         if !WinExist( "ahk_id " Screen)
  315.             return -2
  316.         WinGetPos,,, w, h, ahk_id %Screen%
  317.         x := y := 0
  318.         hhdc := GetDCEx(Screen, 3)
  319.     }
  320.     else if (Screen&1 != "")
  321.     {
  322.         Sysget, M, Monitor, %Screen%
  323.         x := MLeft, y := MTop, w := MRight-MLeft, h := MBottom-MTop
  324.     }
  325.     else
  326.     {
  327.         StringSplit, S, Screen, |
  328.         x := S1, y := S2, w := S3, h := S4
  329.     }
  330.  
  331.     if (x = "") || (y = "") || (w = "") || (h = "")
  332.         return -1
  333.  
  334.     chdc := CreateCompatibleDC(), hbm := CreateDIBSection(w, h, chdc), obm := SelectObject(chdc, hbm), hhdc := hhdc ? hhdc : GetDC()
  335.     BitBlt(chdc, 0, 0, w, h, hhdc, x, y, Raster)
  336.     ReleaseDC(hhdc)
  337.    
  338.     pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
  339.     SelectObject(chdc, obm), DeleteObject(hbm), DeleteDC(hhdc), DeleteDC(chdc)
  340.     return pBitmap
  341. }
  342.  
  343. ;#####################################################################################
  344.  
  345. ; Function              Gdip_BitmapFromHWND
  346. ; Description           Uses PrintWindow to get a handle to the specified window and return a bitmap from it
  347. ;
  348. ; hwnd                  handle to the window to get a bitmap from
  349. ;
  350. ; return                If the function succeeds, the return value is a pointer to a gdi+ bitmap
  351. ;
  352. ; notes                 Window must not be not minimised in order to get a handle to it's client area
  353.  
  354. Gdip_BitmapFromHWND(hwnd)
  355. {
  356.     WinGetPos,,, Width, Height, ahk_id %hwnd%
  357.     hbm := CreateDIBSection(Width, Height), hdc := CreateCompatibleDC(), obm := SelectObject(hdc, hbm)
  358.     PrintWindow(hwnd, hdc)
  359.     pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
  360.     SelectObject(hdc, obm), DeleteObject(hbm), DeleteDC(hdc)
  361.     return pBitmap
  362. }
  363.  
  364. ;#####################################################################################
  365.  
  366. ; Function              CreateRectF
  367. ; Description           Creates a RectF object, containing a the coordinates and dimensions of a rectangle
  368. ;
  369. ; RectF                 Name to call the RectF object
  370. ; x                     x-coordinate of the upper left corner of the rectangle
  371. ; y                     y-coordinate of the upper left corner of the rectangle
  372. ; w                     Width of the rectangle
  373. ; h                     Height of the rectangle
  374. ;
  375. ; return                No return value
  376.  
  377. CreateRectF(ByRef RectF, x, y, w, h)
  378. {
  379.    VarSetCapacity(RectF, 16)
  380.    NumPut(x, RectF, 0, "float"), NumPut(y, RectF, 4, "float"), NumPut(w, RectF, 8, "float"), NumPut(h, RectF, 12, "float")
  381. }
  382.  
  383. ;#####################################################################################
  384.  
  385. ; Function              CreateRect
  386. ; Description           Creates a Rect object, containing a the coordinates and dimensions of a rectangle
  387. ;
  388. ; RectF                 Name to call the RectF object
  389. ; x                     x-coordinate of the upper left corner of the rectangle
  390. ; y                     y-coordinate of the upper left corner of the rectangle
  391. ; w                     Width of the rectangle
  392. ; h                     Height of the rectangle
  393. ;
  394. ; return                No return value
  395.  
  396. CreateRect(ByRef Rect, x, y, w, h)
  397. {
  398.     VarSetCapacity(Rect, 16)
  399.     NumPut(x, Rect, 0, "uint"), NumPut(y, Rect, 4, "uint"), NumPut(w, Rect, 8, "uint"), NumPut(h, Rect, 12, "uint")
  400. }
  401. ;#####################################################################################
  402.  
  403. ; Function              CreateSizeF
  404. ; Description           Creates a SizeF object, containing an 2 values
  405. ;
  406. ; SizeF                 Name to call the SizeF object
  407. ; w                     w-value for the SizeF object
  408. ; h                     h-value for the SizeF object
  409. ;
  410. ; return                No Return value
  411.  
  412. CreateSizeF(ByRef SizeF, w, h)
  413. {
  414.    VarSetCapacity(SizeF, 8)
  415.    NumPut(w, SizeF, 0, "float"), NumPut(h, SizeF, 4, "float")    
  416. }
  417. ;#####################################################################################
  418.  
  419. ; Function              CreatePointF
  420. ; Description           Creates a SizeF object, containing an 2 values
  421. ;
  422. ; SizeF                 Name to call the SizeF object
  423. ; w                     w-value for the SizeF object
  424. ; h                     h-value for the SizeF object
  425. ;
  426. ; return                No Return value
  427.  
  428. CreatePointF(ByRef PointF, x, y)
  429. {
  430.    VarSetCapacity(PointF, 8)
  431.    NumPut(x, PointF, 0, "float"), NumPut(y, PointF, 4, "float")    
  432. }
  433. ;#####################################################################################
  434.  
  435. ; Function              CreateDIBSection
  436. ; Description           The CreateDIBSection function creates a DIB (Device Independent Bitmap) that applications can write to directly
  437. ;
  438. ; w                     width of the bitmap to create
  439. ; h                     height of the bitmap to create
  440. ; hdc                   a handle to the device context to use the palette from
  441. ; bpp                   bits per pixel (32 = ARGB)
  442. ; ppvBits               A pointer to a variable that receives a pointer to the location of the DIB bit values
  443. ;
  444. ; return                returns a DIB. A gdi bitmap
  445. ;
  446. ; notes                 ppvBits will receive the location of the pixels in the DIB
  447.  
  448. CreateDIBSection(w, h, hdc="", bpp=32, ByRef ppvBits=0)
  449. {
  450.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  451.    
  452.     hdc2 := hdc ? hdc : GetDC()
  453.     VarSetCapacity(bi, 40, 0)
  454.    
  455.     NumPut(w, bi, 4, "uint")
  456.     , NumPut(h, bi, 8, "uint")
  457.     , NumPut(40, bi, 0, "uint")
  458.     , NumPut(1, bi, 12, "ushort")
  459.     , NumPut(0, bi, 16, "uInt")
  460.     , NumPut(bpp, bi, 14, "ushort")
  461.    
  462.     hbm := DllCall("CreateDIBSection"
  463.                     , Ptr, hdc2
  464.                     , Ptr, &bi
  465.                     , "uint", 0
  466.                     , A_PtrSize ? "UPtr*" : "uint*", ppvBits
  467.                     , Ptr, 0
  468.                     , "uint", 0, Ptr)
  469.  
  470.     if !hdc
  471.         ReleaseDC(hdc2)
  472.     return hbm
  473. }
  474.  
  475. ;#####################################################################################
  476.  
  477. ; Function              PrintWindow
  478. ; Description           The PrintWindow function copies a visual window into the specified device context (DC), typically a printer DC
  479. ;
  480. ; hwnd                  A handle to the window that will be copied
  481. ; hdc                   A handle to the device context
  482. ; Flags                 Drawing options
  483. ;
  484. ; return                If the function succeeds, it returns a nonzero value
  485. ;
  486. ; PW_CLIENTONLY         = 1
  487.  
  488. PrintWindow(hwnd, hdc, Flags=0)
  489. {
  490.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  491.    
  492.     return DllCall("PrintWindow", Ptr, hwnd, Ptr, hdc, "uint", Flags)
  493. }
  494.  
  495. ;#####################################################################################
  496.  
  497. ; Function              DestroyIcon
  498. ; Description           Destroys an icon and frees any memory the icon occupied
  499. ;
  500. ; hIcon                 Handle to the icon to be destroyed. The icon must not be in use
  501. ;
  502. ; return                If the function succeeds, the return value is nonzero
  503.  
  504. DestroyIcon(hIcon)
  505. {
  506.     return DllCall("DestroyIcon", A_PtrSize ? "UPtr" : "UInt", hIcon)
  507. }
  508.  
  509. ;#####################################################################################
  510.  
  511. PaintDesktop(hdc)
  512. {
  513.     return DllCall("PaintDesktop", A_PtrSize ? "UPtr" : "UInt", hdc)
  514. }
  515.  
  516. ;#####################################################################################
  517.  
  518. CreateCompatibleBitmap(hdc, w, h)
  519. {
  520.     return DllCall("gdi32\CreateCompatibleBitmap", A_PtrSize ? "UPtr" : "UInt", hdc, "int", w, "int", h)
  521. }
  522.  
  523. ;#####################################################################################
  524.  
  525. ; Function              CreateCompatibleDC
  526. ; Description           This function creates a memory device context (DC) compatible with the specified device
  527. ;
  528. ; hdc                   Handle to an existing device context                   
  529. ;
  530. ; return                returns the handle to a device context or 0 on failure
  531. ;
  532. ; notes                 If this handle is 0 (by default), the function creates a memory device context compatible with the application's current screen
  533.  
  534. CreateCompatibleDC(hdc=0)
  535. {
  536.    return DllCall("CreateCompatibleDC", A_PtrSize ? "UPtr" : "UInt", hdc)
  537. }
  538.  
  539. ;#####################################################################################
  540.  
  541. ; Function              SelectObject
  542. ; Description           The SelectObject function selects an object into the specified device context (DC). The new object replaces the previous object of the same type
  543. ;
  544. ; hdc                   Handle to a DC
  545. ; hgdiobj               A handle to the object to be selected into the DC
  546. ;
  547. ; return                If the selected object is not a region and the function succeeds, the return value is a handle to the object being replaced
  548. ;
  549. ; notes                 The specified object must have been created by using one of the following functions
  550. ;                       Bitmap - CreateBitmap, CreateBitmapIndirect, CreateCompatibleBitmap, CreateDIBitmap, CreateDIBSection (A single bitmap cannot be selected into more than one DC at the same time)
  551. ;                       Brush - CreateBrushIndirect, CreateDIBPatternBrush, CreateDIBPatternBrushPt, CreateHatchBrush, CreatePatternBrush, CreateSolidBrush
  552. ;                       Font - CreateFont, CreateFontIndirect
  553. ;                       Pen - CreatePen, CreatePenIndirect
  554. ;                       Region - CombineRgn, CreateEllipticRgn, CreateEllipticRgnIndirect, CreatePolygonRgn, CreateRectRgn, CreateRectRgnIndirect
  555. ;
  556. ; notes                 If the selected object is a region and the function succeeds, the return value is one of the following value
  557. ;
  558. ; SIMPLEREGION          = 2 Region consists of a single rectangle
  559. ; COMPLEXREGION         = 3 Region consists of more than one rectangle
  560. ; NULLREGION            = 1 Region is empty
  561.  
  562. SelectObject(hdc, hgdiobj)
  563. {
  564.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  565.    
  566.     return DllCall("SelectObject", Ptr, hdc, Ptr, hgdiobj)
  567. }
  568.  
  569. ;#####################################################################################
  570.  
  571. ; Function              DeleteObject
  572. ; Description           This function deletes a logical pen, brush, font, bitmap, region, or palette, freeing all system resources associated with the object
  573. ;                       After the object is deleted, the specified handle is no longer valid
  574. ;
  575. ; hObject               Handle to a logical pen, brush, font, bitmap, region, or palette to delete
  576. ;
  577. ; return                Nonzero indicates success. Zero indicates that the specified handle is not valid or that the handle is currently selected into a device context
  578.  
  579. DeleteObject(hObject)
  580. {
  581.    return DllCall("DeleteObject", A_PtrSize ? "UPtr" : "UInt", hObject)
  582. }
  583.  
  584. ;#####################################################################################
  585.  
  586. ; Function              GetDC
  587. ; Description           This function retrieves a handle to a display device context (DC) for the client area of the specified window.
  588. ;                       The display device context can be used in subsequent graphics display interface (GDI) functions to draw in the client area of the window.
  589. ;
  590. ; hwnd                  Handle to the window whose device context is to be retrieved. If this value is NULL, GetDC retrieves the device context for the entire screen                  
  591. ;
  592. ; return                The handle the device context for the specified window's client area indicates success. NULL indicates failure
  593.  
  594. GetDC(hwnd=0)
  595. {
  596.     return DllCall("GetDC", A_PtrSize ? "UPtr" : "UInt", hwnd)
  597. }
  598.  
  599. ;#####################################################################################
  600.  
  601. ; DCX_CACHE = 0x2
  602. ; DCX_CLIPCHILDREN = 0x8
  603. ; DCX_CLIPSIBLINGS = 0x10
  604. ; DCX_EXCLUDERGN = 0x40
  605. ; DCX_EXCLUDEUPDATE = 0x100
  606. ; DCX_INTERSECTRGN = 0x80
  607. ; DCX_INTERSECTUPDATE = 0x200
  608. ; DCX_LOCKWINDOWUPDATE = 0x400
  609. ; DCX_NORECOMPUTE = 0x100000
  610. ; DCX_NORESETATTRS = 0x4
  611. ; DCX_PARENTCLIP = 0x20
  612. ; DCX_VALIDATE = 0x200000
  613. ; DCX_WINDOW = 0x1
  614.  
  615. GetDCEx(hwnd, flags=0, hrgnClip=0)
  616. {
  617.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  618.    
  619.     return DllCall("GetDCEx", Ptr, hwnd, Ptr, hrgnClip, "int", flags)
  620. }
  621.  
  622. ;#####################################################################################
  623.  
  624. ; Function              ReleaseDC
  625. ; Description           This function releases a device context (DC), freeing it for use by other applications. The effect of ReleaseDC depends on the type of device context
  626. ;
  627. ; hdc                   Handle to the device context to be released
  628. ; hwnd                  Handle to the window whose device context is to be released
  629. ;
  630. ; return                1 = released
  631. ;                       0 = not released
  632. ;
  633. ; notes                 The application must call the ReleaseDC function for each call to the GetWindowDC function and for each call to the GetDC function that retrieves a common device context
  634. ;                       An application cannot use the ReleaseDC function to release a device context that was created by calling the CreateDC function; instead, it must use the DeleteDC function.
  635.  
  636. ReleaseDC(hdc, hwnd=0)
  637. {
  638.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  639.    
  640.     return DllCall("ReleaseDC", Ptr, hwnd, Ptr, hdc)
  641. }
  642.  
  643. ;#####################################################################################
  644.  
  645. ; Function              DeleteDC
  646. ; Description           The DeleteDC function deletes the specified device context (DC)
  647. ;
  648. ; hdc                   A handle to the device context
  649. ;
  650. ; return                If the function succeeds, the return value is nonzero
  651. ;
  652. ; notes                 An application must not delete a DC whose handle was obtained by calling the GetDC function. Instead, it must call the ReleaseDC function to free the DC
  653.  
  654. DeleteDC(hdc)
  655. {
  656.    return DllCall("DeleteDC", A_PtrSize ? "UPtr" : "UInt", hdc)
  657. }
  658. ;#####################################################################################
  659.  
  660. ; Function              Gdip_LibraryVersion
  661. ; Description           Get the current library version
  662. ;
  663. ; return                the library version
  664. ;
  665. ; notes                 This is useful for non compiled programs to ensure that a person doesn't run an old version when testing your scripts
  666.  
  667. Gdip_LibraryVersion()
  668. {
  669.     return 1.45
  670. }
  671.  
  672. ;#####################################################################################
  673.  
  674. ; Function:             Gdip_BitmapFromBRA
  675. ; Description:          Gets a pointer to a gdi+ bitmap from a BRA file
  676. ;
  677. ; BRAFromMemIn          The variable for a BRA file read to memory
  678. ; File                  The name of the file, or its number that you would like (This depends on alternate parameter)
  679. ; Alternate             Changes whether the File parameter is the file name or its number
  680. ;
  681. ; return                If the function succeeds, the return value is a pointer to a gdi+ bitmap
  682. ;                       -1 = The BRA variable is empty
  683. ;                       -2 = The BRA has an incorrect header
  684. ;                       -3 = The BRA has information missing
  685. ;                       -4 = Could not find file inside the BRA
  686.  
  687. Gdip_BitmapFromBRA(ByRef BRAFromMemIn, File, Alternate=0)
  688. {
  689.     Static FName = "ObjRelease"
  690.    
  691.     if !BRAFromMemIn
  692.         return -1
  693.     Loop, Parse, BRAFromMemIn, `n
  694.     {
  695.         if (A_Index = 1)
  696.         {
  697.             StringSplit, Header, A_LoopField, |
  698.             if (Header0 != 4 || Header2 != "BRA!")
  699.                 return -2
  700.         }
  701.         else if (A_Index = 2)
  702.         {
  703.             StringSplit, Info, A_LoopField, |
  704.             if (Info0 != 3)
  705.                 return -3
  706.         }
  707.         else
  708.             break
  709.     }
  710.     if !Alternate
  711.         StringReplace, File, File, \, \\, All
  712.     RegExMatch(BRAFromMemIn, "mi`n)^" (Alternate ? File "\|.+?\|(\d+)\|(\d+)" : "\d+\|" File "\|(\d+)\|(\d+)") "$", FileInfo)
  713.     if !FileInfo
  714.         return -4
  715.    
  716.     hData := DllCall("GlobalAlloc", "uint", 2, Ptr, FileInfo2, Ptr)
  717.     pData := DllCall("GlobalLock", Ptr, hData, Ptr)
  718.     DllCall("RtlMoveMemory", Ptr, pData, Ptr, &BRAFromMemIn+Info2+FileInfo1, Ptr, FileInfo2)
  719.     DllCall("GlobalUnlock", Ptr, hData)
  720.     DllCall("ole32\CreateStreamOnHGlobal", Ptr, hData, "int", 1, A_PtrSize ? "UPtr*" : "UInt*", pStream)
  721.     DllCall("gdiplus\GdipCreateBitmapFromStream", Ptr, pStream, A_PtrSize ? "UPtr*" : "UInt*", pBitmap)
  722.     If (A_PtrSize)
  723.         %FName%(pStream)
  724.     Else
  725.         DllCall(NumGet(NumGet(1*pStream)+8), "uint", pStream)
  726.     return pBitmap
  727. }
  728.  
  729. ;#####################################################################################
  730.  
  731. ; Function              Gdip_DrawRectangle
  732. ; Description           This function uses a pen to draw the outline of a rectangle into the Graphics of a bitmap
  733. ;
  734. ; pGraphics             Pointer to the Graphics of a bitmap
  735. ; pPen                  Pointer to a pen
  736. ; x                     x-coordinate of the top left of the rectangle
  737. ; y                     y-coordinate of the top left of the rectangle
  738. ; w                     width of the rectanlge
  739. ; h                     height of the rectangle
  740. ;
  741. ; return                status enumeration. 0 = success
  742. ;
  743. ; notes                 as all coordinates are taken from the top left of each pixel, then the entire width/height should be specified as subtracting the pen width
  744.  
  745. Gdip_DrawRectangle(pGraphics, pPen, x, y, w, h)
  746. {
  747.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  748.    
  749.     return DllCall("gdiplus\GdipDrawRectangle", Ptr, pGraphics, Ptr, pPen, "float", x, "float", y, "float", w, "float", h)
  750. }
  751.  
  752. ;#####################################################################################
  753.  
  754. ; Function              Gdip_DrawRoundedRectangle
  755. ; Description           This function uses a pen to draw the outline of a rounded rectangle into the Graphics of a bitmap
  756. ;
  757. ; pGraphics             Pointer to the Graphics of a bitmap
  758. ; pPen                  Pointer to a pen
  759. ; x                     x-coordinate of the top left of the rounded rectangle
  760. ; y                     y-coordinate of the top left of the rounded rectangle
  761. ; w                     width of the rectanlge
  762. ; h                     height of the rectangle
  763. ; r                     radius of the rounded corners
  764. ;
  765. ; return                status enumeration. 0 = success
  766. ;
  767. ; notes                 as all coordinates are taken from the top left of each pixel, then the entire width/height should be specified as subtracting the pen width
  768.  
  769. Gdip_DrawRoundedRectangle(pGraphics, pPen, x, y, w, h, r)
  770. {
  771.     Gdip_SetClipRect(pGraphics, x-r, y-r, 2*r, 2*r, 4)
  772.     Gdip_SetClipRect(pGraphics, x+w-r, y-r, 2*r, 2*r, 4)
  773.     Gdip_SetClipRect(pGraphics, x-r, y+h-r, 2*r, 2*r, 4)
  774.     Gdip_SetClipRect(pGraphics, x+w-r, y+h-r, 2*r, 2*r, 4)
  775.     E := Gdip_DrawRectangle(pGraphics, pPen, x, y, w, h)
  776.     Gdip_ResetClip(pGraphics)
  777.     Gdip_SetClipRect(pGraphics, x-(2*r), y+r, w+(4*r), h-(2*r), 4)
  778.     Gdip_SetClipRect(pGraphics, x+r, y-(2*r), w-(2*r), h+(4*r), 4)
  779.     Gdip_DrawEllipse(pGraphics, pPen, x, y, 2*r, 2*r)
  780.     Gdip_DrawEllipse(pGraphics, pPen, x+w-(2*r), y, 2*r, 2*r)
  781.     Gdip_DrawEllipse(pGraphics, pPen, x, y+h-(2*r), 2*r, 2*r)
  782.     Gdip_DrawEllipse(pGraphics, pPen, x+w-(2*r), y+h-(2*r), 2*r, 2*r)
  783.     Gdip_ResetClip(pGraphics)
  784.     return E
  785. }
  786.  
  787. ;#####################################################################################
  788.  
  789. ; Function              Gdip_DrawEllipse
  790. ; Description           This function uses a pen to draw the outline of an ellipse into the Graphics of a bitmap
  791. ;
  792. ; pGraphics             Pointer to the Graphics of a bitmap
  793. ; pPen                  Pointer to a pen
  794. ; x                     x-coordinate of the top left of the rectangle the ellipse will be drawn into
  795. ; y                     y-coordinate of the top left of the rectangle the ellipse will be drawn into
  796. ; w                     width of the ellipse
  797. ; h                     height of the ellipse
  798. ;
  799. ; return                status enumeration. 0 = success
  800. ;
  801. ; notes                 as all coordinates are taken from the top left of each pixel, then the entire width/height should be specified as subtracting the pen width
  802.  
  803. Gdip_DrawEllipse(pGraphics, pPen, x, y, w, h)
  804. {
  805.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  806.    
  807.     return DllCall("gdiplus\GdipDrawEllipse", Ptr, pGraphics, Ptr, pPen, "float", x, "float", y, "float", w, "float", h)
  808. }
  809.  
  810. ;#####################################################################################
  811.  
  812. ; Function              Gdip_DrawBezier
  813. ; Description           This function uses a pen to draw the outline of a bezier (a weighted curve) into the Graphics of a bitmap
  814. ;
  815. ; pGraphics             Pointer to the Graphics of a bitmap
  816. ; pPen                  Pointer to a pen
  817. ; x1                    x-coordinate of the start of the bezier
  818. ; y1                    y-coordinate of the start of the bezier
  819. ; x2                    x-coordinate of the first arc of the bezier
  820. ; y2                    y-coordinate of the first arc of the bezier
  821. ; x3                    x-coordinate of the second arc of the bezier
  822. ; y3                    y-coordinate of the second arc of the bezier
  823. ; x4                    x-coordinate of the end of the bezier
  824. ; y4                    y-coordinate of the end of the bezier
  825. ;
  826. ; return                status enumeration. 0 = success
  827. ;
  828. ; notes                 as all coordinates are taken from the top left of each pixel, then the entire width/height should be specified as subtracting the pen width
  829.  
  830. Gdip_DrawBezier(pGraphics, pPen, x1, y1, x2, y2, x3, y3, x4, y4)
  831. {
  832.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  833.    
  834.     return DllCall("gdiplus\GdipDrawBezier"
  835.                     , Ptr, pgraphics
  836.                     , Ptr, pPen
  837.                     , "float", x1
  838.                     , "float", y1
  839.                     , "float", x2
  840.                     , "float", y2
  841.                     , "float", x3
  842.                     , "float", y3
  843.                     , "float", x4
  844.                     , "float", y4)
  845. }
  846.  
  847. ;#####################################################################################
  848.  
  849. ; Function              Gdip_DrawArc
  850. ; Description           This function uses a pen to draw the outline of an arc into the Graphics of a bitmap
  851. ;
  852. ; pGraphics             Pointer to the Graphics of a bitmap
  853. ; pPen                  Pointer to a pen
  854. ; x                     x-coordinate of the start of the arc
  855. ; y                     y-coordinate of the start of the arc
  856. ; w                     width of the arc
  857. ; h                     height of the arc
  858. ; StartAngle            specifies the angle between the x-axis and the starting point of the arc
  859. ; SweepAngle            specifies the angle between the starting and ending points of the arc
  860. ;
  861. ; return                status enumeration. 0 = success
  862. ;
  863. ; notes                 as all coordinates are taken from the top left of each pixel, then the entire width/height should be specified as subtracting the pen width
  864.  
  865. Gdip_DrawArc(pGraphics, pPen, x, y, w, h, StartAngle, SweepAngle)
  866. {
  867.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  868.    
  869.     return DllCall("gdiplus\GdipDrawArc"
  870.                     , Ptr, pGraphics
  871.                     , Ptr, pPen
  872.                     , "float", x
  873.                     , "float", y
  874.                     , "float", w
  875.                     , "float", h
  876.                     , "float", StartAngle
  877.                     , "float", SweepAngle)
  878. }
  879.  
  880. ;#####################################################################################
  881.  
  882. ; Function              Gdip_DrawPie
  883. ; Description           This function uses a pen to draw the outline of a pie into the Graphics of a bitmap
  884. ;
  885. ; pGraphics             Pointer to the Graphics of a bitmap
  886. ; pPen                  Pointer to a pen
  887. ; x                     x-coordinate of the start of the pie
  888. ; y                     y-coordinate of the start of the pie
  889. ; w                     width of the pie
  890. ; h                     height of the pie
  891. ; StartAngle            specifies the angle between the x-axis and the starting point of the pie
  892. ; SweepAngle            specifies the angle between the starting and ending points of the pie
  893. ;
  894. ; return                status enumeration. 0 = success
  895. ;
  896. ; notes                 as all coordinates are taken from the top left of each pixel, then the entire width/height should be specified as subtracting the pen width
  897.  
  898. Gdip_DrawPie(pGraphics, pPen, x, y, w, h, StartAngle, SweepAngle)
  899. {
  900.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  901.    
  902.     return DllCall("gdiplus\GdipDrawPie", Ptr, pGraphics, Ptr, pPen, "float", x, "float", y, "float", w, "float", h, "float", StartAngle, "float", SweepAngle)
  903. }
  904.  
  905. ;#####################################################################################
  906.  
  907. ; Function              Gdip_DrawLine
  908. ; Description           This function uses a pen to draw a line into the Graphics of a bitmap
  909. ;
  910. ; pGraphics             Pointer to the Graphics of a bitmap
  911. ; pPen                  Pointer to a pen
  912. ; x1                    x-coordinate of the start of the line
  913. ; y1                    y-coordinate of the start of the line
  914. ; x2                    x-coordinate of the end of the line
  915. ; y2                    y-coordinate of the end of the line
  916. ;
  917. ; return                status enumeration. 0 = success    
  918.  
  919. Gdip_DrawLine(pGraphics, pPen, x1, y1, x2, y2)
  920. {
  921.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  922.    
  923.     return DllCall("gdiplus\GdipDrawLine"
  924.                     , Ptr, pGraphics
  925.                     , Ptr, pPen
  926.                     , "float", x1
  927.                     , "float", y1
  928.                     , "float", x2
  929.                     , "float", y2)
  930. }
  931.  
  932. ;#####################################################################################
  933.  
  934. ; Function              Gdip_DrawLines
  935. ; Description           This function uses a pen to draw a series of joined lines into the Graphics of a bitmap
  936. ;
  937. ; pGraphics             Pointer to the Graphics of a bitmap
  938. ; pPen                  Pointer to a pen
  939. ; Points                the coordinates of all the points passed as x1,y1|x2,y2|x3,y3.....
  940. ;
  941. ; return                status enumeration. 0 = success            
  942.  
  943. Gdip_DrawLines(pGraphics, pPen, Points)
  944. {
  945.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  946.     StringSplit, Points, Points, |
  947.     VarSetCapacity(PointF, 8*Points0)  
  948.     Loop, %Points0%
  949.     {
  950.         StringSplit, Coord, Points%A_Index%, `,
  951.         NumPut(Coord1, PointF, 8*(A_Index-1), "float"), NumPut(Coord2, PointF, (8*(A_Index-1))+4, "float")
  952.     }
  953.     return DllCall("gdiplus\GdipDrawLines", Ptr, pGraphics, Ptr, pPen, Ptr, &PointF, "int", Points0)
  954. }
  955.  
  956. ;#####################################################################################
  957.  
  958. ; Function              Gdip_FillRectangle
  959. ; Description           This function uses a brush to fill a rectangle in the Graphics of a bitmap
  960. ;
  961. ; pGraphics             Pointer to the Graphics of a bitmap
  962. ; pBrush                Pointer to a brush
  963. ; x                     x-coordinate of the top left of the rectangle
  964. ; y                     y-coordinate of the top left of the rectangle
  965. ; w                     width of the rectanlge
  966. ; h                     height of the rectangle
  967. ;
  968. ; return                status enumeration. 0 = success
  969.  
  970. Gdip_FillRectangle(pGraphics, pBrush, x, y, w, h)
  971. {
  972.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  973.    
  974.     return DllCall("gdiplus\GdipFillRectangle"
  975.                     , Ptr, pGraphics
  976.                     , Ptr, pBrush
  977.                     , "float", x
  978.                     , "float", y
  979.                     , "float", w
  980.                     , "float", h)
  981. }
  982.  
  983. ;#####################################################################################
  984.  
  985. ; Function              Gdip_FillRoundedRectangle
  986. ; Description           This function uses a brush to fill a rounded rectangle in the Graphics of a bitmap
  987. ;
  988. ; pGraphics             Pointer to the Graphics of a bitmap
  989. ; pBrush                Pointer to a brush
  990. ; x                     x-coordinate of the top left of the rounded rectangle
  991. ; y                     y-coordinate of the top left of the rounded rectangle
  992. ; w                     width of the rectanlge
  993. ; h                     height of the rectangle
  994. ; r                     radius of the rounded corners
  995. ;
  996. ; return                status enumeration. 0 = success
  997.  
  998. Gdip_FillRoundedRectangle(pGraphics, pBrush, x, y, w, h, r)
  999. {
  1000.     Region := Gdip_GetClipRegion(pGraphics)
  1001.     Gdip_SetClipRect(pGraphics, x-r, y-r, 2*r, 2*r, 4)
  1002.     Gdip_SetClipRect(pGraphics, x+w-r, y-r, 2*r, 2*r, 4)
  1003.     Gdip_SetClipRect(pGraphics, x-r, y+h-r, 2*r, 2*r, 4)
  1004.     Gdip_SetClipRect(pGraphics, x+w-r, y+h-r, 2*r, 2*r, 4)
  1005.     E := Gdip_FillRectangle(pGraphics, pBrush, x, y, w, h)
  1006.     Gdip_SetClipRegion(pGraphics, Region, 0)
  1007.     Gdip_SetClipRect(pGraphics, x-(2*r), y+r, w+(4*r), h-(2*r), 4)
  1008.     Gdip_SetClipRect(pGraphics, x+r, y-(2*r), w-(2*r), h+(4*r), 4)
  1009.     Gdip_FillEllipse(pGraphics, pBrush, x, y, 2*r, 2*r)
  1010.     Gdip_FillEllipse(pGraphics, pBrush, x+w-(2*r), y, 2*r, 2*r)
  1011.     Gdip_FillEllipse(pGraphics, pBrush, x, y+h-(2*r), 2*r, 2*r)
  1012.     Gdip_FillEllipse(pGraphics, pBrush, x+w-(2*r), y+h-(2*r), 2*r, 2*r)
  1013.     Gdip_SetClipRegion(pGraphics, Region, 0)
  1014.     Gdip_DeleteRegion(Region)
  1015.     return E
  1016. }
  1017.  
  1018. ;#####################################################################################
  1019.  
  1020. ; Function              Gdip_FillPolygon
  1021. ; Description           This function uses a brush to fill a polygon in the Graphics of a bitmap
  1022. ;
  1023. ; pGraphics             Pointer to the Graphics of a bitmap
  1024. ; pBrush                Pointer to a brush
  1025. ; Points                the coordinates of all the points passed as x1,y1|x2,y2|x3,y3.....
  1026. ;
  1027. ; return                status enumeration. 0 = success
  1028. ;
  1029. ; notes                 Alternate will fill the polygon as a whole, wheras winding will fill each new "segment"
  1030. ; Alternate             = 0
  1031. ; Winding               = 1
  1032.  
  1033. Gdip_FillPolygon(pGraphics, pBrush, Points, FillMode=0)
  1034. {
  1035.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1036.    
  1037.     StringSplit, Points, Points, |
  1038.     VarSetCapacity(PointF, 8*Points0)  
  1039.     Loop, %Points0%
  1040.     {
  1041.         StringSplit, Coord, Points%A_Index%, `,
  1042.         NumPut(Coord1, PointF, 8*(A_Index-1), "float"), NumPut(Coord2, PointF, (8*(A_Index-1))+4, "float")
  1043.     }  
  1044.     return DllCall("gdiplus\GdipFillPolygon", Ptr, pGraphics, Ptr, pBrush, Ptr, &PointF, "int", Points0, "int", FillMode)
  1045. }
  1046.  
  1047. ;#####################################################################################
  1048.  
  1049. ; Function              Gdip_FillPie
  1050. ; Description           This function uses a brush to fill a pie in the Graphics of a bitmap
  1051. ;
  1052. ; pGraphics             Pointer to the Graphics of a bitmap
  1053. ; pBrush                Pointer to a brush
  1054. ; x                     x-coordinate of the top left of the pie
  1055. ; y                     y-coordinate of the top left of the pie
  1056. ; w                     width of the pie
  1057. ; h                     height of the pie
  1058. ; StartAngle            specifies the angle between the x-axis and the starting point of the pie
  1059. ; SweepAngle            specifies the angle between the starting and ending points of the pie
  1060. ;
  1061. ; return                status enumeration. 0 = success
  1062.  
  1063. Gdip_FillPie(pGraphics, pBrush, x, y, w, h, StartAngle, SweepAngle)
  1064. {
  1065.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1066.    
  1067.     return DllCall("gdiplus\GdipFillPie"
  1068.                     , Ptr, pGraphics
  1069.                     , Ptr, pBrush
  1070.                     , "float", x
  1071.                     , "float", y
  1072.                     , "float", w
  1073.                     , "float", h
  1074.                     , "float", StartAngle
  1075.                     , "float", SweepAngle)
  1076. }
  1077.  
  1078. ;#####################################################################################
  1079.  
  1080. ; Function              Gdip_FillEllipse
  1081. ; Description           This function uses a brush to fill an ellipse in the Graphics of a bitmap
  1082. ;
  1083. ; pGraphics             Pointer to the Graphics of a bitmap
  1084. ; pBrush                Pointer to a brush
  1085. ; x                     x-coordinate of the top left of the ellipse
  1086. ; y                     y-coordinate of the top left of the ellipse
  1087. ; w                     width of the ellipse
  1088. ; h                     height of the ellipse
  1089. ;
  1090. ; return                status enumeration. 0 = success
  1091.  
  1092. Gdip_FillEllipse(pGraphics, pBrush, x, y, w, h)
  1093. {
  1094.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1095.    
  1096.     return DllCall("gdiplus\GdipFillEllipse", Ptr, pGraphics, Ptr, pBrush, "float", x, "float", y, "float", w, "float", h)
  1097. }
  1098.  
  1099. ;#####################################################################################
  1100.  
  1101. ; Function              Gdip_FillRegion
  1102. ; Description           This function uses a brush to fill a region in the Graphics of a bitmap
  1103. ;
  1104. ; pGraphics             Pointer to the Graphics of a bitmap
  1105. ; pBrush                Pointer to a brush
  1106. ; Region                Pointer to a Region
  1107. ;
  1108. ; return                status enumeration. 0 = success
  1109. ;
  1110. ; notes                 You can create a region Gdip_CreateRegion() and then add to this
  1111.  
  1112. Gdip_FillRegion(pGraphics, pBrush, Region)
  1113. {
  1114.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1115.    
  1116.     return DllCall("gdiplus\GdipFillRegion", Ptr, pGraphics, Ptr, pBrush, Ptr, Region)
  1117. }
  1118.  
  1119. ;#####################################################################################
  1120.  
  1121. ; Function              Gdip_FillPath
  1122. ; Description           This function uses a brush to fill a path in the Graphics of a bitmap
  1123. ;
  1124. ; pGraphics             Pointer to the Graphics of a bitmap
  1125. ; pBrush                Pointer to a brush
  1126. ; Region                Pointer to a Path
  1127. ;
  1128. ; return                status enumeration. 0 = success
  1129.  
  1130. Gdip_FillPath(pGraphics, pBrush, Path)
  1131. {
  1132.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1133.    
  1134.     return DllCall("gdiplus\GdipFillPath", Ptr, pGraphics, Ptr, pBrush, Ptr, Path)
  1135. }
  1136.  
  1137. ;#####################################################################################
  1138.  
  1139. ; Function              Gdip_DrawImagePointsRect
  1140. ; Description           This function draws a bitmap into the Graphics of another bitmap and skews it
  1141. ;
  1142. ; pGraphics             Pointer to the Graphics of a bitmap
  1143. ; pBitmap               Pointer to a bitmap to be drawn
  1144. ; Points                Points passed as x1,y1|x2,y2|x3,y3 (3 points: top left, top right, bottom left) describing the drawing of the bitmap
  1145. ; sx                    x-coordinate of source upper-left corner
  1146. ; sy                    y-coordinate of source upper-left corner
  1147. ; sw                    width of source rectangle
  1148. ; sh                    height of source rectangle
  1149. ; Matrix                a matrix used to alter image attributes when drawing
  1150. ;
  1151. ; return                status enumeration. 0 = success
  1152. ;
  1153. ; notes                 if sx,sy,sw,sh are missed then the entire source bitmap will be used
  1154. ;                       Matrix can be omitted to just draw with no alteration to ARGB
  1155. ;                       Matrix may be passed as a digit from 0 - 1 to change just transparency
  1156. ;                       Matrix can be passed as a matrix with any delimiter
  1157.  
  1158. Gdip_DrawImagePointsRect(pGraphics, pBitmap, Points, sx="", sy="", sw="", sh="", Matrix=1)
  1159. {
  1160.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1161.    
  1162.     StringSplit, Points, Points, |
  1163.     VarSetCapacity(PointF, 8*Points0)  
  1164.     Loop, %Points0%
  1165.     {
  1166.         StringSplit, Coord, Points%A_Index%, `,
  1167.         NumPut(Coord1, PointF, 8*(A_Index-1), "float"), NumPut(Coord2, PointF, (8*(A_Index-1))+4, "float")
  1168.     }
  1169.  
  1170.     if (Matrix&1 = "")
  1171.         ImageAttr := Gdip_SetImageAttributesColorMatrix(Matrix)
  1172.     else if (Matrix != 1)
  1173.         ImageAttr := Gdip_SetImageAttributesColorMatrix("1|0|0|0|0|0|1|0|0|0|0|0|1|0|0|0|0|0|" Matrix "|0|0|0|0|0|1")
  1174.        
  1175.     if (sx = "" && sy = "" && sw = "" && sh = "")
  1176.     {
  1177.         sx := 0, sy := 0
  1178.         sw := Gdip_GetImageWidth(pBitmap)
  1179.         sh := Gdip_GetImageHeight(pBitmap)
  1180.     }
  1181.  
  1182.     E := DllCall("gdiplus\GdipDrawImagePointsRect"
  1183.                 , Ptr, pGraphics
  1184.                 , Ptr, pBitmap
  1185.                 , Ptr, &PointF
  1186.                 , "int", Points0
  1187.                 , "float", sx
  1188.                 , "float", sy
  1189.                 , "float", sw
  1190.                 , "float", sh
  1191.                 , "int", 2
  1192.                 , Ptr, ImageAttr
  1193.                 , Ptr, 0
  1194.                 , Ptr, 0)
  1195.     if ImageAttr
  1196.         Gdip_DisposeImageAttributes(ImageAttr)
  1197.     return E
  1198. }
  1199.  
  1200. ;#####################################################################################
  1201.  
  1202. ; Function              Gdip_DrawImage
  1203. ; Description           This function draws a bitmap into the Graphics of another bitmap
  1204. ;
  1205. ; pGraphics             Pointer to the Graphics of a bitmap
  1206. ; pBitmap               Pointer to a bitmap to be drawn
  1207. ; dx                    x-coord of destination upper-left corner
  1208. ; dy                    y-coord of destination upper-left corner
  1209. ; dw                    width of destination image
  1210. ; dh                    height of destination image
  1211. ; sx                    x-coordinate of source upper-left corner
  1212. ; sy                    y-coordinate of source upper-left corner
  1213. ; sw                    width of source image
  1214. ; sh                    height of source image
  1215. ; Matrix                a matrix used to alter image attributes when drawing
  1216. ;
  1217. ; return                status enumeration. 0 = success
  1218. ;
  1219. ; notes                 if sx,sy,sw,sh are missed then the entire source bitmap will be used
  1220. ;                       Gdip_DrawImage performs faster
  1221. ;                       Matrix can be omitted to just draw with no alteration to ARGB
  1222. ;                       Matrix may be passed as a digit from 0 - 1 to change just transparency
  1223. ;                       Matrix can be passed as a matrix with any delimiter. For example:
  1224. ;                       MatrixBright=
  1225. ;                       (
  1226. ;                       1.5     |0      |0      |0      |0
  1227. ;                       0       |1.5    |0      |0      |0
  1228. ;                       0       |0      |1.5    |0      |0
  1229. ;                       0       |0      |0      |1      |0
  1230. ;                       0.05    |0.05   |0.05   |0      |1
  1231. ;                       )
  1232. ;
  1233. ; notes                 MatrixBright = 1.5|0|0|0|0|0|1.5|0|0|0|0|0|1.5|0|0|0|0|0|1|0|0.05|0.05|0.05|0|1
  1234. ;                       MatrixGreyScale = 0.299|0.299|0.299|0|0|0.587|0.587|0.587|0|0|0.114|0.114|0.114|0|0|0|0|0|1|0|0|0|0|0|1
  1235. ;                       MatrixNegative = -1|0|0|0|0|0|-1|0|0|0|0|0|-1|0|0|0|0|0|1|0|0|0|0|0|1
  1236.  
  1237. Gdip_DrawImage(pGraphics, pBitmap, dx="", dy="", dw="", dh="", sx="", sy="", sw="", sh="", Matrix=1)
  1238. {
  1239.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1240.    
  1241.     if (Matrix&1 = "")
  1242.         ImageAttr := Gdip_SetImageAttributesColorMatrix(Matrix)
  1243.     else if (Matrix != 1)
  1244.         ImageAttr := Gdip_SetImageAttributesColorMatrix("1|0|0|0|0|0|1|0|0|0|0|0|1|0|0|0|0|0|" Matrix "|0|0|0|0|0|1")
  1245.  
  1246.     if (sx = "" && sy = "" && sw = "" && sh = "")
  1247.     {
  1248.         if (dx = "" && dy = "" && dw = "" && dh = "")
  1249.         {
  1250.             sx := dx := 0, sy := dy := 0
  1251.             sw := dw := Gdip_GetImageWidth(pBitmap)
  1252.             sh := dh := Gdip_GetImageHeight(pBitmap)
  1253.         }
  1254.         else
  1255.         {
  1256.             sx := sy := 0
  1257.             sw := Gdip_GetImageWidth(pBitmap)
  1258.             sh := Gdip_GetImageHeight(pBitmap)
  1259.         }
  1260.     }
  1261.  
  1262.     E := DllCall("gdiplus\GdipDrawImageRectRect"
  1263.                 , Ptr, pGraphics
  1264.                 , Ptr, pBitmap
  1265.                 , "float", dx
  1266.                 , "float", dy
  1267.                 , "float", dw
  1268.                 , "float", dh
  1269.                 , "float", sx
  1270.                 , "float", sy
  1271.                 , "float", sw
  1272.                 , "float", sh
  1273.                 , "int", 2
  1274.                 , Ptr, ImageAttr
  1275.                 , Ptr, 0
  1276.                 , Ptr, 0)
  1277.     if ImageAttr
  1278.         Gdip_DisposeImageAttributes(ImageAttr)
  1279.     return E
  1280. }
  1281.  
  1282. ;#####################################################################################
  1283.  
  1284. ; Function              Gdip_SetImageAttributesColorMatrix
  1285. ; Description           This function creates an image matrix ready for drawing
  1286. ;
  1287. ; Matrix                a matrix used to alter image attributes when drawing
  1288. ;                       passed with any delimeter
  1289. ;
  1290. ; return                returns an image matrix on sucess or 0 if it fails
  1291. ;
  1292. ; notes                 MatrixBright = 1.5|0|0|0|0|0|1.5|0|0|0|0|0|1.5|0|0|0|0|0|1|0|0.05|0.05|0.05|0|1
  1293. ;                       MatrixGreyScale = 0.299|0.299|0.299|0|0|0.587|0.587|0.587|0|0|0.114|0.114|0.114|0|0|0|0|0|1|0|0|0|0|0|1
  1294. ;                       MatrixNegative = -1|0|0|0|0|0|-1|0|0|0|0|0|-1|0|0|0|0|0|1|0|0|0|0|0|1
  1295.  
  1296. Gdip_SetImageAttributesColorMatrix(Matrix)
  1297. {
  1298.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1299.    
  1300.     VarSetCapacity(ColourMatrix, 100, 0)
  1301.     Matrix := RegExReplace(RegExReplace(Matrix, "^[^\d-\.]+([\d\.])", "$1", "", 1), "[^\d-\.]+", "|")
  1302.     StringSplit, Matrix, Matrix, |
  1303.     Loop, 25
  1304.     {
  1305.         Matrix := (Matrix%A_Index% != "") ? Matrix%A_Index% : Mod(A_Index-1, 6) ? 0 : 1
  1306.         NumPut(Matrix, ColourMatrix, (A_Index-1)*4, "float")
  1307.     }
  1308.     DllCall("gdiplus\GdipCreateImageAttributes", A_PtrSize ? "UPtr*" : "uint*", ImageAttr)
  1309.     DllCall("gdiplus\GdipSetImageAttributesColorMatrix", Ptr, ImageAttr, "int", 1, "int", 1, Ptr, &ColourMatrix, Ptr, 0, "int", 0)
  1310.     return ImageAttr
  1311. }
  1312.  
  1313. ;#####################################################################################
  1314.  
  1315. ; Function              Gdip_GraphicsFromImage
  1316. ; Description           This function gets the graphics for a bitmap used for drawing functions
  1317. ;
  1318. ; pBitmap               Pointer to a bitmap to get the pointer to its graphics
  1319. ;
  1320. ; return                returns a pointer to the graphics of a bitmap
  1321. ;
  1322. ; notes                 a bitmap can be drawn into the graphics of another bitmap
  1323.  
  1324. Gdip_GraphicsFromImage(pBitmap)
  1325. {
  1326.     DllCall("gdiplus\GdipGetImageGraphicsContext", A_PtrSize ? "UPtr" : "UInt", pBitmap, A_PtrSize ? "UPtr*" : "UInt*", pGraphics)
  1327.     return pGraphics
  1328. }
  1329.  
  1330. ;#####################################################################################
  1331.  
  1332. ; Function              Gdip_GraphicsFromHDC
  1333. ; Description           This function gets the graphics from the handle to a device context
  1334. ;
  1335. ; hdc                   This is the handle to the device context
  1336. ;
  1337. ; return                returns a pointer to the graphics of a bitmap
  1338. ;
  1339. ; notes                 You can draw a bitmap into the graphics of another bitmap
  1340.  
  1341. Gdip_GraphicsFromHDC(hdc)
  1342. {
  1343.     DllCall("gdiplus\GdipCreateFromHDC", A_PtrSize ? "UPtr" : "UInt", hdc, A_PtrSize ? "UPtr*" : "UInt*", pGraphics)
  1344.     return pGraphics
  1345. }
  1346.  
  1347. ;#####################################################################################
  1348.  
  1349. ; Function              Gdip_GetDC
  1350. ; Description           This function gets the device context of the passed Graphics
  1351. ;
  1352. ; hdc                   This is the handle to the device context
  1353. ;
  1354. ; return                returns the device context for the graphics of a bitmap
  1355.  
  1356. Gdip_GetDC(pGraphics)
  1357. {
  1358.     DllCall("gdiplus\GdipGetDC", A_PtrSize ? "UPtr" : "UInt", pGraphics, A_PtrSize ? "UPtr*" : "UInt*", hdc)
  1359.     return hdc
  1360. }
  1361.  
  1362. ;#####################################################################################
  1363.  
  1364. ; Function              Gdip_ReleaseDC
  1365. ; Description           This function releases a device context from use for further use
  1366. ;
  1367. ; pGraphics             Pointer to the graphics of a bitmap
  1368. ; hdc                   This is the handle to the device context
  1369. ;
  1370. ; return                status enumeration. 0 = success
  1371.  
  1372. Gdip_ReleaseDC(pGraphics, hdc)
  1373. {
  1374.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1375.    
  1376.     return DllCall("gdiplus\GdipReleaseDC", Ptr, pGraphics, Ptr, hdc)
  1377. }
  1378.  
  1379. ;#####################################################################################
  1380.  
  1381. ; Function              Gdip_GraphicsClear
  1382. ; Description           Clears the graphics of a bitmap ready for further drawing
  1383. ;
  1384. ; pGraphics             Pointer to the graphics of a bitmap
  1385. ; ARGB                  The colour to clear the graphics to
  1386. ;
  1387. ; return                status enumeration. 0 = success
  1388. ;
  1389. ; notes                 By default this will make the background invisible
  1390. ;                       Using clipping regions you can clear a particular area on the graphics rather than clearing the entire graphics
  1391.  
  1392. Gdip_GraphicsClear(pGraphics, ARGB=0x00ffffff)
  1393. {
  1394.     return DllCall("gdiplus\GdipGraphicsClear", A_PtrSize ? "UPtr" : "UInt", pGraphics, "int", ARGB)
  1395. }
  1396.  
  1397. ;#####################################################################################
  1398.  
  1399. ; Function              Gdip_BlurBitmap
  1400. ; Description           Gives a pointer to a blurred bitmap from a pointer to a bitmap
  1401. ;
  1402. ; pBitmap               Pointer to a bitmap to be blurred
  1403. ; Blur                  The Amount to blur a bitmap by from 1 (least blur) to 100 (most blur)
  1404. ;
  1405. ; return                If the function succeeds, the return value is a pointer to the new blurred bitmap
  1406. ;                       -1 = The blur parameter is outside the range 1-100
  1407. ;
  1408. ; notes                 This function will not dispose of the original bitmap
  1409.  
  1410. Gdip_BlurBitmap(pBitmap, Blur)
  1411. {
  1412.     if (Blur > 100) || (Blur < 1)
  1413.         return -1  
  1414.    
  1415.     sWidth := Gdip_GetImageWidth(pBitmap), sHeight := Gdip_GetImageHeight(pBitmap)
  1416.     dWidth := sWidth//Blur, dHeight := sHeight//Blur
  1417.  
  1418.     pBitmap1 := Gdip_CreateBitmap(dWidth, dHeight)
  1419.     G1 := Gdip_GraphicsFromImage(pBitmap1)
  1420.     Gdip_SetInterpolationMode(G1, 7)
  1421.     Gdip_DrawImage(G1, pBitmap, 0, 0, dWidth, dHeight, 0, 0, sWidth, sHeight)
  1422.  
  1423.     Gdip_DeleteGraphics(G1)
  1424.  
  1425.     pBitmap2 := Gdip_CreateBitmap(sWidth, sHeight)
  1426.     G2 := Gdip_GraphicsFromImage(pBitmap2)
  1427.     Gdip_SetInterpolationMode(G2, 7)
  1428.     Gdip_DrawImage(G2, pBitmap1, 0, 0, sWidth, sHeight, 0, 0, dWidth, dHeight)
  1429.  
  1430.     Gdip_DeleteGraphics(G2)
  1431.     Gdip_DisposeImage(pBitmap1)
  1432.     return pBitmap2
  1433. }
  1434.  
  1435. ;#####################################################################################
  1436.  
  1437. ; Function:             Gdip_SaveBitmapToFile
  1438. ; Description:          Saves a bitmap to a file in any supported format onto disk
  1439. ;  
  1440. ; pBitmap               Pointer to a bitmap
  1441. ; sOutput               The name of the file that the bitmap will be saved to. Supported extensions are: .BMP,.DIB,.RLE,.JPG,.JPEG,.JPE,.JFIF,.GIF,.TIF,.TIFF,.PNG
  1442. ; Quality               If saving as jpg (.JPG,.JPEG,.JPE,.JFIF) then quality can be 1-100 with default at maximum quality
  1443. ;
  1444. ; return                If the function succeeds, the return value is zero, otherwise:
  1445. ;                       -1 = Extension supplied is not a supported file format
  1446. ;                       -2 = Could not get a list of encoders on system
  1447. ;                       -3 = Could not find matching encoder for specified file format
  1448. ;                       -4 = Could not get WideChar name of output file
  1449. ;                       -5 = Could not save file to disk
  1450. ;
  1451. ; notes                 This function will use the extension supplied from the sOutput parameter to determine the output format
  1452.  
  1453. Gdip_SaveBitmapToFile(pBitmap, sOutput, Quality=75)
  1454. {
  1455.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1456.    
  1457.     SplitPath, sOutput,,, Extension
  1458.     if Extension not in BMP,DIB,RLE,JPG,JPEG,JPE,JFIF,GIF,TIF,TIFF,PNG
  1459.         return -1
  1460.     Extension := "." Extension
  1461.  
  1462.     DllCall("gdiplus\GdipGetImageEncodersSize", "uint*", nCount, "uint*", nSize)
  1463.     VarSetCapacity(ci, nSize)
  1464.     DllCall("gdiplus\GdipGetImageEncoders", "uint", nCount, "uint", nSize, Ptr, &ci)
  1465.     if !(nCount && nSize)
  1466.         return -2
  1467.    
  1468.     If (A_IsUnicode){
  1469.         StrGet_Name := "StrGet"
  1470.         Loop, %nCount%
  1471.         {
  1472.             sString := %StrGet_Name%(NumGet(ci, (idx := (48+7*A_PtrSize)*(A_Index-1))+32+3*A_PtrSize), "UTF-16")
  1473.             if !InStr(sString, "*" Extension)
  1474.                 continue
  1475.            
  1476.             pCodec := &ci+idx
  1477.             break
  1478.         }
  1479.     } else {
  1480.         Loop, %nCount%
  1481.         {
  1482.             Location := NumGet(ci, 76*(A_Index-1)+44)
  1483.             nSize := DllCall("WideCharToMultiByte", "uint", 0, "uint", 0, "uint", Location, "int", -1, "uint", 0, "int",  0, "uint", 0, "uint", 0)
  1484.             VarSetCapacity(sString, nSize)
  1485.             DllCall("WideCharToMultiByte", "uint", 0, "uint", 0, "uint", Location, "int", -1, "str", sString, "int", nSize, "uint", 0, "uint", 0)
  1486.             if !InStr(sString, "*" Extension)
  1487.                 continue
  1488.            
  1489.             pCodec := &ci+76*(A_Index-1)
  1490.             break
  1491.         }
  1492.     }
  1493.    
  1494.     if !pCodec
  1495.         return -3
  1496.  
  1497.     if (Quality != 75)
  1498.     {
  1499.         Quality := (Quality < 0) ? 0 : (Quality > 100) ? 100 : Quality
  1500.         if Extension in .JPG,.JPEG,.JPE,.JFIF
  1501.         {
  1502.             DllCall("gdiplus\GdipGetEncoderParameterListSize", Ptr, pBitmap, Ptr, pCodec, "uint*", nSize)
  1503.             VarSetCapacity(EncoderParameters, nSize, 0)
  1504.             DllCall("gdiplus\GdipGetEncoderParameterList", Ptr, pBitmap, Ptr, pCodec, "uint", nSize, Ptr, &EncoderParameters)
  1505.             Loop, % NumGet(EncoderParameters, "UInt")      ;%
  1506.             {
  1507.                 elem := (24+(A_PtrSize ? A_PtrSize : 4))*(A_Index-1) + 4 + (pad := A_PtrSize = 8 ? 4 : 0)
  1508.                 if (NumGet(EncoderParameters, elem+16, "UInt") = 1) && (NumGet(EncoderParameters, elem+20, "UInt") = 6)
  1509.                 {
  1510.                     p := elem+&EncoderParameters-pad-4
  1511.                     NumPut(Quality, NumGet(NumPut(4, NumPut(1, p+0)+20, "UInt")), "UInt")
  1512.                     break
  1513.                 }
  1514.             }      
  1515.         }
  1516.     }
  1517.  
  1518.     if (!A_IsUnicode)
  1519.     {
  1520.         nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sOutput, "int", -1, Ptr, 0, "int", 0)
  1521.         VarSetCapacity(wOutput, nSize*2)
  1522.         DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sOutput, "int", -1, Ptr, &wOutput, "int", nSize)
  1523.         VarSetCapacity(wOutput, -1)
  1524.         if !VarSetCapacity(wOutput)
  1525.             return -4
  1526.         E := DllCall("gdiplus\GdipSaveImageToFile", Ptr, pBitmap, Ptr, &wOutput, Ptr, pCodec, "uint", p ? p : 0)
  1527.     }
  1528.     else
  1529.         E := DllCall("gdiplus\GdipSaveImageToFile", Ptr, pBitmap, Ptr, &sOutput, Ptr, pCodec, "uint", p ? p : 0)
  1530.     return E ? -5 : 0
  1531. }
  1532.  
  1533. ;#####################################################################################
  1534.  
  1535. ; Function              Gdip_GetPixel
  1536. ; Description           Gets the ARGB of a pixel in a bitmap
  1537. ;
  1538. ; pBitmap               Pointer to a bitmap
  1539. ; x                     x-coordinate of the pixel
  1540. ; y                     y-coordinate of the pixel
  1541. ;
  1542. ; return                Returns the ARGB value of the pixel
  1543.  
  1544. Gdip_GetPixel(pBitmap, x, y)
  1545. {
  1546.     DllCall("gdiplus\GdipBitmapGetPixel", A_PtrSize ? "UPtr" : "UInt", pBitmap, "int", x, "int", y, "uint*", ARGB)
  1547.     return ARGB
  1548. }
  1549.  
  1550. ;#####################################################################################
  1551.  
  1552. ; Function              Gdip_SetPixel
  1553. ; Description           Sets the ARGB of a pixel in a bitmap
  1554. ;
  1555. ; pBitmap               Pointer to a bitmap
  1556. ; x                     x-coordinate of the pixel
  1557. ; y                     y-coordinate of the pixel
  1558. ;
  1559. ; return                status enumeration. 0 = success
  1560.  
  1561. Gdip_SetPixel(pBitmap, x, y, ARGB)
  1562. {
  1563.    return DllCall("gdiplus\GdipBitmapSetPixel", A_PtrSize ? "UPtr" : "UInt", pBitmap, "int", x, "int", y, "int", ARGB)
  1564. }
  1565.  
  1566. ;#####################################################################################
  1567.  
  1568. ; Function              Gdip_GetImageWidth
  1569. ; Description           Gives the width of a bitmap
  1570. ;
  1571. ; pBitmap               Pointer to a bitmap
  1572. ;
  1573. ; return                Returns the width in pixels of the supplied bitmap
  1574.  
  1575. Gdip_GetImageWidth(pBitmap)
  1576. {
  1577.    DllCall("gdiplus\GdipGetImageWidth", A_PtrSize ? "UPtr" : "UInt", pBitmap, "uint*", Width)
  1578.    return Width
  1579. }
  1580.  
  1581. ;#####################################################################################
  1582.  
  1583. ; Function              Gdip_GetImageHeight
  1584. ; Description           Gives the height of a bitmap
  1585. ;
  1586. ; pBitmap               Pointer to a bitmap
  1587. ;
  1588. ; return                Returns the height in pixels of the supplied bitmap
  1589.  
  1590. Gdip_GetImageHeight(pBitmap)
  1591. {
  1592.    DllCall("gdiplus\GdipGetImageHeight", A_PtrSize ? "UPtr" : "UInt", pBitmap, "uint*", Height)
  1593.    return Height
  1594. }
  1595.  
  1596. ;#####################################################################################
  1597.  
  1598. ; Function              Gdip_GetDimensions
  1599. ; Description           Gives the width and height of a bitmap
  1600. ;
  1601. ; pBitmap               Pointer to a bitmap
  1602. ; Width                 ByRef variable. This variable will be set to the width of the bitmap
  1603. ; Height                ByRef variable. This variable will be set to the height of the bitmap
  1604. ;
  1605. ; return                No return value
  1606. ;                       Gdip_GetDimensions(pBitmap, ThisWidth, ThisHeight) will set ThisWidth to the width and ThisHeight to the height
  1607.  
  1608. Gdip_GetImageDimensions(pBitmap, ByRef Width, ByRef Height)
  1609. {
  1610.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1611.     DllCall("gdiplus\GdipGetImageWidth", Ptr, pBitmap, "uint*", Width)
  1612.     DllCall("gdiplus\GdipGetImageHeight", Ptr, pBitmap, "uint*", Height)
  1613. }
  1614.  
  1615. ;#####################################################################################
  1616.  
  1617. Gdip_GetDimensions(pBitmap, ByRef Width, ByRef Height)
  1618. {
  1619.     Gdip_GetImageDimensions(pBitmap, Width, Height)
  1620. }
  1621.  
  1622. ;#####################################################################################
  1623.  
  1624. Gdip_GetImagePixelFormat(pBitmap)
  1625. {
  1626.     DllCall("gdiplus\GdipGetImagePixelFormat", A_PtrSize ? "UPtr" : "UInt", pBitmap, A_PtrSize ? "UPtr*" : "UInt*", Format)
  1627.     return Format
  1628. }
  1629.  
  1630. ;#####################################################################################
  1631.  
  1632. ; Function              Gdip_GetDpiX
  1633. ; Description           Gives the horizontal dots per inch of the graphics of a bitmap
  1634. ;
  1635. ; pBitmap               Pointer to a bitmap
  1636. ; Width                 ByRef variable. This variable will be set to the width of the bitmap
  1637. ; Height                ByRef variable. This variable will be set to the height of the bitmap
  1638. ;
  1639. ; return                No return value
  1640. ;                       Gdip_GetDimensions(pBitmap, ThisWidth, ThisHeight) will set ThisWidth to the width and ThisHeight to the height
  1641.  
  1642. Gdip_GetDpiX(pGraphics)
  1643. {
  1644.     DllCall("gdiplus\GdipGetDpiX", A_PtrSize ? "UPtr" : "uint", pGraphics, "float*", dpix)
  1645.     return Round(dpix)
  1646. }
  1647.  
  1648. ;#####################################################################################
  1649.  
  1650. Gdip_GetDpiY(pGraphics)
  1651. {
  1652.     DllCall("gdiplus\GdipGetDpiY", A_PtrSize ? "UPtr" : "uint", pGraphics, "float*", dpiy)
  1653.     return Round(dpiy)
  1654. }
  1655.  
  1656. ;#####################################################################################
  1657.  
  1658. Gdip_GetImageHorizontalResolution(pBitmap)
  1659. {
  1660.     DllCall("gdiplus\GdipGetImageHorizontalResolution", A_PtrSize ? "UPtr" : "uint", pBitmap, "float*", dpix)
  1661.     return Round(dpix)
  1662. }
  1663.  
  1664. ;#####################################################################################
  1665.  
  1666. Gdip_GetImageVerticalResolution(pBitmap)
  1667. {
  1668.     DllCall("gdiplus\GdipGetImageVerticalResolution", A_PtrSize ? "UPtr" : "uint", pBitmap, "float*", dpiy)
  1669.     return Round(dpiy)
  1670. }
  1671.  
  1672. ;#####################################################################################
  1673.  
  1674. Gdip_BitmapSetResolution(pBitmap, dpix, dpiy)
  1675. {
  1676.     return DllCall("gdiplus\GdipBitmapSetResolution", A_PtrSize ? "UPtr" : "uint", pBitmap, "float", dpix, "float", dpiy)
  1677. }
  1678.  
  1679. ;#####################################################################################
  1680.  
  1681. Gdip_CreateBitmapFromFile(sFile, IconNumber=1, IconSize="")
  1682. {
  1683.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1684.     , PtrA := A_PtrSize ? "UPtr*" : "UInt*"
  1685.    
  1686.     SplitPath, sFile,,, ext
  1687.     if ext in exe,dll
  1688.     {
  1689.         Sizes := IconSize ? IconSize : 256 "|" 128 "|" 64 "|" 48 "|" 32 "|" 16
  1690.         BufSize := 16 + (2*(A_PtrSize ? A_PtrSize : 4))
  1691.        
  1692.         VarSetCapacity(buf, BufSize, 0)
  1693.         Loop, Parse, Sizes, |
  1694.         {
  1695.             DllCall("PrivateExtractIcons", "str", sFile, "int", IconNumber-1, "int", A_LoopField, "int", A_LoopField, PtrA, hIcon, PtrA, 0, "uint", 1, "uint", 0)
  1696.            
  1697.             if !hIcon
  1698.                 continue
  1699.  
  1700.             if !DllCall("GetIconInfo", Ptr, hIcon, Ptr, &buf)
  1701.             {
  1702.                 DestroyIcon(hIcon)
  1703.                 continue
  1704.             }
  1705.            
  1706.             hbmMask  := NumGet(buf, 12 + ((A_PtrSize ? A_PtrSize : 4) - 4))
  1707.             hbmColor := NumGet(buf, 12 + ((A_PtrSize ? A_PtrSize : 4) - 4) + (A_PtrSize ? A_PtrSize : 4))
  1708.             if !(hbmColor && DllCall("GetObject", Ptr, hbmColor, "int", BufSize, Ptr, &buf))
  1709.             {
  1710.                 DestroyIcon(hIcon)
  1711.                 continue
  1712.             }
  1713.             break
  1714.         }
  1715.         if !hIcon
  1716.             return -1
  1717.  
  1718.         Width := NumGet(buf, 4, "int"), Height := NumGet(buf, 8, "int")
  1719.         hbm := CreateDIBSection(Width, -Height), hdc := CreateCompatibleDC(), obm := SelectObject(hdc, hbm)
  1720.         if !DllCall("DrawIconEx", Ptr, hdc, "int", 0, "int", 0, Ptr, hIcon, "uint", Width, "uint", Height, "uint", 0, Ptr, 0, "uint", 3)
  1721.         {
  1722.             DestroyIcon(hIcon)
  1723.             return -2
  1724.         }
  1725.        
  1726.         VarSetCapacity(dib, 104)
  1727.         DllCall("GetObject", Ptr, hbm, "int", A_PtrSize = 8 ? 104 : 84, Ptr, &dib) ; sizeof(DIBSECTION) = 76+2*(A_PtrSize=8?4:0)+2*A_PtrSize
  1728.         Stride := NumGet(dib, 12, "Int"), Bits := NumGet(dib, 20 + (A_PtrSize = 8 ? 4 : 0)) ; padding
  1729.         DllCall("gdiplus\GdipCreateBitmapFromScan0", "int", Width, "int", Height, "int", Stride, "int", 0x26200A, Ptr, Bits, PtrA, pBitmapOld)
  1730.         pBitmap := Gdip_CreateBitmap(Width, Height)
  1731.         G := Gdip_GraphicsFromImage(pBitmap)
  1732.         , Gdip_DrawImage(G, pBitmapOld, 0, 0, Width, Height, 0, 0, Width, Height)
  1733.         SelectObject(hdc, obm), DeleteObject(hbm), DeleteDC(hdc)
  1734.         Gdip_DeleteGraphics(G), Gdip_DisposeImage(pBitmapOld)
  1735.         DestroyIcon(hIcon)
  1736.     }
  1737.     else
  1738.     {
  1739.         if (!A_IsUnicode)
  1740.         {
  1741.             VarSetCapacity(wFile, 1024)
  1742.             DllCall("kernel32\MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sFile, "int", -1, Ptr, &wFile, "int", 512)
  1743.             DllCall("gdiplus\GdipCreateBitmapFromFile", Ptr, &wFile, PtrA, pBitmap)
  1744.         }
  1745.         else
  1746.             DllCall("gdiplus\GdipCreateBitmapFromFile", Ptr, &sFile, PtrA, pBitmap)
  1747.     }
  1748.    
  1749.     return pBitmap
  1750. }
  1751.  
  1752. ;#####################################################################################
  1753.  
  1754. Gdip_CreateBitmapFromHBITMAP(hBitmap, Palette=0)
  1755. {
  1756.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1757.    
  1758.     DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", Ptr, hBitmap, Ptr, Palette, A_PtrSize ? "UPtr*" : "uint*", pBitmap)
  1759.     return pBitmap
  1760. }
  1761.  
  1762. ;#####################################################################################
  1763.  
  1764. Gdip_CreateHBITMAPFromBitmap(pBitmap, Background=0xffffffff)
  1765. {
  1766.     DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", A_PtrSize ? "UPtr" : "UInt", pBitmap, A_PtrSize ? "UPtr*" : "uint*", hbm, "int", Background)
  1767.     return hbm
  1768. }
  1769.  
  1770. ;#####################################################################################
  1771.  
  1772. Gdip_CreateBitmapFromHICON(hIcon)
  1773. {
  1774.     DllCall("gdiplus\GdipCreateBitmapFromHICON", A_PtrSize ? "UPtr" : "UInt", hIcon, A_PtrSize ? "UPtr*" : "uint*", pBitmap)
  1775.     return pBitmap
  1776. }
  1777.  
  1778. ;#####################################################################################
  1779.  
  1780. Gdip_CreateHICONFromBitmap(pBitmap)
  1781. {
  1782.     DllCall("gdiplus\GdipCreateHICONFromBitmap", A_PtrSize ? "UPtr" : "UInt", pBitmap, A_PtrSize ? "UPtr*" : "uint*", hIcon)
  1783.     return hIcon
  1784. }
  1785.  
  1786. ;#####################################################################################
  1787.  
  1788. Gdip_CreateBitmap(Width, Height, Format=0x26200A)
  1789. {
  1790.     DllCall("gdiplus\GdipCreateBitmapFromScan0", "int", Width, "int", Height, "int", 0, "int", Format, A_PtrSize ? "UPtr" : "UInt", 0, A_PtrSize ? "UPtr*" : "uint*", pBitmap)
  1791.     Return pBitmap
  1792. }
  1793.  
  1794. ;#####################################################################################
  1795.  
  1796. Gdip_CreateBitmapFromClipboard()
  1797. {
  1798.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1799.    
  1800.     if !DllCall("OpenClipboard", Ptr, 0)
  1801.         return -1
  1802.     if !DllCall("IsClipboardFormatAvailable", "uint", 8)
  1803.         return -2
  1804.     if !hBitmap := DllCall("GetClipboardData", "uint", 2, Ptr)
  1805.         return -3
  1806.     if !pBitmap := Gdip_CreateBitmapFromHBITMAP(hBitmap)
  1807.         return -4
  1808.     if !DllCall("CloseClipboard")
  1809.         return -5
  1810.     DeleteObject(hBitmap)
  1811.     return pBitmap
  1812. }
  1813.  
  1814. ;#####################################################################################
  1815.  
  1816. Gdip_SetBitmapToClipboard(pBitmap)
  1817. {
  1818.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1819.     off1 := A_PtrSize = 8 ? 52 : 44, off2 := A_PtrSize = 8 ? 32 : 24
  1820.    
  1821.     hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
  1822.     DllCall("GetObject", Ptr, hBitmap, "int", VarSetCapacity(oi, A_PtrSize = 8 ? 96 : 84, 0), Ptr, &oi)
  1823.     hdib := DllCall("GlobalAlloc", "uint", 2, Ptr, off1+NumGet(oi, off1, "UInt")-4, Ptr)
  1824.     pdib := DllCall("GlobalLock", Ptr, hdib, Ptr)
  1825.     DllCall("RtlMoveMemory", Ptr, pdib, "uint", &oi+off2, Ptr, 40)
  1826.     DllCall("RtlMoveMemory", Ptr, pdib+40, Ptr, NumGet(oi, off2 - (A_PtrSize ? A_PtrSize : 4)), Ptr, NumGet(oi, off1, "UInt"))
  1827.     DllCall("GlobalUnlock", Ptr, hdib)
  1828.     DllCall("DeleteObject", Ptr, hBitmap)
  1829.     DllCall("OpenClipboard", Ptr, 0)
  1830.     DllCall("EmptyClipboard")
  1831.     DllCall("SetClipboardData", "uint", 8, Ptr, hdib)
  1832.     DllCall("CloseClipboard")
  1833. }
  1834.  
  1835. ;#####################################################################################
  1836.  
  1837. Gdip_CloneBitmapArea(pBitmap, x, y, w, h, Format=0x26200A)
  1838. {
  1839.     DllCall("gdiplus\GdipCloneBitmapArea"
  1840.                     , "float", x
  1841.                     , "float", y
  1842.                     , "float", w
  1843.                     , "float", h
  1844.                     , "int", Format
  1845.                     , A_PtrSize ? "UPtr" : "UInt", pBitmap
  1846.                     , A_PtrSize ? "UPtr*" : "UInt*", pBitmapDest)
  1847.     return pBitmapDest
  1848. }
  1849.  
  1850. ;#####################################################################################
  1851. ; Create resources
  1852. ;#####################################################################################
  1853.  
  1854. Gdip_CreatePen(ARGB, w)
  1855. {
  1856.    DllCall("gdiplus\GdipCreatePen1", "UInt", ARGB, "float", w, "int", 2, A_PtrSize ? "UPtr*" : "UInt*", pPen)
  1857.    return pPen
  1858. }
  1859.  
  1860. ;#####################################################################################
  1861.  
  1862. Gdip_CreatePenFromBrush(pBrush, w)
  1863. {
  1864.     DllCall("gdiplus\GdipCreatePen2", A_PtrSize ? "UPtr" : "UInt", pBrush, "float", w, "int", 2, A_PtrSize ? "UPtr*" : "UInt*", pPen)
  1865.     return pPen
  1866. }
  1867.  
  1868. ;#####################################################################################
  1869.  
  1870. Gdip_BrushCreateSolid(ARGB=0xff000000)
  1871. {
  1872.     DllCall("gdiplus\GdipCreateSolidFill", "UInt", ARGB, A_PtrSize ? "UPtr*" : "UInt*", pBrush)
  1873.     return pBrush
  1874. }
  1875.  
  1876. ;#####################################################################################
  1877.  
  1878. ; HatchStyleHorizontal = 0
  1879. ; HatchStyleVertical = 1
  1880. ; HatchStyleForwardDiagonal = 2
  1881. ; HatchStyleBackwardDiagonal = 3
  1882. ; HatchStyleCross = 4
  1883. ; HatchStyleDiagonalCross = 5
  1884. ; HatchStyle05Percent = 6
  1885. ; HatchStyle10Percent = 7
  1886. ; HatchStyle20Percent = 8
  1887. ; HatchStyle25Percent = 9
  1888. ; HatchStyle30Percent = 10
  1889. ; HatchStyle40Percent = 11
  1890. ; HatchStyle50Percent = 12
  1891. ; HatchStyle60Percent = 13
  1892. ; HatchStyle70Percent = 14
  1893. ; HatchStyle75Percent = 15
  1894. ; HatchStyle80Percent = 16
  1895. ; HatchStyle90Percent = 17
  1896. ; HatchStyleLightDownwardDiagonal = 18
  1897. ; HatchStyleLightUpwardDiagonal = 19
  1898. ; HatchStyleDarkDownwardDiagonal = 20
  1899. ; HatchStyleDarkUpwardDiagonal = 21
  1900. ; HatchStyleWideDownwardDiagonal = 22
  1901. ; HatchStyleWideUpwardDiagonal = 23
  1902. ; HatchStyleLightVertical = 24
  1903. ; HatchStyleLightHorizontal = 25
  1904. ; HatchStyleNarrowVertical = 26
  1905. ; HatchStyleNarrowHorizontal = 27
  1906. ; HatchStyleDarkVertical = 28
  1907. ; HatchStyleDarkHorizontal = 29
  1908. ; HatchStyleDashedDownwardDiagonal = 30
  1909. ; HatchStyleDashedUpwardDiagonal = 31
  1910. ; HatchStyleDashedHorizontal = 32
  1911. ; HatchStyleDashedVertical = 33
  1912. ; HatchStyleSmallConfetti = 34
  1913. ; HatchStyleLargeConfetti = 35
  1914. ; HatchStyleZigZag = 36
  1915. ; HatchStyleWave = 37
  1916. ; HatchStyleDiagonalBrick = 38
  1917. ; HatchStyleHorizontalBrick = 39
  1918. ; HatchStyleWeave = 40
  1919. ; HatchStylePlaid = 41
  1920. ; HatchStyleDivot = 42
  1921. ; HatchStyleDottedGrid = 43
  1922. ; HatchStyleDottedDiamond = 44
  1923. ; HatchStyleShingle = 45
  1924. ; HatchStyleTrellis = 46
  1925. ; HatchStyleSphere = 47
  1926. ; HatchStyleSmallGrid = 48
  1927. ; HatchStyleSmallCheckerBoard = 49
  1928. ; HatchStyleLargeCheckerBoard = 50
  1929. ; HatchStyleOutlinedDiamond = 51
  1930. ; HatchStyleSolidDiamond = 52
  1931. ; HatchStyleTotal = 53
  1932. Gdip_BrushCreateHatch(ARGBfront, ARGBback, HatchStyle=0)
  1933. {
  1934.     DllCall("gdiplus\GdipCreateHatchBrush", "int", HatchStyle, "UInt", ARGBfront, "UInt", ARGBback, A_PtrSize ? "UPtr*" : "UInt*", pBrush)
  1935.     return pBrush
  1936. }
  1937.  
  1938. ;#####################################################################################
  1939.  
  1940. Gdip_CreateTextureBrush(pBitmap, WrapMode=1, x=0, y=0, w="", h="")
  1941. {
  1942.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1943.     , PtrA := A_PtrSize ? "UPtr*" : "UInt*"
  1944.    
  1945.     if !(w && h)
  1946.         DllCall("gdiplus\GdipCreateTexture", Ptr, pBitmap, "int", WrapMode, PtrA, pBrush)
  1947.     else
  1948.         DllCall("gdiplus\GdipCreateTexture2", Ptr, pBitmap, "int", WrapMode, "float", x, "float", y, "float", w, "float", h, PtrA, pBrush)
  1949.     return pBrush
  1950. }
  1951.  
  1952. ;#####################################################################################
  1953.  
  1954. ; WrapModeTile = 0
  1955. ; WrapModeTileFlipX = 1
  1956. ; WrapModeTileFlipY = 2
  1957. ; WrapModeTileFlipXY = 3
  1958. ; WrapModeClamp = 4
  1959. Gdip_CreateLineBrush(x1, y1, x2, y2, ARGB1, ARGB2, WrapMode=1)
  1960. {
  1961.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  1962.    
  1963.     CreatePointF(PointF1, x1, y1), CreatePointF(PointF2, x2, y2)
  1964.     DllCall("gdiplus\GdipCreateLineBrush", Ptr, &PointF1, Ptr, &PointF2, "Uint", ARGB1, "Uint", ARGB2, "int", WrapMode, A_PtrSize ? "UPtr*" : "UInt*", LGpBrush)
  1965.     return LGpBrush
  1966. }
  1967.  
  1968. ;#####################################################################################
  1969.  
  1970. ; LinearGradientModeHorizontal = 0
  1971. ; LinearGradientModeVertical = 1
  1972. ; LinearGradientModeForwardDiagonal = 2
  1973. ; LinearGradientModeBackwardDiagonal = 3
  1974. Gdip_CreateLineBrushFromRect(x, y, w, h, ARGB1, ARGB2, LinearGradientMode=1, WrapMode=1)
  1975. {
  1976.     CreateRectF(RectF, x, y, w, h)
  1977.     DllCall("gdiplus\GdipCreateLineBrushFromRect", A_PtrSize ? "UPtr" : "UInt", &RectF, "int", ARGB1, "int", ARGB2, "int", LinearGradientMode, "int", WrapMode, A_PtrSize ? "UPtr*" : "UInt*", LGpBrush)
  1978.     return LGpBrush
  1979. }
  1980.  
  1981. ;#####################################################################################
  1982.  
  1983. Gdip_CloneBrush(pBrush)
  1984. {
  1985.     DllCall("gdiplus\GdipCloneBrush", A_PtrSize ? "UPtr" : "UInt", pBrush, A_PtrSize ? "UPtr*" : "UInt*", pBrushClone)
  1986.     return pBrushClone
  1987. }
  1988.  
  1989. ;#####################################################################################
  1990. ; Delete resources
  1991. ;#####################################################################################
  1992.  
  1993. Gdip_DeletePen(pPen)
  1994. {
  1995.    return DllCall("gdiplus\GdipDeletePen", A_PtrSize ? "UPtr" : "UInt", pPen)
  1996. }
  1997.  
  1998. ;#####################################################################################
  1999.  
  2000. Gdip_DeleteBrush(pBrush)
  2001. {
  2002.    return DllCall("gdiplus\GdipDeleteBrush", A_PtrSize ? "UPtr" : "UInt", pBrush)
  2003. }
  2004.  
  2005. ;#####################################################################################
  2006.  
  2007. Gdip_DisposeImage(pBitmap)
  2008. {
  2009.    return DllCall("gdiplus\GdipDisposeImage", A_PtrSize ? "UPtr" : "UInt", pBitmap)
  2010. }
  2011.  
  2012. ;#####################################################################################
  2013.  
  2014. Gdip_DeleteGraphics(pGraphics)
  2015. {
  2016.    return DllCall("gdiplus\GdipDeleteGraphics", A_PtrSize ? "UPtr" : "UInt", pGraphics)
  2017. }
  2018.  
  2019. ;#####################################################################################
  2020.  
  2021. Gdip_DisposeImageAttributes(ImageAttr)
  2022. {
  2023.     return DllCall("gdiplus\GdipDisposeImageAttributes", A_PtrSize ? "UPtr" : "UInt", ImageAttr)
  2024. }
  2025.  
  2026. ;#####################################################################################
  2027.  
  2028. Gdip_DeleteFont(hFont)
  2029. {
  2030.    return DllCall("gdiplus\GdipDeleteFont", A_PtrSize ? "UPtr" : "UInt", hFont)
  2031. }
  2032.  
  2033. ;#####################################################################################
  2034.  
  2035. Gdip_DeleteStringFormat(hFormat)
  2036. {
  2037.    return DllCall("gdiplus\GdipDeleteStringFormat", A_PtrSize ? "UPtr" : "UInt", hFormat)
  2038. }
  2039.  
  2040. ;#####################################################################################
  2041.  
  2042. Gdip_DeleteFontFamily(hFamily)
  2043. {
  2044.    return DllCall("gdiplus\GdipDeleteFontFamily", A_PtrSize ? "UPtr" : "UInt", hFamily)
  2045. }
  2046.  
  2047. ;#####################################################################################
  2048.  
  2049. Gdip_DeleteMatrix(Matrix)
  2050. {
  2051.    return DllCall("gdiplus\GdipDeleteMatrix", A_PtrSize ? "UPtr" : "UInt", Matrix)
  2052. }
  2053.  
  2054. ;#####################################################################################
  2055. ; Text functions
  2056. ;#####################################################################################
  2057.  
  2058. Gdip_TextToGraphics(pGraphics, Text, Options, Font="Arial", Width="", Height="", Measure=0)
  2059. {
  2060.     IWidth := Width, IHeight:= Height
  2061.    
  2062.     RegExMatch(Options, "i)X([\-\d\.]+)(p*)", xpos)
  2063.     RegExMatch(Options, "i)Y([\-\d\.]+)(p*)", ypos)
  2064.     RegExMatch(Options, "i)W([\-\d\.]+)(p*)", Width)
  2065.     RegExMatch(Options, "i)H([\-\d\.]+)(p*)", Height)
  2066.     RegExMatch(Options, "i)C(?!(entre|enter))([a-f\d]+)", Colour)
  2067.     RegExMatch(Options, "i)Top|Up|Bottom|Down|vCentre|vCenter", vPos)
  2068.     RegExMatch(Options, "i)NoWrap", NoWrap)
  2069.     RegExMatch(Options, "i)R(\d)", Rendering)
  2070.     RegExMatch(Options, "i)S(\d+)(p*)", Size)
  2071.  
  2072.     if !Gdip_DeleteBrush(Gdip_CloneBrush(Colour2))
  2073.         PassBrush := 1, pBrush := Colour2
  2074.    
  2075.     if !(IWidth && IHeight) && (xpos2 || ypos2 || Width2 || Height2 || Size2)
  2076.         return -1
  2077.  
  2078.     Style := 0, Styles := "Regular|Bold|Italic|BoldItalic|Underline|Strikeout"
  2079.     Loop, Parse, Styles, |
  2080.     {
  2081.         if RegExMatch(Options, "\b" A_loopField)
  2082.         Style |= (A_LoopField != "StrikeOut") ? (A_Index-1) : 8
  2083.     }
  2084.  
  2085.     Align := 0, Alignments := "Near|Left|Centre|Center|Far|Right"
  2086.     Loop, Parse, Alignments, |
  2087.     {
  2088.         if RegExMatch(Options, "\b" A_loopField)
  2089.             Align |= A_Index//2.1      ; 0|0|1|1|2|2
  2090.     }
  2091.  
  2092.     xpos := (xpos1 != "") ? xpos2 ? IWidth*(xpos1/100) : xpos1 : 0
  2093.     ypos := (ypos1 != "") ? ypos2 ? IHeight*(ypos1/100) : ypos1 : 0
  2094.     Width := Width1 ? Width2 ? IWidth*(Width1/100) : Width1 : IWidth
  2095.     Height := Height1 ? Height2 ? IHeight*(Height1/100) : Height1 : IHeight
  2096.     if !PassBrush
  2097.         Colour := "0x" (Colour2 ? Colour2 : "ff000000")
  2098.     Rendering := ((Rendering1 >= 0) && (Rendering1 <= 5)) ? Rendering1 : 4
  2099.     Size := (Size1 > 0) ? Size2 ? IHeight*(Size1/100) : Size1 : 12
  2100.  
  2101.     hFamily := Gdip_FontFamilyCreate(Font)
  2102.     hFont := Gdip_FontCreate(hFamily, Size, Style)
  2103.     FormatStyle := NoWrap ? 0x4000 | 0x1000 : 0x4000
  2104.     hFormat := Gdip_StringFormatCreate(FormatStyle)
  2105.     pBrush := PassBrush ? pBrush : Gdip_BrushCreateSolid(Colour)
  2106.     if !(hFamily && hFont && hFormat && pBrush && pGraphics)
  2107.         return !pGraphics ? -2 : !hFamily ? -3 : !hFont ? -4 : !hFormat ? -5 : !pBrush ? -6 : 0
  2108.    
  2109.     CreateRectF(RC, xpos, ypos, Width, Height)
  2110.     Gdip_SetStringFormatAlign(hFormat, Align)
  2111.     Gdip_SetTextRenderingHint(pGraphics, Rendering)
  2112.     ReturnRC := Gdip_MeasureString(pGraphics, Text, hFont, hFormat, RC)
  2113.  
  2114.     if vPos
  2115.     {
  2116.         StringSplit, ReturnRC, ReturnRC, |
  2117.        
  2118.         if (vPos = "vCentre") || (vPos = "vCenter")
  2119.             ypos += (Height-ReturnRC4)//2
  2120.         else if (vPos = "Top") || (vPos = "Up")
  2121.             ypos := 0
  2122.         else if (vPos = "Bottom") || (vPos = "Down")
  2123.             ypos := Height-ReturnRC4
  2124.        
  2125.         CreateRectF(RC, xpos, ypos, Width, ReturnRC4)
  2126.         ReturnRC := Gdip_MeasureString(pGraphics, Text, hFont, hFormat, RC)
  2127.     }
  2128.  
  2129.     if !Measure
  2130.         E := Gdip_DrawString(pGraphics, Text, hFont, hFormat, pBrush, RC)
  2131.  
  2132.     if !PassBrush
  2133.         Gdip_DeleteBrush(pBrush)
  2134.     Gdip_DeleteStringFormat(hFormat)  
  2135.     Gdip_DeleteFont(hFont)
  2136.     Gdip_DeleteFontFamily(hFamily)
  2137.     return E ? E : ReturnRC
  2138. }
  2139.  
  2140. ;#####################################################################################
  2141.  
  2142. Gdip_DrawString(pGraphics, sString, hFont, hFormat, pBrush, ByRef RectF)
  2143. {
  2144.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2145.    
  2146.     if (!A_IsUnicode)
  2147.     {
  2148.         nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sString, "int", -1, Ptr, 0, "int", 0)
  2149.         VarSetCapacity(wString, nSize*2)
  2150.         DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sString, "int", -1, Ptr, &wString, "int", nSize)
  2151.     }
  2152.    
  2153.     return DllCall("gdiplus\GdipDrawString"
  2154.                     , Ptr, pGraphics
  2155.                     , Ptr, A_IsUnicode ? &sString : &wString
  2156.                     , "int", -1
  2157.                     , Ptr, hFont
  2158.                     , Ptr, &RectF
  2159.                     , Ptr, hFormat
  2160.                     , Ptr, pBrush)
  2161. }
  2162.  
  2163. ;#####################################################################################
  2164.  
  2165. Gdip_MeasureString(pGraphics, sString, hFont, hFormat, ByRef RectF)
  2166. {
  2167.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2168.    
  2169.     VarSetCapacity(RC, 16)
  2170.     if !A_IsUnicode
  2171.     {
  2172.         nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sString, "int", -1, "uint", 0, "int", 0)
  2173.         VarSetCapacity(wString, nSize*2)  
  2174.         DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sString, "int", -1, Ptr, &wString, "int", nSize)
  2175.     }
  2176.    
  2177.     DllCall("gdiplus\GdipMeasureString"
  2178.                     , Ptr, pGraphics
  2179.                     , Ptr, A_IsUnicode ? &sString : &wString
  2180.                     , "int", -1
  2181.                     , Ptr, hFont
  2182.                     , Ptr, &RectF
  2183.                     , Ptr, hFormat
  2184.                     , Ptr, &RC
  2185.                     , "uint*", Chars
  2186.                     , "uint*", Lines)
  2187.    
  2188.     return &RC ? NumGet(RC, 0, "float") "|" NumGet(RC, 4, "float") "|" NumGet(RC, 8, "float") "|" NumGet(RC, 12, "float") "|" Chars "|" Lines : 0
  2189. }
  2190.  
  2191. ; Near = 0
  2192. ; Center = 1
  2193. ; Far = 2
  2194. Gdip_SetStringFormatAlign(hFormat, Align)
  2195. {
  2196.    return DllCall("gdiplus\GdipSetStringFormatAlign", A_PtrSize ? "UPtr" : "UInt", hFormat, "int", Align)
  2197. }
  2198.  
  2199. ; StringFormatFlagsDirectionRightToLeft    = 0x00000001
  2200. ; StringFormatFlagsDirectionVertical       = 0x00000002
  2201. ; StringFormatFlagsNoFitBlackBox           = 0x00000004
  2202. ; StringFormatFlagsDisplayFormatControl    = 0x00000020
  2203. ; StringFormatFlagsNoFontFallback          = 0x00000400
  2204. ; StringFormatFlagsMeasureTrailingSpaces   = 0x00000800
  2205. ; StringFormatFlagsNoWrap                  = 0x00001000
  2206. ; StringFormatFlagsLineLimit               = 0x00002000
  2207. ; StringFormatFlagsNoClip                  = 0x00004000
  2208. Gdip_StringFormatCreate(Format=0, Lang=0)
  2209. {
  2210.    DllCall("gdiplus\GdipCreateStringFormat", "int", Format, "int", Lang, A_PtrSize ? "UPtr*" : "UInt*", hFormat)
  2211.    return hFormat
  2212. }
  2213.  
  2214. ; Regular = 0
  2215. ; Bold = 1
  2216. ; Italic = 2
  2217. ; BoldItalic = 3
  2218. ; Underline = 4
  2219. ; Strikeout = 8
  2220. Gdip_FontCreate(hFamily, Size, Style=0)
  2221. {
  2222.    DllCall("gdiplus\GdipCreateFont", A_PtrSize ? "UPtr" : "UInt", hFamily, "float", Size, "int", Style, "int", 0, A_PtrSize ? "UPtr*" : "UInt*", hFont)
  2223.    return hFont
  2224. }
  2225.  
  2226. Gdip_FontFamilyCreate(Font)
  2227. {
  2228.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2229.    
  2230.     if (!A_IsUnicode)
  2231.     {
  2232.         nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &Font, "int", -1, "uint", 0, "int", 0)
  2233.         VarSetCapacity(wFont, nSize*2)
  2234.         DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &Font, "int", -1, Ptr, &wFont, "int", nSize)
  2235.     }
  2236.    
  2237.     DllCall("gdiplus\GdipCreateFontFamilyFromName"
  2238.                     , Ptr, A_IsUnicode ? &Font : &wFont
  2239.                     , "uint", 0
  2240.                     , A_PtrSize ? "UPtr*" : "UInt*", hFamily)
  2241.    
  2242.     return hFamily
  2243. }
  2244.  
  2245. ;#####################################################################################
  2246. ; Matrix functions
  2247. ;#####################################################################################
  2248.  
  2249. Gdip_CreateAffineMatrix(m11, m12, m21, m22, x, y)
  2250. {
  2251.    DllCall("gdiplus\GdipCreateMatrix2", "float", m11, "float", m12, "float", m21, "float", m22, "float", x, "float", y, A_PtrSize ? "UPtr*" : "UInt*", Matrix)
  2252.    return Matrix
  2253. }
  2254.  
  2255. Gdip_CreateMatrix()
  2256. {
  2257.    DllCall("gdiplus\GdipCreateMatrix", A_PtrSize ? "UPtr*" : "UInt*", Matrix)
  2258.    return Matrix
  2259. }
  2260.  
  2261. ;#####################################################################################
  2262. ; GraphicsPath functions
  2263. ;#####################################################################################
  2264.  
  2265. ; Alternate = 0
  2266. ; Winding = 1
  2267. Gdip_CreatePath(BrushMode=0)
  2268. {
  2269.     DllCall("gdiplus\GdipCreatePath", "int", BrushMode, A_PtrSize ? "UPtr*" : "UInt*", Path)
  2270.     return Path
  2271. }
  2272.  
  2273. Gdip_AddPathEllipse(Path, x, y, w, h)
  2274. {
  2275.     return DllCall("gdiplus\GdipAddPathEllipse", A_PtrSize ? "UPtr" : "UInt", Path, "float", x, "float", y, "float", w, "float", h)
  2276. }
  2277.  
  2278. Gdip_AddPathPolygon(Path, Points)
  2279. {
  2280.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2281.    
  2282.     StringSplit, Points, Points, |
  2283.     VarSetCapacity(PointF, 8*Points0)  
  2284.     Loop, %Points0%
  2285.     {
  2286.         StringSplit, Coord, Points%A_Index%, `,
  2287.         NumPut(Coord1, PointF, 8*(A_Index-1), "float"), NumPut(Coord2, PointF, (8*(A_Index-1))+4, "float")
  2288.     }  
  2289.  
  2290.     return DllCall("gdiplus\GdipAddPathPolygon", Ptr, Path, Ptr, &PointF, "int", Points0)
  2291. }
  2292.  
  2293. Gdip_DeletePath(Path)
  2294. {
  2295.     return DllCall("gdiplus\GdipDeletePath", A_PtrSize ? "UPtr" : "UInt", Path)
  2296. }
  2297.  
  2298. ;#####################################################################################
  2299. ; Quality functions
  2300. ;#####################################################################################
  2301.  
  2302. ; SystemDefault = 0
  2303. ; SingleBitPerPixelGridFit = 1
  2304. ; SingleBitPerPixel = 2
  2305. ; AntiAliasGridFit = 3
  2306. ; AntiAlias = 4
  2307. Gdip_SetTextRenderingHint(pGraphics, RenderingHint)
  2308. {
  2309.     return DllCall("gdiplus\GdipSetTextRenderingHint", A_PtrSize ? "UPtr" : "UInt", pGraphics, "int", RenderingHint)
  2310. }
  2311.  
  2312. ; Default = 0
  2313. ; LowQuality = 1
  2314. ; HighQuality = 2
  2315. ; Bilinear = 3
  2316. ; Bicubic = 4
  2317. ; NearestNeighbor = 5
  2318. ; HighQualityBilinear = 6
  2319. ; HighQualityBicubic = 7
  2320. Gdip_SetInterpolationMode(pGraphics, InterpolationMode)
  2321. {
  2322.    return DllCall("gdiplus\GdipSetInterpolationMode", A_PtrSize ? "UPtr" : "UInt", pGraphics, "int", InterpolationMode)
  2323. }
  2324.  
  2325. ; Default = 0
  2326. ; HighSpeed = 1
  2327. ; HighQuality = 2
  2328. ; None = 3
  2329. ; AntiAlias = 4
  2330. Gdip_SetSmoothingMode(pGraphics, SmoothingMode)
  2331. {
  2332.    return DllCall("gdiplus\GdipSetSmoothingMode", A_PtrSize ? "UPtr" : "UInt", pGraphics, "int", SmoothingMode)
  2333. }
  2334.  
  2335. ; CompositingModeSourceOver = 0 (blended)
  2336. ; CompositingModeSourceCopy = 1 (overwrite)
  2337. Gdip_SetCompositingMode(pGraphics, CompositingMode=0)
  2338. {
  2339.    return DllCall("gdiplus\GdipSetCompositingMode", A_PtrSize ? "UPtr" : "UInt", pGraphics, "int", CompositingMode)
  2340. }
  2341.  
  2342. ;#####################################################################################
  2343. ; Extra functions
  2344. ;#####################################################################################
  2345.  
  2346. Gdip_Startup()
  2347. {
  2348.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2349.    
  2350.     if !DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
  2351.         DllCall("LoadLibrary", "str", "gdiplus")
  2352.     VarSetCapacity(si, A_PtrSize = 8 ? 24 : 16, 0), si := Chr(1)
  2353.     DllCall("gdiplus\GdiplusStartup", A_PtrSize ? "UPtr*" : "uint*", pToken, Ptr, &si, Ptr, 0)
  2354.     return pToken
  2355. }
  2356.  
  2357. Gdip_Shutdown(pToken)
  2358. {
  2359.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2360.    
  2361.     DllCall("gdiplus\GdiplusShutdown", Ptr, pToken)
  2362.     if hModule := DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
  2363.         DllCall("FreeLibrary", Ptr, hModule)
  2364.     return 0
  2365. }
  2366.  
  2367. ; Prepend = 0; The new operation is applied before the old operation.
  2368. ; Append = 1; The new operation is applied after the old operation.
  2369. Gdip_RotateWorldTransform(pGraphics, Angle, MatrixOrder=0)
  2370. {
  2371.     return DllCall("gdiplus\GdipRotateWorldTransform", A_PtrSize ? "UPtr" : "UInt", pGraphics, "float", Angle, "int", MatrixOrder)
  2372. }
  2373.  
  2374. Gdip_ScaleWorldTransform(pGraphics, x, y, MatrixOrder=0)
  2375. {
  2376.     return DllCall("gdiplus\GdipScaleWorldTransform", A_PtrSize ? "UPtr" : "UInt", pGraphics, "float", x, "float", y, "int", MatrixOrder)
  2377. }
  2378.  
  2379. Gdip_TranslateWorldTransform(pGraphics, x, y, MatrixOrder=0)
  2380. {
  2381.     return DllCall("gdiplus\GdipTranslateWorldTransform", A_PtrSize ? "UPtr" : "UInt", pGraphics, "float", x, "float", y, "int", MatrixOrder)
  2382. }
  2383.  
  2384. Gdip_ResetWorldTransform(pGraphics)
  2385. {
  2386.     return DllCall("gdiplus\GdipResetWorldTransform", A_PtrSize ? "UPtr" : "UInt", pGraphics)
  2387. }
  2388.  
  2389. Gdip_GetRotatedTranslation(Width, Height, Angle, ByRef xTranslation, ByRef yTranslation)
  2390. {
  2391.     pi := 3.14159, TAngle := Angle*(pi/180)
  2392.  
  2393.     Bound := (Angle >= 0) ? Mod(Angle, 360) : 360-Mod(-Angle, -360)
  2394.     if ((Bound >= 0) && (Bound <= 90))
  2395.         xTranslation := Height*Sin(TAngle), yTranslation := 0
  2396.     else if ((Bound > 90) && (Bound <= 180))
  2397.         xTranslation := (Height*Sin(TAngle))-(Width*Cos(TAngle)), yTranslation := -Height*Cos(TAngle)
  2398.     else if ((Bound > 180) && (Bound <= 270))
  2399.         xTranslation := -(Width*Cos(TAngle)), yTranslation := -(Height*Cos(TAngle))-(Width*Sin(TAngle))
  2400.     else if ((Bound > 270) && (Bound <= 360))
  2401.         xTranslation := 0, yTranslation := -Width*Sin(TAngle)
  2402. }
  2403.  
  2404. Gdip_GetRotatedDimensions(Width, Height, Angle, ByRef RWidth, ByRef RHeight)
  2405. {
  2406.     pi := 3.14159, TAngle := Angle*(pi/180)
  2407.     if !(Width && Height)
  2408.         return -1
  2409.     RWidth := Ceil(Abs(Width*Cos(TAngle))+Abs(Height*Sin(TAngle)))
  2410.     RHeight := Ceil(Abs(Width*Sin(TAngle))+Abs(Height*Cos(Tangle)))
  2411. }
  2412.  
  2413. ; RotateNoneFlipNone   = 0
  2414. ; Rotate90FlipNone     = 1
  2415. ; Rotate180FlipNone    = 2
  2416. ; Rotate270FlipNone    = 3
  2417. ; RotateNoneFlipX      = 4
  2418. ; Rotate90FlipX        = 5
  2419. ; Rotate180FlipX       = 6
  2420. ; Rotate270FlipX       = 7
  2421. ; RotateNoneFlipY      = Rotate180FlipX
  2422. ; Rotate90FlipY        = Rotate270FlipX
  2423. ; Rotate180FlipY       = RotateNoneFlipX
  2424. ; Rotate270FlipY       = Rotate90FlipX
  2425. ; RotateNoneFlipXY     = Rotate180FlipNone
  2426. ; Rotate90FlipXY       = Rotate270FlipNone
  2427. ; Rotate180FlipXY      = RotateNoneFlipNone
  2428. ; Rotate270FlipXY      = Rotate90FlipNone
  2429.  
  2430. Gdip_ImageRotateFlip(pBitmap, RotateFlipType=1)
  2431. {
  2432.     return DllCall("gdiplus\GdipImageRotateFlip", A_PtrSize ? "UPtr" : "UInt", pBitmap, "int", RotateFlipType)
  2433. }
  2434.  
  2435. ; Replace = 0
  2436. ; Intersect = 1
  2437. ; Union = 2
  2438. ; Xor = 3
  2439. ; Exclude = 4
  2440. ; Complement = 5
  2441. Gdip_SetClipRect(pGraphics, x, y, w, h, CombineMode=0)
  2442. {
  2443.    return DllCall("gdiplus\GdipSetClipRect",  A_PtrSize ? "UPtr" : "UInt", pGraphics, "float", x, "float", y, "float", w, "float", h, "int", CombineMode)
  2444. }
  2445.  
  2446. Gdip_SetClipPath(pGraphics, Path, CombineMode=0)
  2447. {
  2448.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2449.     return DllCall("gdiplus\GdipSetClipPath", Ptr, pGraphics, Ptr, Path, "int", CombineMode)
  2450. }
  2451.  
  2452. Gdip_ResetClip(pGraphics)
  2453. {
  2454.    return DllCall("gdiplus\GdipResetClip", A_PtrSize ? "UPtr" : "UInt", pGraphics)
  2455. }
  2456.  
  2457. Gdip_GetClipRegion(pGraphics)
  2458. {
  2459.     Region := Gdip_CreateRegion()
  2460.     DllCall("gdiplus\GdipGetClip", A_PtrSize ? "UPtr" : "UInt", pGraphics, A_PtrSize ? "UPtr*" : "UInt*", Region)
  2461.     return Region
  2462. }
  2463.  
  2464. Gdip_SetClipRegion(pGraphics, Region, CombineMode=0)
  2465. {
  2466.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2467.    
  2468.     return DllCall("gdiplus\GdipSetClipRegion", Ptr, pGraphics, Ptr, Region, "int", CombineMode)
  2469. }
  2470.  
  2471. Gdip_CreateRegion()
  2472. {
  2473.     DllCall("gdiplus\GdipCreateRegion", A_PtrSize ? "UPtr*" : "UInt*", Region)
  2474.     return Region
  2475. }
  2476.  
  2477. Gdip_DeleteRegion(Region)
  2478. {
  2479.     return DllCall("gdiplus\GdipDeleteRegion", A_PtrSize ? "UPtr" : "UInt", Region)
  2480. }
  2481.  
  2482. ;#####################################################################################
  2483. ; BitmapLockBits
  2484. ;#####################################################################################
  2485.  
  2486. Gdip_LockBits(pBitmap, x, y, w, h, ByRef Stride, ByRef Scan0, ByRef BitmapData, LockMode = 3, PixelFormat = 0x26200a)
  2487. {
  2488.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2489.    
  2490.     CreateRect(Rect, x, y, w, h)
  2491.     VarSetCapacity(BitmapData, 16+2*(A_PtrSize ? A_PtrSize : 4), 0)
  2492.     E := DllCall("Gdiplus\GdipBitmapLockBits", Ptr, pBitmap, Ptr, &Rect, "uint", LockMode, "int", PixelFormat, Ptr, &BitmapData)
  2493.     Stride := NumGet(BitmapData, 8, "Int")
  2494.     Scan0 := NumGet(BitmapData, 16, Ptr)
  2495.     return E
  2496. }
  2497.  
  2498. ;#####################################################################################
  2499.  
  2500. Gdip_UnlockBits(pBitmap, ByRef BitmapData)
  2501. {
  2502.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2503.    
  2504.     return DllCall("Gdiplus\GdipBitmapUnlockBits", Ptr, pBitmap, Ptr, &BitmapData)
  2505. }
  2506.  
  2507. ;#####################################################################################
  2508.  
  2509. Gdip_SetLockBitPixel(ARGB, Scan0, x, y, Stride)
  2510. {
  2511.     Numput(ARGB, Scan0+0, (x*4)+(y*Stride), "UInt")
  2512. }
  2513.  
  2514. ;#####################################################################################
  2515.  
  2516. Gdip_GetLockBitPixel(Scan0, x, y, Stride)
  2517. {
  2518.     return NumGet(Scan0+0, (x*4)+(y*Stride), "UInt")
  2519. }
  2520.  
  2521. ;#####################################################################################
  2522.  
  2523. Gdip_PixelateBitmap(pBitmap, ByRef pBitmapOut, BlockSize)
  2524. {
  2525.     static PixelateBitmap
  2526.    
  2527.     Ptr := A_PtrSize ? "UPtr" : "UInt"
  2528.    
  2529.     if (!PixelateBitmap)
  2530.     {
  2531.         if A_PtrSize != 8 ; x86 machine code
  2532.         MCode_PixelateBitmap =
  2533.         (LTrim Join
  2534.         558BEC83EC3C8B4514538B5D1C99F7FB56578BC88955EC894DD885C90F8E830200008B451099F7FB8365DC008365E000894DC88955F08945E833FF897DD4
  2535.         397DE80F8E160100008BCB0FAFCB894DCC33C08945F88945FC89451C8945143BD87E608B45088D50028BC82BCA8BF02BF2418945F48B45E02955F4894DC4
  2536.         8D0CB80FAFCB03CA895DD08BD1895DE40FB64416030145140FB60201451C8B45C40FB604100145FC8B45F40FB604020145F883C204FF4DE475D6034D18FF
  2537.         4DD075C98B4DCC8B451499F7F98945148B451C99F7F989451C8B45FC99F7F98945FC8B45F899F7F98945F885DB7E648B450C8D50028BC82BCA83C103894D
  2538.         C48BC82BCA41894DF48B4DD48945E48B45E02955E48D0C880FAFCB03CA895DD08BD18BF38A45148B7DC48804178A451C8B7DF488028A45FC8804178A45F8
  2539.         8B7DE488043A83C2044E75DA034D18FF4DD075CE8B4DCC8B7DD447897DD43B7DE80F8CF2FEFFFF837DF0000F842C01000033C08945F88945FC89451C8945
  2540.         148945E43BD87E65837DF0007E578B4DDC034DE48B75E80FAF4D180FAFF38B45088D500203CA8D0CB18BF08BF88945F48B45F02BF22BFA2955F48945CC0F
  2541.         B6440E030145140FB60101451C0FB6440F010145FC8B45F40FB604010145F883C104FF4DCC75D8FF45E4395DE47C9B8B4DF00FAFCB85C9740B8B451499F7
  2542.         F9894514EB048365140033F63BCE740B8B451C99F7F989451CEB0389751C3BCE740B8B45FC99F7F98945FCEB038975FC3BCE740B8B45F899F7F98945F8EB
  2543.         038975F88975E43BDE7E5A837DF0007E4C8B4DDC034DE48B75E80FAF4D180FAFF38B450C8D500203CA8D0CB18BF08BF82BF22BFA2BC28B55F08955CC8A55
  2544.         1488540E038A551C88118A55FC88540F018A55F888140183C104FF4DCC75DFFF45E4395DE47CA68B45180145E0015DDCFF4DC80F8594FDFFFF8B451099F7
  2545.         FB8955F08945E885C00F8E450100008B45EC0FAFC38365DC008945D48B45E88945CC33C08945F88945FC89451C8945148945103945EC7E6085DB7E518B4D
  2546.         D88B45080FAFCB034D108D50020FAF4D18034DDC8BF08BF88945F403CA2BF22BFA2955F4895DC80FB6440E030145140FB60101451C0FB6440F010145FC8B
  2547.         45F40FB604080145F883C104FF4DC875D8FF45108B45103B45EC7CA08B4DD485C9740B8B451499F7F9894514EB048365140033F63BCE740B8B451C99F7F9
  2548.         89451CEB0389751C3BCE740B8B45FC99F7F98945FCEB038975FC3BCE740B8B45F899F7F98945F8EB038975F88975103975EC7E5585DB7E468B4DD88B450C
  2549.         0FAFCB034D108D50020FAF4D18034DDC8BF08BF803CA2BF22BFA2BC2895DC88A551488540E038A551C88118A55FC88540F018A55F888140183C104FF4DC8
  2550.         75DFFF45108B45103B45EC7CAB8BC3C1E0020145DCFF4DCC0F85CEFEFFFF8B4DEC33C08945F88945FC89451C8945148945103BC87E6C3945F07E5C8B4DD8
  2551.         8B75E80FAFCB034D100FAFF30FAF4D188B45088D500203CA8D0CB18BF08BF88945F48B45F02BF22BFA2955F48945C80FB6440E030145140FB60101451C0F
  2552.         B6440F010145FC8B45F40FB604010145F883C104FF4DC875D833C0FF45108B4DEC394D107C940FAF4DF03BC874068B451499F7F933F68945143BCE740B8B
  2553.         451C99F7F989451CEB0389751C3BCE740B8B45FC99F7F98945FCEB038975FC3BCE740B8B45F899F7F98945F8EB038975F88975083975EC7E63EB0233F639
  2554.         75F07E4F8B4DD88B75E80FAFCB034D080FAFF30FAF4D188B450C8D500203CA8D0CB18BF08BF82BF22BFA2BC28B55F08955108A551488540E038A551C8811
  2555.         8A55FC88540F018A55F888140883C104FF4D1075DFFF45088B45083B45EC7C9F5F5E33C05BC9C21800
  2556.         )
  2557.         else ; x64 machine code
  2558.         MCode_PixelateBitmap =
  2559.         (LTrim Join
  2560.         4489442418488954241048894C24085355565741544155415641574883EC28418BC1448B8C24980000004C8BDA99488BD941F7F9448BD0448BFA8954240C
  2561.         448994248800000085C00F8E9D020000418BC04533E4458BF299448924244C8954241041F7F933C9898C24980000008BEA89542404448BE889442408EB05
  2562.         4C8B5C24784585ED0F8E1A010000458BF1418BFD48897C2418450FAFF14533D233F633ED4533E44533ED4585C97E5B4C63BC2490000000418D040A410FAF
  2563.         C148984C8D441802498BD9498BD04D8BD90FB642010FB64AFF4403E80FB60203E90FB64AFE4883C2044403E003F149FFCB75DE4D03C748FFCB75D0488B7C
  2564.         24188B8C24980000004C8B5C2478418BC59941F7FE448BE8418BC49941F7FE448BE08BC59941F7FE8BE88BC69941F7FE8BF04585C97E4048639C24900000
  2565.         004103CA4D8BC1410FAFC94863C94A8D541902488BCA498BC144886901448821408869FF408871FE4883C10448FFC875E84803D349FFC875DA8B8C249800
  2566.         0000488B5C24704C8B5C24784183C20448FFCF48897C24180F850AFFFFFF8B6C2404448B2424448B6C24084C8B74241085ED0F840A01000033FF33DB4533
  2567.         DB4533D24533C04585C97E53488B74247085ED7E42438D0C04418BC50FAF8C2490000000410FAFC18D04814863C8488D5431028BCD0FB642014403D00FB6
  2568.         024883C2044403D80FB642FB03D80FB642FA03F848FFC975DE41FFC0453BC17CB28BCD410FAFC985C9740A418BC299F7F98BF0EB0233F685C9740B418BC3
  2569.         99F7F9448BD8EB034533DB85C9740A8BC399F7F9448BD0EB034533D285C9740A8BC799F7F9448BC0EB034533C033D24585C97E4D4C8B74247885ED7E3841
  2570.         8D0C14418BC50FAF8C2490000000410FAFC18D04814863C84A8D4431028BCD40887001448818448850FF448840FE4883C00448FFC975E8FFC2413BD17CBD
  2571.         4C8B7424108B8C2498000000038C2490000000488B5C24704503E149FFCE44892424898C24980000004C897424100F859EFDFFFF448B7C240C448B842480
  2572.         000000418BC09941F7F98BE8448BEA89942498000000896C240C85C00F8E3B010000448BAC2488000000418BCF448BF5410FAFC9898C248000000033FF33
  2573.         ED33F64533DB4533D24533C04585FF7E524585C97E40418BC5410FAFC14103C00FAF84249000000003C74898488D541802498BD90FB642014403D00FB602
  2574.         4883C2044403D80FB642FB03F00FB642FA03E848FFCB75DE488B5C247041FFC0453BC77CAE85C9740B418BC299F7F9448BE0EB034533E485C9740A418BC3
  2575.         99F7F98BD8EB0233DB85C9740A8BC699F7F9448BD8EB034533DB85C9740A8BC599F7F9448BD0EB034533D24533C04585FF7E4E488B4C24784585C97E3541
  2576.         8BC5410FAFC14103C00FAF84249000000003C74898488D540802498BC144886201881A44885AFF448852FE4883C20448FFC875E941FFC0453BC77CBE8B8C
  2577.         2480000000488B5C2470418BC1C1E00203F849FFCE0F85ECFEFFFF448BAC24980000008B6C240C448BA4248800000033FF33DB4533DB4533D24533C04585
  2578.         FF7E5A488B7424704585ED7E48418BCC8BC5410FAFC94103C80FAF8C2490000000410FAFC18D04814863C8488D543102418BCD0FB642014403D00FB60248
  2579.         83C2044403D80FB642FB03D80FB642FA03F848FFC975DE41FFC0453BC77CAB418BCF410FAFCD85C9740A418BC299F7F98BF0EB0233F685C9740B418BC399
  2580.         F7F9448BD8EB034533DB85C9740A8BC399F7F9448BD0EB034533D285C9740A8BC799F7F9448BC0EB034533C033D24585FF7E4E4585ED7E42418BCC8BC541
  2581.         0FAFC903CA0FAF8C2490000000410FAFC18D04814863C8488B442478488D440102418BCD40887001448818448850FF448840FE4883C00448FFC975E8FFC2
  2582.         413BD77CB233C04883C428415F415E415D415C5F5E5D5BC3
  2583.         )
  2584.        
  2585.         VarSetCapacity(PixelateBitmap, StrLen(MCode_PixelateBitmap)//2)
  2586.         Loop % StrLen(MCode_PixelateBitmap)//2      ;%
  2587.             NumPut("0x" SubStr(MCode_PixelateBitmap, (2*A_Index)-1, 2), PixelateBitmap, A_Index-1, "UChar")
  2588.         DllCall("VirtualProtect", Ptr, &PixelateBitmap, Ptr, VarSetCapacity(PixelateBitmap), "uint", 0x40, A_PtrSize ? "UPtr*" : "UInt*", 0)
  2589.     }
  2590.  
  2591.     Gdip_GetImageDimensions(pBitmap, Width, Height)
  2592.    
  2593.     if (Width != Gdip_GetImageWidth(pBitmapOut) || Height != Gdip_GetImageHeight(pBitmapOut))
  2594.         return -1
  2595.     if (BlockSize > Width || BlockSize > Height)
  2596.         return -2
  2597.  
  2598.     E1 := Gdip_LockBits(pBitmap, 0, 0, Width, Height, Stride1, Scan01, BitmapData1)
  2599.     E2 := Gdip_LockBits(pBitmapOut, 0, 0, Width, Height, Stride2, Scan02, BitmapData2)
  2600.     if (E1 || E2)
  2601.         return -3
  2602.  
  2603.     E := DllCall(&PixelateBitmap, Ptr, Scan01, Ptr, Scan02, "int", Width, "int", Height, "int", Stride1, "int", BlockSize)
  2604.    
  2605.     Gdip_UnlockBits(pBitmap, BitmapData1), Gdip_UnlockBits(pBitmapOut, BitmapData2)
  2606.     return 0
  2607. }
  2608.  
  2609. ;#####################################################################################
  2610.  
  2611. Gdip_ToARGB(A, R, G, B)
  2612. {
  2613.     return (A << 24) | (R << 16) | (G << 8) | B
  2614. }
  2615.  
  2616. ;#####################################################################################
  2617.  
  2618. Gdip_FromARGB(ARGB, ByRef A, ByRef R, ByRef G, ByRef B)
  2619. {
  2620.     A := (0xff000000 & ARGB) >> 24
  2621.     R := (0x00ff0000 & ARGB) >> 16
  2622.     G := (0x0000ff00 & ARGB) >> 8
  2623.     B := 0x000000ff & ARGB
  2624. }
  2625.  
  2626. ;#####################################################################################
  2627.  
  2628. Gdip_AFromARGB(ARGB)
  2629. {
  2630.     return (0xff000000 & ARGB) >> 24
  2631. }
  2632.  
  2633. ;#####################################################################################
  2634.  
  2635. Gdip_RFromARGB(ARGB)
  2636. {
  2637.     return (0x00ff0000 & ARGB) >> 16
  2638. }
  2639.  
  2640. ;#####################################################################################
  2641.  
  2642. Gdip_GFromARGB(ARGB)
  2643. {
  2644.     return (0x0000ff00 & ARGB) >> 8
  2645. }
  2646.  
  2647. ;#####################################################################################
  2648.  
  2649. Gdip_BFromARGB(ARGB)
  2650. {
  2651.     return 0x000000ff & ARGB
  2652. }
  2653.  
  2654. ;#####################################################################################
  2655.  
  2656. StrGetB(Address, Length=-1, Encoding=0)
  2657. {
  2658.     ; Flexible parameter handling:
  2659.     if Length is not integer
  2660.     Encoding := Length,  Length := -1
  2661.  
  2662.     ; Check for obvious errors.
  2663.     if (Address+0 < 1024)
  2664.         return
  2665.  
  2666.     ; Ensure 'Encoding' contains a numeric identifier.
  2667.     if Encoding = UTF-16
  2668.         Encoding = 1200
  2669.     else if Encoding = UTF-8
  2670.         Encoding = 65001
  2671.     else if SubStr(Encoding,1,2)="CP"
  2672.         Encoding := SubStr(Encoding,3)
  2673.  
  2674.     if !Encoding ; "" or 0
  2675.     {
  2676.         ; No conversion necessary, but we might not want the whole string.
  2677.         if (Length == -1)
  2678.             Length := DllCall("lstrlen", "uint", Address)
  2679.         VarSetCapacity(String, Length)
  2680.         DllCall("lstrcpyn", "str", String, "uint", Address, "int", Length + 1)
  2681.     }
  2682.     else if Encoding = 1200 ; UTF-16
  2683.     {
  2684.         char_count := DllCall("WideCharToMultiByte", "uint", 0, "uint", 0x400, "uint", Address, "int", Length, "uint", 0, "uint", 0, "uint", 0, "uint", 0)
  2685.         VarSetCapacity(String, char_count)
  2686.         DllCall("WideCharToMultiByte", "uint", 0, "uint", 0x400, "uint", Address, "int", Length, "str", String, "int", char_count, "uint", 0, "uint", 0)
  2687.     }
  2688.     else if Encoding is integer
  2689.     {
  2690.         ; Convert from target encoding to UTF-16 then to the active code page.
  2691.         char_count := DllCall("MultiByteToWideChar", "uint", Encoding, "uint", 0, "uint", Address, "int", Length, "uint", 0, "int", 0)
  2692.         VarSetCapacity(String, char_count * 2)
  2693.         char_count := DllCall("MultiByteToWideChar", "uint", Encoding, "uint", 0, "uint", Address, "int", Length, "uint", &String, "int", char_count * 2)
  2694.         String := StrGetB(&String, char_count, 1200)
  2695.     }
  2696.    
  2697.     return String
  2698. }
Advertisement
Add Comment
Please, Sign In to add comment