AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

GDI+ standard library 1.22 by tic
Goto page Previous  1, 2, 3, 4, 5, 6  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
LuceGold2
Guest





PostPosted: Fri Jun 27, 2008 3:17 pm    Post subject: Reply with quote

Tic, thank you incredibly for the reference to this post.

Please see what I have here and hopefully you can guide me further:

Below is basically what I am using, which works, to generate a image file from a PSTREAM

Code:


; GENERATE GDI+ BITMAP FROM A PSTREAM

DllCall( "LoadLibrary", Str,"gdiplus" )
VarSetCapacity(si, 16, 0), si := Chr(1)
DllCall( "gdiplus\GdiplusStartup", UIntP,pToken, UInt,&si, UInt,0 )
DllCall( "gdiplus\GdipCreateBitmapFromStream", UInt,pStream, UIntP,bitmap )

; SAVE THIS BITMAP TO A FILE

GDIplus_GetEncoderCLSID(pngEncoder, "image/png")
GDIplus_SaveImage(bitmap, "C:\image.png", pngEncoder, encoderParams)



... but what I really want to do, is load up more than one pStream into the Bitmap before it is saved. Where I am slightly confused is this

Code:


Gdip_DrawImage(G, pBitmap, dx, dy, dw, dh, sx, sy, sw, sh, 0.5)



I don't really understand how this line works... but where I am very confused is the transparency part. I really don't want one image less 'transparent' than the other... They are both assumed to be semi-transparent PNG's, and I want to Composite them over each other.

Basically, what would be so awesome is if I can see a working model, where a 500x500 flat red or green background is generated,.... and 2 images of 100x100 are placed over this background. The images should be semi-transparent (i.e. some PNG image that would have transparent parts blended over this color backdrop ---) Smile

I hope this makes sense. I am really greatful for your assistance.

Very Happy
Back to top
tic



Joined: 22 Apr 2007
Posts: 1365

PostPosted: Fri Jun 27, 2008 3:37 pm    Post subject: Reply with quote

Do you want this to be saved to disk or displayed as a gui? Also is there any reason you keep mentioning using streams? Is this what you are asking:


    Create a 500x500 green background
    Take a png from disk and draw it over the green background
    Save the result to disk as another png
Back to top
View user's profile Send private message
LuceGold2
Guest





PostPosted: Fri Jun 27, 2008 3:46 pm    Post subject: Reply with quote

Hiyas!

1) Create a 500x500 green background
2) Take a png from disk and draw it over the green background at x25 y25
3) Take another png from disk and draw it over the green background x100 y100
4) Save the result to disk as another png

Correct Smile, this would be the desired flow I would be seeking to get a feel for how this all works.

Sorry for my mixed terminology on 'pstreams'. Basically what I mean is to load the image from a file into a stream... but maybe I should be using a different term there. Sorry for any mixup Smile

The main thing I am looking for is the general ability to lay these images over in a blended fashion, so if the PNG has transparent properties, it looks proper over the layout Very Happy
Back to top
tic



Joined: 22 Apr 2007
Posts: 1365

PostPosted: Fri Jun 27, 2008 4:56 pm    Post subject: Reply with quote

Try example 6 on the 1st post. I recommend you try all the other 5 examples also however as it will give you a better understanding of how it all works.

Note for everyone: Always ensure you have the latest Gdip.ahk!

Hope this helps Smile
Back to top
View user's profile Send private message
LuceGold2
Guest





PostPosted: Sat Jun 28, 2008 7:33 am    Post subject: Reply with quote

Thank you Tic for this pro documentation and learning examples, it has been very helpful in my knowledge learning of this. Have a very good weekend!

Cheers!, Steven
Back to top
PixelVision



Joined: 28 Jun 2008
Posts: 4
Location: NIECE, FRANCE

PostPosted: Thu Jul 03, 2008 4:24 pm    Post subject: Reply with quote

Hi Tic! I have been working with your tutorials, and think this is best thing since sliced bread & you make it easy to follow.

May I pose one question? I am not so clear on all syntaxs. Basically, I want to take a 500x500 image, ... a green block will do.... and write a Blue border around it with a 2 pixel border thickness @ 50% transparency Smile

How can this be done efficiently? Smile Have a great day!

PixelVision
Back to top
View user's profile Send private message
tic



Joined: 22 Apr 2007
Posts: 1365

PostPosted: Thu Jul 03, 2008 5:55 pm    Post subject: Reply with quote

PixelVision wrote:
Hi Tic! I have been working with your tutorials, and think this is best thing since sliced bread & you make it easy to follow.

May I pose one question? I am not so clear on all syntaxs. Basically, I want to take a 500x500 image, ... a green block will do.... and write a Blue border around it with a 2 pixel border thickness @ 50% transparency Smile

How can this be done efficiently? Smile Have a great day!

