Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

GDI+ standard library 1.45 by tic


  • Please log in to reply
1385 replies to this topic
tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007

Download: Gdip.ahk
Alternative Download: Gdip All ((beta) - Alternate link for other versions of AHK: 32, 64, unicode, ansi)

Full featured gdi+ library with examples by me tic (Tariq Porter)
Supports AHK and AHK_L

This library can be distributed for commercial and non-commercial use and I actively encourage both. I would appreciate it if people give acknowledgement for my work and would be nice to have pasted once at the top of your script something similar to:


Thanks to tic (Tariq Porter) for his GDI+ Library
http://www.autohotkey.com/forum/viewtopic.php?t=32238

or anything more elaborate if you feel like it!

A big thank you to Lexikos and Sean for their help and contributions

You Can download the official help file maintained by Lucid_Method here

If your OS is not >= XP then you will need gdiplus.dll

You will need to place it in the same directory as the script
Minimum OS is Windows 2000

As well as the below examples you can also see GDI+ voodoo

Examples

Example 1: Gdip.Tutorial.1-Draw.Shapes.ahk - Create a simple gui and draw an ellipse and a rectangle

Example 2: Gdip.Tutorial.2-Draw.Outlined.Shapes.ahk - Create a simple gui and draw the outlines of an ellipse and a rectangle

Example 3: Gdip.Tutorial.3-Create.Gui.From.Image.ahk - Create a gui from an existing image on disk

Example 4: Gdip.Tutorial.4-Draw.Circles.ahk - Create a fullscreen gui and fill it with random ellipses

Example 5: Gdip.Tutorial.5-Create.Bitmap.ahk - Put a few shapes into a bitmap and save to a ".png" filetype

Example 6: Gdip.Tutorial.6-Image.Editing.ahk - Take files from disk, load them onto a background and save to a ".png" filetype

Example 7: Gdip.Tutorial.7-Draw.draggable.rounded.rectangle.ahk - Draw a rounded rectangle for a gui and allow it to be dragged with the left click of the mouse

Example 8: Gdip.Tutorial.8-Write.text.onto.a.gui.ahk - Take the rounded rectangle gui and add text to it, showing all the text options possible

Example 9: Gdip.Tutorial.9-Create.a.progress.bar.on.standard.gui.ahk - Create a nice looking progress bar using the library in a standard gui

Example 10: Gdip.Tutorial.10-Rotate.Flip.or.Mirror.an.image.ahk - Rotate, flip or mirror an image on a layered window

Please note BRA functions will be released soon!

The BRA Library so far: BRA.ahk

Example 11: Gdip.Tutorial.11-Using.BRAs.to.perform.animations.ahk - Using BRA files to transport many images with your script and show animation
Please also download the BRA here: Gdip.tutorial.file-fish.bra

Example 12: Gdip.Tutorial.12-Pixelate.a.bitmap.using.machine.code - Use LockBits and machine code to pixelate an image and update it quickly



trik
  • Members
  • 1317 posts
  • Last active: Jun 11 2010 11:48 PM
  • Joined: 15 Jul 2007
Examples? :lol:

Or please get in the IRC.
Religion is false. >_>

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
Well the 1st example I have just posted is how to set a pixel to any colour in an image and save it as any filetype:

#SingleInstance, Force
SetBatchLines, -1
SetWorkingDir %A_ScriptDir%

#Include Gdip.ahk

pToken := Gdip_Startup()
pBitmap := Gdip_CreateBitmapFromFile("in.png")
Gdip_SetPixel(pBitmap, 100, 100, 0xffff0000)
Gdip_SaveBitmap(pBitmap, "out.png")
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
ExitApp
Return

Very simple example, but more will come. If you see one you like the look of then ask and I'm sure I'll already have an existing example lying around

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
Example 1 released: This will create a "screensaver" like effect by drawing random circles on the screen, that are randomly filled with different hatched brushes.

Gdip updated to 1.01

  • Guests
  • Last active:
  • Joined: --
I just d/l and ran it, and... - nothing happens.

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
Which OS are you on? If you are Win2000 then you will also need to download gdiplus.dll from the 1st post. Please redownload the example and library

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
Please download version 1.02 of the library! There are more than just cosmetic changes.

Also example 2 has been added. Could people please repond with how they useful/hard they are finding them or else I don't know if there's any point continuing! :p

Example 2 will show you how to create a bitmap, fill it with shapes and save to any supported filetype

danalec
  • Members
  • 225 posts
  • Last active: Oct 03 2014 05:31 PM
  • Joined: 20 Jul 2006
fast & furious gdi+ manipulation!

corrupt
  • Members
  • 2558 posts
  • Last active: Nov 01 2014 03:23 PM
  • Joined: 29 Dec 2004
Cool :)

heresy
  • Members
  • 291 posts
  • Last active: Sep 26 2008 10:47 PM
  • Joined: 11 Mar 2008
thanks for sharing this well organized library and kind tutorials
gdi is always been an inviolable area for beginners (non-programmers)
seems it will take me to the sweet gdi world
i like the tutorials :)
Thanks tic!!
Easy WinAPI - Dive into Windows API World
Benchmark your AutoHotkey skills at PlayAHK.com

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
Thank you for your kind words. As there is at least some enthusiasm (:p) I have decided to rejiggle the examples. I have made a new 1st example and moved the other ones onwards. The new example is much easier to understand. The same method is used for any gui using gdi+

Please comment if you want some more examples at this level! I guarantee anyone else can be a master of gdi+ in a matter of hours!

heresy
  • Members
  • 291 posts
  • Last active: Sep 26 2008 10:47 PM
  • Joined: 11 Mar 2008
