I am practicing using gdip library by tic.
Original picture :
How can I add an text OUTSIDE and BELOW THE BOTTOM of an image like this :
Thank you in advance!
[Help] [Gdip] How to add Caption to a Picture ?
Re: [Help] [Gdip] How to add Caption to a Picture ?
example ( include the gdip lib if not in standard lib directory ) :
Code: Select all
SetWorkingDir, %A_ScriptDir%
SetBatchLines, -1
If !pToken := Gdip_Startup()
{
MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
ExitApp
}
sFile=%A_ScriptDir%\image.png
oFile=%A_ScriptDir%\image_caption.png
pBitmap :=Gdip_CreateBitmapFromFile(sFile)
Gdip_GetImageDimensions(pBitmap, w,h)
caption_height:=30
hc:=h+caption_height
bm:=Gdip_CreateBitmap(w,hc) ; create bitmap with added height
pGraphics := Gdip_GraphicsFromImage(bm)
Gdip_GraphicsClear(pGraphics, 0xff000000)
caption:=" Caption= Earth from Moon"
color:="cffc0c0c0"
font:="tahoma"
If !hFamily := Gdip_FontFamilyCreate(Font)
{
MsgBox, font %font% not Exist on this system ExitApp
ExitApp
}
options= x0 y%h% w%w% h%caption_height% %color% vCenter Center r4 s18
Gdip_TextToGraphics(pGraphics, caption, Options, font)
Gdip_DrawImage(pGraphics, pBitmap, 0,0,w,h)
Gdip_SaveBitmapToFile(bm, oFile)
; ---------------- show picture ---------------- ----------------
Gui, add,picture,,%ofile%
gui, show, autosize
sleep 10000
; ---------------- show picture ---------------- ----------------
Gdip_DisposeImage(pBitmap)
Gdip_DisposeImage(bm)
Gdip_DeleteGraphics(pGraphics)
Gdip_Shutdown(pToken)
exitapp
Re: [Help] [Gdip] How to add Caption to a Picture ?
Thank you, noname
Your code is really clean and work like a charm !
How did you learn gdip ?
Your code is really clean and work like a charm !
How did you learn gdip ?
Re: [Help] [Gdip] How to add Caption to a Picture ?
Code: Select all
if(!pToken := Gdip_Startup()) {
MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
ExitApp
}
Gdip_SaveBitmapToFile(Caption("Image Caption : I'm Firefox", "fox.jpg"), "img1.png")
Gdip_SaveBitmapToFile(Caption("Image Caption: Firefox`nused Fire Spin", "fox.jpg", "0xffff0000", "0x00ffffff", , , "Bold"), "img2.png")
Gdip_Shutdown(pToken)
Caption(Str, sFile, colour := "0xffffffff", bgColour := "0xff000000", Font := "Impact", fSize := 0.05, textOptions := ""){
pImage := Gdip_CreateBitmapFromFile(sFile)
Gdip_GetImageDimensions(pImage, width, height)
fSize := fSize > 1 ? fSize : height * fSize
Style := 0, Styles := "Regular|Bold|Italic|BoldItalic|Underline|Strikeout"
Loop, Parse, Styles, |
{
if RegExMatch(textOptions, "\b" A_loopField)
Style |= (A_LoopField != "StrikeOut") ? (A_Index-1) : 8
}
chrSize := CharHeight(pImage, Str, Font, fSize, Style)
pBitmap := Gdip_CreateBitmap(width, height + chrSize)
G := Gdip_GraphicsFromImage(pBitmap)
Gdip_GraphicsClear(G, bgColour+0)
Gdip_DrawImage(G, pImage)
Gdip_SetSmoothingMode(G, 4)
Options := "x" width / 2 " y" height " h" chrSize " Centre c" SubStr(colour, 3) " r4 s" fSize " " textOptions
Gdip_TextToGraphics(G, Str, Options, Font)
Gdip_DeleteGraphics(G)
Gdip_DisposeImage(pImage)
Return pBitmap
}
CharHeight(pImage, Str, Font, fSize, Style) {
G := Gdip_GraphicsFromImage(pImage)
hFamily := Gdip_FontFamilyCreate(Font)
hFont := Gdip_FontCreate(hFamily, fSize, Style)
hFormat := Gdip_StringFormatCreate(0x00000002)
chrSize := StrSplit(Gdip_MeasureString(G, Str, hFont, hFormat, RectF), "|")
Gdip_DeleteGraphics(G)
Gdip_DeleteStringFormat(hFormat)
Gdip_DeleteFont(hFont)
Gdip_DeleteFontFamily(hFamily)
Return chrSize[3]
}
- Attachments
-
- img1.png (1.24 MiB) Viewed 1716 times
-
- img2.png (1.25 MiB) Viewed 1716 times
Please excuse my spelling I am dyslexic.
Re: [Help] [Gdip] How to add Caption to a Picture ?
By trying out all tutorials / examples from Tic's post here:How did you learn gdip ?
https://autohotkey.com/board/topic/2944 ... 45-by-tic/
Re: [Help] [Gdip] How to add Caption to a Picture ?
@Capn Odin:
The following happens when the text is too long :
The following happens when the text is too long :
Re: [Help] [Gdip] How to add Caption to a Picture ?
You will have to manually brake the line using `n
Please excuse my spelling I am dyslexic.
Who is online
Users browsing this forum: lirouxtm and 323 guests