Jump to content

Using GDIPlus Creating a button with text and saving


Go to solution Solved by Andreik,

Recommended Posts

Why can't I seem to save a png rounded rectangle shaped button with the font color red? For some reason it's showing black.  Any GDIplus experts?

 

Thanks!

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Example()

Func Example()
    Local $hGUI, $hGraphics, $hPen, $hBrush, $hPath, $iWidth = 200, $iHeight = 100, $iCornerRadius = 20
    Local $sOutputFilePath = "myfile.png" ; Save as PNG instead of JPEG

    ; Initialize GDI+
    _GDIPlus_Startup()

    ; Create a GUI
    $hGUI = GUICreate("Rounded Rectangle Example", $iWidth, $iHeight)
    GUISetState(@SW_SHOW)

    ; Create a graphics object
    $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI)

    ; Create a pen for drawing the outline of the rounded rectangle
    $hPen = _GDIPlus_PenCreate(0xFF000000, 3)

    ; Create a brush for filling the rounded rectangle with blue color
    $hBrush = _GDIPlus_BrushCreateSolid(0xFF0000FF) ; Blue color Background(0xFF0000FF)

    ; Create a path for the rounded rectangle
    $hPath = _GDIPlus_PathCreate()
    _GDIPlus_PathAddBezier($hPath, 0, $iCornerRadius / 2, 0, 0, $iCornerRadius / 2, 0, 0, 0) ; Top-left corner
    _GDIPlus_PathAddBezier($hPath, $iWidth - $iCornerRadius / 2, 0, $iWidth, 0, $iWidth, $iCornerRadius / 2, $iWidth, 0) ; Top-right corner
    _GDIPlus_PathAddBezier($hPath, $iWidth, $iHeight - $iCornerRadius / 2, $iWidth, $iHeight, $iWidth - $iCornerRadius / 2, $iHeight, $iWidth, $iHeight) ; Bottom-right corner
    _GDIPlus_PathAddBezier($hPath, $iCornerRadius / 2, $iHeight, 0, $iHeight, 0, $iHeight - $iCornerRadius / 2, 0, $iHeight) ; Bottom-left corner
    _GDIPlus_PathCloseFigure($hPath)

    ; Create a bitmap and draw the rounded rectangle on it
    Local $hBmp = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $hGraphics)
    Local $hBmpGraphics = _GDIPlus_ImageGetGraphicsContext($hBmp)

    ; Fill the path with the blue color
    _GDIPlus_GraphicsFillPath($hBmpGraphics, $hPath, $hBrush)

    ; Draw the outline of the rounded rectangle using the pen
    _GDIPlus_GraphicsDrawPath($hBmpGraphics, $hPath, $hPen)

    ; Add the text "button" to the center of the rectangle
    Local $hFont = _GDIPlus_FontCreate("Arial", 24)
    Local $tLayout = _GDIPlus_RectFCreate(0, 0, $iWidth, $iHeight)
    Local $hFormat = _GDIPlus_StringFormatCreate()
    _GDIPlus_StringFormatSetAlign($hFormat, 1) ; Center alignment
    _GDIPlus_StringFormatSetLineAlign($hFormat, 1) ; Center line alignment

    $hBrushText = _GDIPlus_BrushCreateSolid(0xFFFF0000)
    ;_GDIPlus_GraphicsDrawStringEx($hBmpGraphics, "button", $hFont, $tLayout, $hFormat, $hBrushText)
    _GDIPlus_GraphicsDrawString($hBmpGraphics, "button", 0, 0, "Arial", 24, 0xFFFF0000)

    ; Clean up font and format objects
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_StringFormatDispose($hFormat)

    ; Save the image to a file
    Sleep(700)
    _GDIPlus_ImageSaveToFile($hBmp, $sOutputFilePath)

    ; Release resources
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_PathDispose($hPath)
    _GDIPlus_GraphicsDispose($hBmpGraphics)
    _GDIPlus_BitmapDispose($hBmp)
    _GDIPlus_GraphicsDispose($hGraphics)

    ; Main loop
    ;Do
    ;Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ; Clean up GDI+
    _GDIPlus_Shutdown()
    ShellExecute($sOutputFilePath) ;open bitmap with default app
EndFunc

 

Link to comment
Share on other sites

  • Solution

Something like this?

#include <GDIPlus.au3>

Global $iWidth = 200, $iHeight = 100, $iCornerRadius = 20

_GDIPlus_Startup()
Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap)
Local $hFontFamily = _GDIPlus_FontFamilyCreate('Arial')
Local $hFont = _GDIPlus_FontCreate($hFontFamily, 24)
Local $tLayout = _GDIPlus_RectFCreate(0, 0, $iWidth, $iHeight)
Local $hFormat = _GDIPlus_StringFormatCreate()
Local $hBrush = _GDIPlus_BrushCreateSolid(0xFFFF0000)
Local $hBrush2 = _GDIPlus_BrushCreateSolid(0xFF000080)
_GDIPlus_StringFormatSetAlign($hFormat, 1)
_GDIPlus_StringFormatSetLineAlign($hFormat, 1)

; UEZ - round corners
; https://www.autoitscript.com/forum/topic/188050-solved-gdi-rounded-corners-on-a-image/#comment-1350513
Local $hPath = _GDIPlus_PathCreate()
_GDIPlus_PathAddArc($hPath, $iWidth - ($iCornerRadius * 2), 0, $iCornerRadius * 2, $iCornerRadius * 2, 270, 90)
_GDIPlus_PathAddArc($hPath, $iWidth - ($iCornerRadius * 2), $iHeight - ($iCornerRadius * 2), $iCornerRadius * 2, $iCornerRadius * 2, 0, 90)
_GDIPlus_PathAddArc($hPath, 0, $iHeight - ($iCornerRadius * 2), $iCornerRadius * 2, $iCornerRadius * 2, 90, 90)
_GDIPlus_PathAddArc($hPath, 0, 0, $iCornerRadius * 2, $iCornerRadius * 2, 180, 90)
_GDIPlus_PathCloseFigure($hPath)

_GDIPlus_GraphicsFillPath($hGraphics, $hPath, $hBrush2)
_GDIPlus_GraphicsDrawStringEx($hGraphics, 'Button', $hFont, $tLayout, $hFormat, $hBrush)
_GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & '\output.png')

_GDIPlus_PathDispose($hPath)
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_BrushDispose($hBrush2)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFontFamily)
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_BitmapDispose($hBitmap)

_GDIPlus_Shutdown()

 

When the words fail... music speaks.

Link to comment
Share on other sites

Creating a bitmap from  graphics you don't have the option to get the pixels as 32 bits per pixel with alpha channel, so there you lost the transparency. But instead you can create your bitmap from Scan0 or you can clone an area of bitmap and both methods allow you to specify bitmap pixels format.

When the words fail... music speaks.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...