people like me who never learn gdi before, must try 1st tutorial on top post.
after following 1st tutorial with 0% knowledge of gdi.
i could draw a frame, filled rectangle in only 20 mins
how easy tic made it for us. this is awesome :)
thank you so much tic
Easy WinAPI - Dive into Windows API World
Benchmark your AutoHotkey skills at PlayAHK.com

Rhys
  • Members
  • 761 posts
  • Last active: Aug 09 2013 04:53 PM
  • Joined: 17 Apr 2007
Tic, I'm gonna go through your examples tomorrow - Here's the video I mentioned of the press+hold for right click in vista - The quality is crap but you can see what I'm talking about at least: <!-- m --><!-- m -->

That's what I'd like to try to emulate once I get better. Difficulty on a scale of 1-10?

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
I've watched the video. If you need a custom texture on the circle as in that example then its a 7/10 but if you just want to draw a circle and fill it with a brush (can be a pre-textured brush) then 3/10. I'll get onto giving an example soon...

Edit:

I have updated the library again. Make sure you always have the latest one!
There is another simple example now using pens rather than brushes!

Rhys
  • Members
  • 761 posts
  • Last active: Aug 09 2013 04:53 PM
  • Joined: 17 Apr 2007
What I've gotten so far... Hold Shift down to draw a circle around your mouse - It will stay as long as you hold shift.

Forgive the code, still testing (even if I'm just hacking up tic's example)!

#SingleInstance, Force
#NoEnv
SetBatchLines, -1

; Uncomment if Gdip.ahk is not in your standard library
#Include, Gdip.ahk
Return
~Shift::
; Start gdi+
If !pToken := Gdip_Startup()
{
	MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
	ExitApp
}
OnExit, Exit

; Set the width and height we want as our drawing area, to draw everything in. This will be the dimensions of our bitmap
Width := 56, Height := 56
;Width := A_ScreenWidth, Height := A_ScreenHeight

; Create a layered window (+E0x80000 : must be used for UpdateLayeredWindow to work!) that is always on top (+AlwaysOnTop), has no taskbar entry or caption
Gui, 1: -Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs

; Show the window
Gui, 1: Show, NA

; Get a handle to this window we have created in order to update it later
hwnd1 := WinExist()

; Create a gdi bitmap with width and height of what we are going to draw into it. This is the entire drawing area for everything
hbm := CreateDIBSection(Width, Height)

; Get a device context compatible with the screen
hdc := CreateCompatibleDC()

; Select the bitmap into the device context
obm := SelectObject(hdc, hbm)

; Get a pointer to the graphics of the bitmap, for use with drawing functions
G := Gdip_GraphicsFromHDC(hdc)

; Set the smoothing mode to antialias = 4 to make shapes appear smother (only used for vector drawing and filling)
Gdip_SetSmoothingMode(G, 4)

; Create a fully opaque red brush (ARGB = Transparency, red, green, blue) to draw a circle
hBrush := Gdip_BrushCreateSolid(0xffff0000)


; Fill the graphics of the bitmap with an ellipse using the brush created
; Filling from coordinates (100,50) an ellipse of 200x300
;Gdip_FillEllipse(G, hBrush, 100, 50, 600, 600)

; Delete the brush as it is no longer needed and wastes memory
Gdip_DeleteBrush(hBrush)

; Create a slightly transparent (66) blue brush (ARGB = Transparency, red, green, blue) to draw a rectangle
hBrush := Gdip_BrushCreateSolid(0x660000ff)
hBrush:=Gdip_BrushCreateHatch(0x660000ff, 0xff0000ff, 47)

; Fill the graphics of the bitmap with a rectangle using the brush created
; Filling from coordinates (250,80) a rectangle of 300x200
;Gdip_FillRectangle(G, hBrush, 250, 80, 300, 200)

; Delete the brush as it is no longer needed and wastes memory
Gdip_DeleteBrush(hBrush)


hBrush:=Gdip_BrushCreateSolid(0xEEff0000)
hPen:=Gdip_CreatePen(0xEEFFFFee0, 2)
fatPen:=Gdip_CreatePen(0xEE0000FF, 5)
CoordMode, Mouse, Screen

Loop,60
{
GetKeyState,State,Shift,P
If State = U
	Break
MouseGetPos,MX,MY
;Gdip_DrawArc(pGraphics, hPen, x, y, w, h, StartAngle, SweepAngle)
Gdip_SetCompositingMode(G, 1)
Gdip_Drawarc(G, FatPen, 2, 2, 50, 50, 270, A_Index*6)
Gdip_SetCompositingMode(G, 0)
Gdip_Drawarc(G, hPen, 2, 2, 50, 50, 270, A_Index*6)
UpdateLayeredWindow(hwnd1, hdc, MX-25, MY-25, Width, Height)
Sleep,10
}
Gdip_SetCompositingMode(G, 0)
Loop,
{
	GetKeyState,State,Shift,P
	If State = U
		Break
	MouseGetPos,MX,MY
	UpdateLayeredWindow(hwnd1, hdc, MX-25, MY-25, Width, Height)
	Sleep,10
}
; Update the specified window we have created (hwnd1) with a handle to our bitmap (hdc), specifying the x,y,w,h we want it positioned on our screen
; So this will position our gui at (0,0) with the Width and Height specified earlier
;UpdateLayeredWindow(hwnd1, hdc, 0, 0, Width, Height)
Gui,Destroy

; Select the object back into the hdc
SelectObject(hdc, obm)

; Now the bitmap may be deleted
DeleteObject(hbm)

; Also the device context related to the bitmap may be deleted
DeleteDC(hdc)

; The graphics may now be deleted
Gdip_DeleteGraphics(G)
Return

;#######################################################################
Esc::
Exit:
; gdi+ may now be shutdown on exiting the program
Gdip_Shutdown(pToken)
ExitApp
Return