Jump to content

[Gdip] How to draw text on transparent bitmap?


Recommended Posts

#include <GDIPlus.au3>

Text2PNG(@ScriptDir & "\x_2.png", 0x7DFFFFFF) ; Transparent text


Func Text2PNG($sFile, $iColor)
    _GDIPlus_Startup()
    Local $hImage = _GDIPlus_BitmapCreateFromFile ( $sFile )
    ;Local $hImage = _GDIPlus_BitmapCreateFromScan0(400, 250)

    ;$sFile2 = @ScriptDir & "\x_3.png"
    ;_GDIPlus_ImageSaveToFile($hImage, $sFile2)

    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage)
    _GDIPlus_GraphicsSetSmoothingMode($hGraphics, $GDIP_SMOOTHINGMODE_HIGHQUALITY)
    _GDIPlus_GraphicsSetTextRenderingHint($hGraphics, $GDIP_TEXTRENDERINGHINT_ANTIALIAS)
    _GDIPlus_GraphicsClear($hGraphics, $iColor)
    _GDIPlus_GraphicsDrawString($hGraphics, "Hello", 0, 0, "Arial", 32, 0)
    _GDIPlus_ImageSaveToFile($hImage, $sFile)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_BitmapDispose($hImage)
    _GDIPlus_Shutdown()
EndFunc   ;==>Text2PNG

The image is at 50% transparency, im trying to write the text on it with the same transparency.

What transparency lvl the text will need to be drawn, to achieve the same transparency as on the image, 50% too? (0x7DFFFFFF)

But atm it does not draw anything unless i create a new bitmap from scan, whats going on?

 

Spoiler

x_2.png.df962669916efaa146e4c3e51abf60f4.png

 

Edited by memerim
Link to comment
Share on other sites

Try this:

#include <GDIPlus.au3>

Text2PNG(@ScriptDir & "\x_2.png", 0xE0FFFFFF) ; Transparent text
ShellExecute(@ScriptDir & "\x_2.png")

Func Text2PNG($sFile, $iColor)
    _GDIPlus_Startup()
    Local $hImage = _GDIPlus_BitmapCreateFromScan0(400, 250)
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage)
    _GDIPlus_GraphicsClear($hGraphics, 0x7F000000)
    _GDIPlus_GraphicsSetSmoothingMode($hGraphics, $GDIP_SMOOTHINGMODE_HIGHQUALITY)
    _GDIPlus_GraphicsSetTextRenderingHint($hGraphics, $GDIP_TEXTRENDERINGHINT_ANTIALIAS)
    Local $hFormat = _GDIPlus_StringFormatCreate()
    Local $hFamily = _GDIPlus_FontFamilyCreate("Consolas")
    Local $hFont = _GDIPlus_FontCreate($hFamily, 32)
    Local $hBrush = _GDIPlus_BrushCreateSolid($iColor)
    _GDIPlus_StringFormatSetAlign($hFormat, 1)
    _GDIPlus_StringFormatSetLineAlign($hFormat, 1)
    _GDIPlus_GraphicsDrawStringEx($hGraphics, "Hello World", $hFont, _GDIPlus_RectFCreate(0, 0, 400, 250), $hFormat, $hBrush)
    _GDIPlus_ImageSaveToFile($hImage, $sFile)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_BitmapDispose($hImage)
    _GDIPlus_Shutdown()
EndFunc   ;==>Text2PNG

 

Edited by UEZ

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

Hello @UEZ, the script resulted in a image without background, just the text, i would like to keep the background and just draw the text over it with the same level of transparency.

The result im trying to achieve is something similar to this image:

Spoiler

x_2.png.882a65ac50bf4ece2a4bcae841ba3851.png

Its the same image of my first post, but now with the text  drawn, and both have the same level of transparency.

Link to comment
Share on other sites

Just add

_GDIPlus_GraphicsClear($hGraphics, 0xFF7F7F7F)

after 

Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage)

and it should work as expected.

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

#include <GDIPlus.au3>

Text2PNG(@ScriptDir & "\x_2.png", 0x7DFFFFFF) ; Transparent text
ShellExecute(@ScriptDir & "\x_2.png")

Func Text2PNG($sFile, $iColor)
    _GDIPlus_Startup()
    Local $hImage = _GDIPlus_BitmapCreateFromScan0(400, 250)
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage)
    _GDIPlus_GraphicsClear($hGraphics, 0xFF7F7F7F)
    _GDIPlus_GraphicsSetSmoothingMode($hGraphics, $GDIP_SMOOTHINGMODE_HIGHQUALITY)
    _GDIPlus_GraphicsSetTextRenderingHint($hGraphics, $GDIP_TEXTRENDERINGHINT_ANTIALIAS)
    Local $hFormat = _GDIPlus_StringFormatCreate()
    Local $hFamily = _GDIPlus_FontFamilyCreate("Arial")
    Local $hFont = _GDIPlus_FontCreate($hFamily, 32)
    Local $hBrush = _GDIPlus_BrushCreateSolid($iColor)
    _GDIPlus_StringFormatSetAlign($hFormat, 1)
    _GDIPlus_StringFormatSetLineAlign($hFormat, 1)
    _GDIPlus_GraphicsDrawStringEx($hGraphics, "Hello World", $hFont, _GDIPlus_RectFCreate(0, 0, 400, 250), $hFormat, $hBrush)
    _GDIPlus_ImageSaveToFile($hImage, $sFile)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_BitmapDispose($hImage)
    _GDIPlus_Shutdown()
EndFunc   ;==>Text2PNG

Now the image have no transparency:

Spoiler

x_2.png.2515de9505faefdbe93cd037174c4a7a.png

 

Link to comment
Share on other sites

If you mean the background then set alpha channel of

_GDIPlus_GraphicsClear($hGraphics, 0x7F7F7F7F) (ARGB)

accordingly. Here 0x7F is semi-transparent.

Edited by UEZ

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

The code above

produces this output: 

Test-gdi.png

You have to adjust the color of the background / text accordingly.

 

 

Edited by UEZ

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

Then look into the help file to checkout how to create a gradient background. 😉

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

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

×
×
  • Create New...