Sign in to follow this  
Followers 0
Burgs

Text on Image confusion

8 posts in this topic

Greetings,

I am trying to test placing some text on a graphic image. Using previous topics posted in the Forum I found along with examples in the AutoIT Help I have put together the following code:

; Initialize GDI+ library
_GDIPlus_StartUp()
$hImage = _GDIPlus_ImageLoadFromFile("C:\Program Files\AAC\Image2.gif")
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
$hBrush2 = _GDIPlus_BrushCreateSolid(0xFF00007F)
$hFormat = _GDIPlus_StringFormatCreate()
_GDIPlus_StringFormatSetAlign($hFormat, 0)
$hFamily = _GDIPlus_FontFamilyCreate("Arial")
$hFont = _GDIPlus_FontCreate($hFamily, 48, 0, 2)
$tLayout = _GDIPlus_RectFCreate(140, 110, 100, 200)

$result = _GDIPlus_GraphicsDrawStringEx($hGraphic, "Hello world", $hFont, $tLayout, $hFormat, $hBrush2)
ConsoleWrite(" result Is: " & $result & " ")
; Save image
_GDIPlus_ImageSaveToFile ( $hImage, "C:\Program Files\AAC\Saved.gif")
; Free resources
_GDIPlus_BrushDispose ( $hBrush2 )
_GDIPlus_StringFormatDispose ( $hFormat )
_GDIPlus_FontDispose ( $hFont )
_GDIPlus_FontFamilyDispose ( $hFamily )
_GDIPlus_GraphicsDispose ( $hGraphic )
_GDIPlus_ImageDispose ( $hImage )
_GDIPlus_ShutDown ( )

This does not work...the returned result is "False"...I cannot understand why since this code is mostly from the examples given in Help. The problem seems to be with the "$tLayout = _GDIPlus_RectFCreate()" (doesn't return a handle) however this seems hard to believe since it is simply the (optional) dimensions of a rectangle...alternatively I also tried using "_GDIPlus_GraphicsDrawString()" as well to get around that:

$result = _GDIPlus_GraphicsDrawString($hGraphic, "Hello world", 140, 110)

This also is not functioning...can somebody explain why this code (again basically taken from the AutoIT Help section) is not functioning...? The image I am attempting to place text onto is 640 x 400 (.gif) so the dimensions are well within that image file...thanks in advance.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

This may help you

#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>
#include <WinAPI.au3>

_Main('C:Program FilesAACImage2.gif')

Func _Main($hBitmap)

; Initialize GDI+ library
_GDIPlus_Startup()

; Capture full screen

$hBitmap=_GDIPlus_ImageLoadFromFile($hBitmap)
; Draw one image in another
$hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap)
If $hGraphics=-1 Then ConsoleWrite('Error 1')
If Not _GDIPlus_GraphicsDrawString($hGraphics, "Hello world", 10, 20,'Arial',40) Then ConsoleWrite('Error 2')


; Save resultant image
If Not _GDIPlus_ImageSaveToFile($hBitmap, @MyDocumentsDir & "GDIPlus_Imagex.jpg") Then ConsoleWrite('Error 3')

; Clean up resources
_WinAPI_DeleteObject($hBitmap)

; Shut down GDI+ library
_GDIPlus_Shutdown()

EndFunc ;==>_Main
Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hello,

Thanks for your response. I copied your code into a new AutoIT script and ran it...I got the "Error 2" message however my saved .jpg file did not have any text written on it. To make it easier for me I re-wrote your code into:

#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>
#include <WinAPI.au3>

_Main('C:Program FilesAACImage2.gif')

Func _Main($hBitmap)
; Initialize GDI+ library
_GDIPlus_Startup()
; Capture full screen
$hBitmap = _GDIPlus_ImageLoadFromFile($hBitmap)
; Draw one image in another
$hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap)
if $hGraphics = -1 Then
ConsoleWrite('Error 1')
Exit
EndIf

$result = _GDIPlus_GraphicsDrawString($hGraphics, "Hello world", 10, 20, 'Arial', 40)
ConsoleWrite(" $result Is: " & $result & " ")

; Save resultant image
_GDIPlus_ImageSaveToFile($hBitmap, "C:Program FilesAACSaved.jpg")

; Clean up resources
_WinAPI_DeleteObject($hBitmap)

; Shut down GDI+ library
_GDIPlus_Shutdown()
EndFunc ;==>_Main

However still the same result. My "saved.jpg" does not have any text written on it and my ConsoleWrite is "$result = False"...this is incredibly frustrating...

Edited by Burgs

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Therez maybe some error in your Image or StringDraw Functions

Try to change the Image orelse please upload the image

I will try it with my Computer

Note : it is assumed that you use the latest version of Autoit , I'm having the latest Beta

BTW : I'm having Win7 32Bit

I did got successful results

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

Hello,

I have tried it with 2 different test images...see attached...

I am using WinXP Pro, with AutoIT v3.3.8.0

Share this post


Link to post
Share on other sites

made a good example for this.

Search the forums, there's many examples.


Things that I've done..

Icon Resource Editor: icon resource editor 

AutoIt Piano: a piano

AutoIt Unlocker: unlocks files when you want to delete them

Colorful tooltips: a wrapper for the tool tips UDF

Rouge GoogleBot: a full screen animation

ASciTE text editor: a text editor written in autoit

Warning: Posts by this user are subject to change or may disappear without notice.

Share this post


Link to post
Share on other sites

Thanks for that post, after playing around a bit with it I made some progress...

Share this post


Link to post
Share on other sites

All the _GDIPlus_ functions that do not work need "#include <GDIPlus.au3>" at the beginning of the script.

This works for me.

#include <GDIPlus.au3>
; Initialize GDI+ library
_GDIPlus_Startup()
$hImage = _GDIPlus_ImageLoadFromFile(@WindowsDir & "WebWallpaperAutumn.jpg")
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
$hBrush2 = _GDIPlus_BrushCreateSolid(0xFF00007F)
$hFormat = _GDIPlus_StringFormatCreate()
_GDIPlus_StringFormatSetAlign($hFormat, 0)
$hFamily = _GDIPlus_FontFamilyCreate("Arial")
$hFont = _GDIPlus_FontCreate($hFamily, 48, 1)
$tLayout = _GDIPlus_RectFCreate(140, 110, 0, 0)
$sString = "       Hello" & @CRLF & "folks of world"
$aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat)
$result = _GDIPlus_GraphicsDrawStringEx($hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrush2)
ConsoleWrite(" result Is: " & $result & " ")
; Save image
_GDIPlus_ImageSaveToFile($hImage, @ScriptDir & "Saved.gif")
; Free resources
_GDIPlus_BrushDispose($hBrush2)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0