HurleyShanabarger Posted January 21, 2020 Posted January 21, 2020 Hi guys, I want to create a graphic set the background color On the background I want to draw a transparent PNG file Write a text below the image from the PNG file Width of the resulting graphic should be determined by the width of the PNG file Height of the resulting graphic should be determined by the height of the PNG file plus the height of the text Here are my question: How do I get the size (width/height) of the string? How do I convert the graphic to an image and save it to a file? How do I make this without using the GUI? Here is what I have so far: expandcollapse popup#Region Includes #include <GDIPlus.au3> #include <GUIConstantsEx.au3> #EndRegion Includes #Region Main _Main("Test") Func _Main($sString) ; Startup _GDIPlus_Startup() ; Load logo Local $hLogo = _GDIPlus_BitmapCreateFromFile(@DesktopDir & "\Image.png") Local $aLogo = _GDIPlus_ImageGetDimension($hLogo) ; Create GUI Local $hGUI = GUICreate("Image creator", $aLogo[0], $aLogo[1] + 90) GUISetState(@SW_SHOW) ; Create brush Local $hBrush_BGC = _GDIPlus_BrushCreateSolid(0xFF828282) Local $hBrush_TXT = _GDIPlus_BrushCreateSolid(0xFF004287) ; Create graphics Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) _GDIPlus_GraphicsClear($hGraphics) _GDIPlus_GraphicsFillRect($hGraphics, 0, 0, $aLogo[0], $aLogo[1] * 2, $hBrush_BGC) ; Create font Local $hFontFamily = _GDIPlus_FontFamilyCreate("Courier New") Local $hFont = _GDIPlus_FontCreate($hFontFamily, 30, 1) Local $hString = _GDIPlus_StringFormatCreate() _GDIPlus_StringFormatSetAlign($hString, 2) _GDIPlus_StringFormatSetLineAlign($hString, 0) ; Create layout, draw and save to file Local $hLayout = _GDIPlus_RectFCreate(0, $aLogo[1], $aLogo[0]) _GDIPlus_GraphicsDrawStringEx($hGraphics, $sString, $hFont, $hLayout, $hString, $hBrush_TXT) _GDIPlus_GraphicsDrawImage($hGraphics, $hLogo, 0, 0) Do Sleep(10) Until GUIGetMsg() = $GUI_EVENT_CLOSE ; Dispose all _GDIPlus_BrushDispose($hBrush_BGC) _GDIPlus_BrushDispose($hBrush_TXT) _GDIPlus_FontFamilyDispose($hFontFamily) _GDIPlus_FontDispose($hFont) _GDIPlus_StringFormatDispose($hString) _GDIPlus_GraphicsDispose($hGraphics) _GDIPlus_Shutdown() EndFunc ;==>_Main #EndRegion Main
InnI Posted January 21, 2020 Posted January 21, 2020 expandcollapse popup#Region Includes #include <GDIPlus.au3> #EndRegion Includes #Region Main _Main("Test") Func _Main($sString) ; Startup _GDIPlus_Startup() ; Load logo Local $hLogo = _GDIPlus_BitmapCreateFromFile(@DesktopDir & "\Image.png") Local $aLogo = _GDIPlus_ImageGetDimension($hLogo) ; Create font Local $hFontFamily = _GDIPlus_FontFamilyCreate("Courier New") Local $hFont = _GDIPlus_FontCreate($hFontFamily, 30, 1) Local $hString = _GDIPlus_StringFormatCreate() ; Calculate rect Local $hGraph = _GDIPlus_GraphicsCreateFromHWND(0) Local $tLayout = _GDIPlus_RectFCreate() Local $aInfo = _GDIPlus_GraphicsMeasureString($hGraph, $sString, $hFont, $tLayout, $hString) ConsoleWrite($aInfo[0].X & ":" & $aInfo[0].Y & ":" & $aInfo[0].Width & ":" & $aInfo[0].Height & @CRLF) $aInfo[0].Y = $aLogo[1] Local $iWidth = ($aLogo[0] > $aInfo[0].Width) ? $aLogo[0] : $aInfo[0].Width $aInfo[0].Width = $iWidth _GDIPlus_StringFormatSetAlign($hString, 2) ; Create buffer Local $hImage = _GDIPlus_BitmapCreateFromScan0($iWidth, $aLogo[1] + Ceiling($aInfo[0].Height)) Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage) ; Draw and save _GDIPlus_GraphicsClear($hGraphics, 0xFF828282) Local $hBrush_TXT = _GDIPlus_BrushCreateSolid(0xFF004287) _GDIPlus_GraphicsDrawImage($hGraphics, $hLogo, 0, 0) _GDIPlus_GraphicsDrawStringEx($hGraphics, $sString, $hFont, $aInfo[0], $hString, $hBrush_TXT) _GDIPlus_ImageSaveToFile($hImage, "Logo.png") ; Dispose all _GDIPlus_BrushDispose($hBrush_TXT) _GDIPlus_FontFamilyDispose($hFontFamily) _GDIPlus_FontDispose($hFont) _GDIPlus_StringFormatDispose($hString) _GDIPlus_GraphicsDispose($hGraph) _GDIPlus_GraphicsDispose($hGraphics) _GDIPlus_ImageDispose($hLogo) _GDIPlus_ImageDispose($hImage) _GDIPlus_Shutdown() EndFunc ;==>_Main #EndRegion Main
HurleyShanabarger Posted January 27, 2020 Author Posted January 27, 2020 Thank you very much - I learned some things with your solution.
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