seadoggie01 Posted November 3, 2019 Share Posted November 3, 2019 (edited) I'm trying to add a title above an image and a subtitle below an image and save it as a file. I'm not sure of the best approach for this, so I was thinking I should create a create a blank graphic, draw the image on it in the middle, and add the text above and below the image. I can't seem to make the Image's size actually change, however. Here's what I have... Spoiler expandcollapse popupFunc ConvertPngBmp($sPngPath, $sTitle, $sHover) _GDIPlus_Startup() ; GDI+ Variables to destroy later... Local $hImage_Desktop, $hFont, $hFamily, $hFormat, $hBrush, $hBrushClear, $hGraphicsBack, $hGraphics, $hImage_Real $hImage_Desktop = _GDIPlus_ImageLoadFromFile($sPngPath) ; Load the image (used to make desktop-sized image) $hImage_Real = _GDIPlus_ImageClone($hImage_Desktop) ; Copy the object (used for actual image) $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage_Desktop) ; Get the graphics Local $iImgHeight = _GDIPlus_ImageGetHeight($hImage_Desktop) ; Get the height Local $iImgWidth = _GDIPlus_ImageGetWidth($hImage_Desktop) ; Get the width Local $iImgTop = (@DesktopHeight - $iImgHeight)/2 ; Calculate top Local $iImgLeft = (@DesktopWidth - $iImgWidth)/2 ; Calculate left _GDIPlus_ImageResize($hImage_Desktop, @DesktopWidth, @DesktopHeight) ; Resize to desktop Local $aiTemp = _GDIPlus_ImageGetDimension($hImage_Desktop) ; Check the size ConsoleWrite($aiTemp[0] & @CRLF) ConsoleWrite($aiTemp[1] & @CRLF) _GDIPlus_GraphicsClear($hGraphics, "0xFFFFFFFF") ; Clear the image (white) _GDIPlus_GraphicsDrawImage($hGraphics, $hImage_Real, $iImgTop, $iImgLeft) ; Draw the real image onto the graphics ;------- String related... skip for now, mostly works, just wrong location ------------------------------------------- ;~ Local $hRect = _GDIPlus_RectFCreate($iImgTop - 15, 0, @DesktopWidth, 50) ; Create a rectange ;~ $hFormat = _GDIPlus_StringFormatCreate() ; Create text format ;~ _GDIPlus_StringFormatSetAlign($hFormat, 1) ; Center the text ;~ $hFamily = _GDIPlus_FontFamilyCreate("Arial") ; Create the Font Family ;~ $hFont = _GDIPlus_FontCreate($hFamily, 15) ; Create the Font ;~ $hBrush = _GDIPlus_BrushCreateSolid() ; Create the brush ;~ _GDIPlus_GraphicsDrawStringEx($hGraphics, $sTitle, $hFont, $hRect, $hFormat, $hBrush) ; Draw the string ;--------------------------------------------------------------------------------------------------------------------- Local $sNewPath = StringReplace($sPngPath, ".png", ".bmp") _GDIPlus_ImageSaveToFile($hImage_Desktop, $sNewPath) ; Save ; Delete all the objects/handles/stuff GDIPlusUnRegister($hImage_Desktop, $hFont, $hFamily, $hFormat, $hBrush, $hBrushClear, $hGraphicsBack, $hGraphics, $hImage_Real) ; The bmp's path Return $sNewPath EndFunc Also, please note that this is my first time using GDI+, so there may be something completely wrong here, but I'm trying Edit: I removed all my error checks before posting, but none of the functions throw an error. The image saved looks like the old image offset diagonally and cropped to the original size. Edited November 3, 2019 by seadoggie01 All my code provided is Public Domain... but it may not work. Use it, change it, break it, whatever you want. Spoiler My Humble Contributions:Personal Function Documentation - A personal HelpFile for your functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types Link to comment Share on other sites More sharing options...
Nine Posted November 3, 2019 Share Posted November 3, 2019 Works well for me : #include <GDIPlus.au3> Example(4) ; pixel ratio should be identical horizontal and vertical, otherwise it will be distorded Func Example($iRatio) _GDIPlus_Startup() Local $hBitmap = _GDIPlus_BitmapCreateFromFile("Trav.png") Local $iW = _GDIPlus_ImageGetWidth ($hBitmap)*$iRatio, $iH = _GDIPlus_ImageGetHeight ($hBitmap)*$iRatio Local $hBitmap_Scaled = _GDIPlus_ImageResize($hBitmap, $iW, $iH) ; respecting pixel ratio Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap_Scaled) _GDIPlus_GraphicsDrawString($hGraphics, "Hello", $iW/2 - 20, 10, "Arial", 14) _GDIPlus_ImageSaveToFile ($hBitmap_Scaled, "Trav.bmp") _GDIPlus_GraphicsDispose($hGraphics) _GDIPlus_BitmapDispose($hBitmap) _GDIPlus_BitmapDispose($hBitmap_Scaled) _GDIPlus_Shutdown() EndFunc ;==>Example “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
seadoggie01 Posted November 3, 2019 Author Share Posted November 3, 2019 Oh, thank you! I failed to realize that _GDIPlus_ImageResize returned a handle to a new image and that caused it to fail. All my code provided is Public Domain... but it may not work. Use it, change it, break it, whatever you want. Spoiler My Humble Contributions:Personal Function Documentation - A personal HelpFile for your functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types 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