Advertisement
twiz

GDIP.ahk

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