[Help] [Gdip] How to add Caption to a Picture ?

Get help with using AutoHotkey and its commands and hotkeys
titep
Posts: 31
Joined: 14 Nov 2015, 09:01

[Help] [Gdip] How to add Caption to a Picture ?

25 Jun 2017, 06:54

I am practicing using gdip library by tic.

Original picture :

Image

How can I add an text OUTSIDE and BELOW THE BOTTOM of an image like this :

Image

Thank you in advance!
User avatar
noname
Posts: 509
Joined: 19 Nov 2013, 09:15

Re: [Help] [Gdip] How to add Caption to a Picture ?

25 Jun 2017, 10:01

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



soundcloud.com/user-32706894
titep
Posts: 31
Joined: 14 Nov 2015, 09:01

Re: [Help] [Gdip] How to add Caption to a Picture ?

25 Jun 2017, 10:33

Thank you, noname

Your code is really clean and work like a charm !

How did you learn gdip ?
User avatar
Capn Odin
Posts: 1308
Joined: 23 Feb 2016, 19:45
Location: Denmark

Re: [Help] [Gdip] How to add Caption to a Picture ?

25 Jun 2017, 13:03

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
img1.png (1.24 MiB) Viewed 1404 times
img2.png
img2.png (1.25 MiB) Viewed 1404 times
Please excuse my spelling I am dyslexic.
User avatar
noname
Posts: 509
Joined: 19 Nov 2013, 09:15

Re: [Help] [Gdip] How to add Caption to a Picture ?

26 Jun 2017, 01:59

How did you learn gdip ?
By trying out all tutorials / examples from Tic's post here:
https://autohotkey.com/board/topic/2944 ... 45-by-tic/ ;)
soundcloud.com/user-32706894
titep
Posts: 31
Joined: 14 Nov 2015, 09:01

Re: [Help] [Gdip] How to add Caption to a Picture ?

26 Jun 2017, 03:06

@Capn Odin:

The following happens when the text is too long : :(

Image
User avatar
Capn Odin
Posts: 1308
Joined: 23 Feb 2016, 19:45
Location: Denmark

Re: [Help] [Gdip] How to add Caption to a Picture ?

26 Jun 2017, 05:07

You will have to manually brake the line using `n
Please excuse my spelling I am dyslexic.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Dom_Christo, Franktic, Google [Bot], mikeyww, stiuna, TAC109 and 34 guests