PixelVision


Very Happy It makes me very happy that you have been able to follow the tutorials and will release a few more in time, but don't want to make them too hard too early

I need to be clear about some things....Is this the order of events you are wanting:

Take an exisiting image on disk
Draw a blue border with 50% opacity round it
Save that back to disk (overwrite/new file?)

I ask this because I am unsure when people ask questions whether they want to turn their idea into a file on disk, or show it in a gui

tic
Back to top
View user's profile Send private message
PixelVision



Joined: 28 Jun 2008
Posts: 4
Location: NIECE, FRANCE

PostPosted: Fri Jul 04, 2008 1:00 am    Post subject: Reply with quote

Quote:
Very Happy It makes me very happy that you have been able to follow the tutorials and will release a few more in time, but don't want to make them too hard too early

I need to be clear about some things....Is this the order of events you are wanting:

Take an exisiting image on disk
Draw a blue border with 50% opacity round it
Save that back to disk (overwrite/new file?)

I ask this because I am unsure when people ask questions whether they want to turn their idea into a file on disk, or show it in a gui

tic



Quote:


*Take an exisiting image on disk
*Draw a blue border with 50% opacity round it
*Save that back to disk (overwrite/new file?)



Very cool! Smile That is basically what I would be hoping for (+ also to adjust the variable thickness of the border) Smile... as far as what to do with the bitmap, either way would work for demo purposes. I've figured out how to display or save bitmap data, following your standards,... so no worries Very Happy
Back to top
View user's profile Send private message
Trikster



Joined: 15 Jul 2007
Posts: 1194
Location: Enterprise, Alabama

PostPosted: Fri Jul 04, 2008 1:45 am    Post subject: Reply with quote

PixelVision, at least put some effort forth.

Code:
pToken       := Gdip_Startup() ; Startup Gdip

sOutput      := "MyBitmap.png" ; File to save bitmap to.

pBitmap      := Gdip_CreateBitmap(500, 500) ; Make a new bitmap, 500x500 pixels
pGraphics    := Gdip_GraphicsFromImage(pBitmap) ; Get graphics from pBitmap

hBrush       := Gdip_BrushCreateSolid(0x7F0000FF) ; Create a blue brush with 50% opacity
Gdip_FillRectangle(pGraphics, hBrush, 0, 0, 500, 500) ; Draw a rectangle in the bitmap, 500x500 pixels, of hBrush
Gdip_DeleteBrush(hBrush) ; Delete hBrush

hBrush       := Gdip_BrushCreateSolid(0xFF00FF00) ; Create a green brush with 100% opacity
Gdip_FillRectangle(pGraphics, hBrush, 2, 2, 496, 496) ; Draw a rectangle in the bitmap, 496x496 pixels, of hBrush
Gdip_DeleteBrush(hBrush) ; Delete hBrush

Gdip_SaveBitmapToFile(pBitmap, sOutput) ; Save pBitmap to sOutput

Gdip_Shutdown(pToken) ; Shutdown Gdip


This would have not been made had it not been for tic teaching me bin2hex conversion.
_________________
ScriptPad/~dieom/dieom/izwian2k7/Ian/God

Back to top
View user's profile Send private message
arunsonnet



Joined: 16 Feb 2008
Posts: 2

PostPosted: Fri Jul 04, 2008 3:58 am    Post subject: Error message Reply with quote

I'm getting this error message whenever i tried to use Gdip. I've tried only the examples given by tic.

Code:
Error at line 193 in #include file "F:\ahk\Gdip.ahk".


Line Text: ppvBits
Error: Default value not allowed with ByRef.


The program will exit.
Back to top
View user's profile Send private message Visit poster's website
tic



Joined: 22 Apr 2007
Posts: 1365

PostPosted: Fri Jul 04, 2008 11:21 am    Post subject: Re: Error message Reply with quote

arunsonnet wrote:
I'm getting this error message whenever i tried to use Gdip. I've tried only the examples given by tic.

Code:
Error at line 193 in #include file "F:\ahk\Gdip.ahk".


Line Text: ppvBits
Error: Default value not allowed with ByRef.


The program will exit.


I think you need to get the latest version of ahk

Trikster wrote:

This would have not been made had it not been for tic teaching me bin2hex conversion.


Heh...I think you mean dec2hex though Razz
You need to remember to dispose of everything you create though, like pBitmap, pGraphics and the 1st hBrush....also I think he wants to take an existing image from disk (like a photo or whatever):

Code:
#SingleInstance, Force
#NoEnv
SetBatchLines, -1
SetWorkingDir %A_ScriptDir%

; Uncomment if Gdip is not in your standard library directory
;#Include, Gdip.ahk

If !pToken := Gdip_Startup()
{
   MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
   ExitApp
}

