alien4u Posted May 2, 2016 Share Posted May 2, 2016 (edited) I'm creating an small script that will get Text from User Input or From a File and write that text in an Image for that I'm using _GDIPlus and _GDIPlus_RectFCreate(). The problem is the text length will vary and I would like to adjust the Text font size using _GDIPlus_FontCreate() to make it fit on the _GDIPlus_RectFCreate(). Someone could help me with this? there is a way to test if an specific text with an specific length and specific font size will fit on a predefined _GDIPlus_RectFCreate()? Here some example code: #include <GDIPlus.au3> _GDIPlus_Startup() $sText = "Example very very long string" $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Clas1.jpg") $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage) $dimen = _GDIPlus_ImageGetDimension($hImage) Local $hBrush = _GDIPlus_BrushCreateSolid(0xFF009900) Local $hFamily = _GDIPlus_FontFamilyCreate("Arial") Local $hFont = _GDIPlus_FontCreate($hFamily, 20) Local $hLayout = _GDIPlus_RectFCreate(0, 0, $dimen[0], $dimen[1]) Local $hStringFormat = _GDIPlus_StringFormatCreate() _GDIPlus_GraphicsDrawStringEx($hGraphic, $sText , $hFont, $hLayout, $hStringFormat, $hBrush) _GDIPlus_ImageSaveToFile($hImage, @ScriptDir & "\GDIPlus_ImageWithText.jpg") ; Clean up resources _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_ImageDispose($hImage) _GDIPlus_Shutdown() Regards Alien. Edited May 2, 2016 by alien4u Link to comment Share on other sites More sharing options...
KaFu Posted May 2, 2016 Share Posted May 2, 2016 (edited) How about _GDIPlus_GraphicsMeasureString()? Edit: Another way might be using _WinAPI_GetTextExtentPoint32(). Edited May 2, 2016 by KaFu OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2024-Oct-13) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Oct-13) HMW - Hide my Windows (2024-Oct-19) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2024-Oct-20) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
UEZ Posted May 2, 2016 Share Posted May 2, 2016 You have to measure the font size by testing testing. #include <GDIPlus.au3> _GDIPlus_Startup() $sText = "Example very very long string" $hImage = _GDIPlus_BitmapCreateFromScan0(300, 200) $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage) $dimen = _GDIPlus_ImageGetDimension($hImage) Local $hBrush = _GDIPlus_BrushCreateSolid(0xFF009900) Local $hFamily = _GDIPlus_FontFamilyCreate("Arial") Local $hLayout = _GDIPlus_RectFCreate(0, 0, $dimen[0], $dimen[1]) Local $hStringFormat = _GDIPlus_StringFormatCreate() $fSize = 50 Do $hFont = _GDIPlus_FontCreate($hFamily, $fSize) $aMeasure = _GDIPlus_GraphicsMeasureString($hGraphic, $sText, $hFont, $hLayout, $hStringFormat) If $aMeasure[2] = 1 Then ExitLoop $fSize -= 1 _GDIPlus_FontDispose($hFont) Until False ConsoleWrite("Font size: " & $fSize & @CRLF) _GDIPlus_GraphicsDrawStringEx($hGraphic, $sText , $hFont, $hLayout, $hStringFormat, $hBrush) _GDIPlus_ImageSaveToFile($hImage, @ScriptDir & "\GDIPlus_ImageWithText.jpg") ; Clean up resources _GDIPlus_BrushDispose($hBrush) _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_ImageDispose($hImage) _GDIPlus_FontDispose($hFont) _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_Shutdown() ShellExecute(@ScriptDir & "\GDIPlus_ImageWithText.jpg") alien4u 1 Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
alien4u Posted May 2, 2016 Author Share Posted May 2, 2016 (edited) Thank you @UEZ that code show me exactly how to do it. I modify the code to use $aMeasure[1] - The number of characters that actually fit into the layout rectangle and use StringLen to know the number of characters. The final code base on your code is this one: expandcollapse popup#include <GDIPlus.au3> #include <String.au3> _GDIPlus_Startup() $sText = "Example very very long string then keep and keep and keep and keep writing until the font get reduced this is the way I need it" $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Clas1.jpg") $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage) $dimen = _GDIPlus_ImageGetDimension($hImage) $iTextLen = StringLen($sText) Local $hBrush = _GDIPlus_BrushCreateSolid(0xFF009900) Local $hFamily = _GDIPlus_FontFamilyCreate("Arial") Local $hLayout = _GDIPlus_RectFCreate(0, $dimen[1] - 280, $dimen[0], $dimen[1] - ($dimen[1] - 280)) Local $hStringFormat = _GDIPlus_StringFormatCreate() $fSize = 50 Do $hFont = _GDIPlus_FontCreate($hFamily, $fSize) $aMeasure = _GDIPlus_GraphicsMeasureString($hGraphic, $sText, $hFont, $hLayout, $hStringFormat) If $aMeasure[1] = $iTextLen Then ExitLoop $fSize -= 1 _GDIPlus_FontDispose($hFont) Until False ConsoleWrite("Font size: " & $fSize & @CRLF) $fSize = $fSize - 5 $hFont = _GDIPlus_FontCreate($hFamily, $fSize) _GDIPlus_GraphicsDrawStringEx($hGraphic, $sText , $hFont, $hLayout, $hStringFormat, $hBrush) _GDIPlus_ImageSaveToFile($hImage, @ScriptDir & "\GDIPlus_ImageWithText.jpg") ; Clean up resources _GDIPlus_BrushDispose($hBrush) _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_ImageDispose($hImage) _GDIPlus_FontDispose($hFont) _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_Shutdown() ShellExecute(@ScriptDir & "\GDIPlus_ImageWithText.jpg") Regards Alien. Edited May 2, 2016 by alien4u Parsix 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now