InFile := "InImage.png", OutFile := "OutImage.png"
BorderWidth := 3

pBitmap := Gdip_CreateBitmapFromFile(InFile), G := Gdip_GraphicsFromImage(pBitmap)
Width := Gdip_GetImageWidth(pBitmap), Height := Gdip_GetImageHeight(pBitmap)

hPen := Gdip_CreatePen(0x7f0000ff, BorderWidth)
Gdip_DrawRectangle(G, hPen, BorderWidth//2, BorderWidth//2, Width-BorderWidth, Height-BorderWidth)
Gdip_DeletePen(hPen)

Gdip_SaveBitmapToFile(pBitmap, OutFile)

Gdip_DeleteGraphics(G), Gdip_DisposeImage(pBitmap), Gdip_Shutdown(pToken)
ExitApp
Return


...Although you are correct that all the information in the code above can be found in the examples Smile happy scripting...
Back to top
View user's profile Send private message
arunsonnet



Joined: 16 Feb 2008
Posts: 2

PostPosted: Fri Jul 04, 2008 3:09 pm    Post subject: Re: Error message Reply with quote

tic wrote:


I think you need to get the latest version of ahk



ya.. that worked . Thanks a lot.
Back to top
View user's profile Send private message Visit poster's website
tic



Joined: 22 Apr 2007
Posts: 1365

PostPosted: Fri Jul 04, 2008 5:09 pm    Post subject: Reply with quote

I have updated the library again with some new functions, most notably:

Code:
Gdip_DrawRoundedRectangle(pGraphics, pPen, x, y, w, h, r)
Gdip_FillRoundedRectangle(pGraphics, pBrush, x, y, w, h, r)


I will give examples how to use these soon (r is the radius of the rounded corner)

Also I have made a tiny mistake Embarassed The variable names hBrush and hPen should actually be pBrush and pPen respectively, as for example pBrush is actually a pointer to a brush object in memory and is not a handle. It's a very small mistake, but has just been changed in all files on the 1st post for correctness.
Back to top
View user's profile Send private message
Guest






PostPosted: Tue Jul 15, 2008 9:19 am    Post subject: Reply with quote

Tic! Hi, can you help me with a save issue?

... from reading your tutorials, I understand if I do something like

Code:


pBitmap := Gdip_CreateBitmap(100,100)
G := Gdip_GraphicsFromIMage(pBitmap)



.... I can do stuff with it, and later run this

Code:


GdipSaveBitmapToFile(pBitmap, "savedimage.png")



ok, so I clear on this. but what if I working with a display image in a gui

Code:


Gui, 1: -Caption +E0x80000 +ToolWindow
Gui, 1: Show, GRAPHIC

hwnd1 := WinExist()

hbm := CreateDIBSection(100,100)
hdc := CreateCompatibleDC()
obm := SelectObject(hdc, hbm)

G := Gdip_GraphicsFromHDC(hdc)

...
..

UpdateLayeredWindow(hwnd1, hdc, 0, 0, 100, 100)



I am confused, ... I get the stuff to display in GUI fine... but I want to modify above block of code, to let me save to PNG? I started writing this, and never really understood what I needed to do for me to write the pBitmap to a file. Sorry I am new, but can you see what I am missing concept wise? Smile

Great tutorials, & thank you for your time tic!!
Back to top
tic



Joined: 22 Apr 2007
Posts: 1365

PostPosted: Tue Jul 15, 2008 10:53 am    Post subject: Reply with quote

Sure.....when working with guis then you will need a hBitmap (hbm), this is a gdi bitmap, and not a gdi+ bitmap (pBitmap), as a gdi bitmap is required to update a gui. In order to save the gdi bitmap the same way we save the gdi+ bitmap, one simple method would be to get pBitmap from hbm.....so here is the 1st example from the front page, but with 3 extra lines (one of them used to dispose of the pBitmap created), allowing us to save a gdi bitmap to disk:

Code:
; gdi+ ahk tutorial 1 written by tic (Tariq Porter)
; Requires Gdip.ahk either in your Lib folder as standard library or using #Include
;
; Tutorial to draw a single ellipse and rectangle to the screen

#SingleInstance, Force
#NoEnv
SetBatchLines, -1

; Uncomment if Gdip.ahk is not in your standard library
#Include, Gdip.ahk

; 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 := 600, Height := 400

; 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
pBrush := 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, pBrush, 100, 50, 200, 300)

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

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

; 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, pBrush, 250, 80, 300, 200)

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


; 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)

pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
Gdip_SaveBitmapToFile(pBitmap, "file.bmp")
Gdip_DisposeImage(pBitmap)

; 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

;#######################################################################

Exit:
; gdi+ may now be shutdown on exiting the program
Gdip_Shutdown(pToken)
ExitApp
Return
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3, 4, 5, 6  Next
Page 2 of 6

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group