Jump to content

[SOLVED] GDI+ transparency


nend
 Share

Recommended Posts

Hoi Guys,

How do I load 2 image (PNG) and make the background image a bit transparent but the forground image must be full vissible.

I can do this in Photoshop but I want to change the transparency realtime.

I only want to make the "Legemeter.png" transparent but not the "Torus.png"

#include <GDIPlus.au3>
#include <GuiConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

GUISetState()
$g_hGUI = GUICreate("Test", 250, 250, -1, -1, $WS_POPUP, BitOR($WS_EX_LAYERED,$WS_EX_TOOLWINDOW))

_GDIPlus_Startup()
$g_hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Torus.png")
$hImagetemp = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Legemeter.png")
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImagetemp)
_GDIPlus_GraphicsDrawImageRect($hGraphic, $g_hImage, 10, 10, 200, 200)

SetBitmap($g_hGUI, $hImagetemp, 255)
GUISetState()

; Loop until user exits
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_GDIPlus_ImageDispose($g_hImage)
_GDIPlus_ImageDispose($hImagetemp)
_GDIPlus_Shutdown()

Func SetBitmap($hGUI, $hImage, $iOpacity)
    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend

    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", _GDIPlus_ImageGetWidth($hImage))
    DllStructSetData($tSize, "Y", _GDIPlus_ImageGetHeight($hImage))
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate($tagBLENDFUNCTION)
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", $iOpacity)
    DllStructSetData($tBlend, "Format", 1)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC($hMemDC)
EndFunc   ;==>SetBitmap

 

Edited by nend
Link to comment
Share on other sites

Make the background of the torus opaque or semi transparent before you display it by creating

  1. create an empty bitmap with the size of the torus
  2. create an gfx handle refering to the bitmap in 1.
  3. clear it using _GDIPlus_GraphicsClear the bitmap
  4. copy the torus image to the bitmap in 1
  5. dispose the resources
  6. copy the new bitmap to $hImagetemp

 

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

@UEZ Thanks for the reply.

I don't  know how to do those steps, do you have a small example code?

I think GDI+ is sometimes a bit difficult for me, I try to learn from example code.

EDIT:

If I look at your steps It look like the png replace each other but I want the legemeter.png a bit transparent (example 200) and the Torus.png full visible ontop of the legemeter.png (example 255)

Edited by nend
Link to comment
Share on other sites

Here we go:

#include <GDIPlus.au3>
#include <GuiConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

GUISetState()
$g_hGUI = GUICreate("Test", 250, 250, -1, -1, $WS_POPUP, BitOR($WS_EX_LAYERED,$WS_EX_TOOLWINDOW))

_GDIPlus_Startup()

$g_hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Torus.png")
$g_aDim = _GDIPlus_ImageGetDimension($g_hImage)
$g_hImage2 = _GDIPlus_BitmapCreateFromScan0($g_aDim[0], $g_aDim[1])
$g_hGfx = _GDIPlus_ImageGetGraphicsContext($g_hImage2)
_GDIPlus_GraphicsClear($g_hGfx, 0xA0000000)
_GDIPlus_GraphicsDrawImageRect($g_hGfx, $g_hImage, 0, 0, $g_aDim[0], $g_aDim[1])
_GDIPlus_GraphicsDispose($g_hGfx)

$hImagetemp = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Legemeter.png")
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImagetemp)
_GDIPlus_GraphicsDrawImageRect($hGraphic, $g_hImage2, 10, 10, 200, 200)

SetBitmap($g_hGUI, $hImagetemp, 255)
GUISetState()

; Loop until user exits
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_GDIPlus_ImageDispose($g_hImage)
_GDIPlus_ImageDispose($g_hImage2)
_GDIPlus_ImageDispose($hImagetemp)
_GDIPlus_Shutdown()

Func SetBitmap($hGUI, $hImage, $iOpacity)
    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend

    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", _GDIPlus_ImageGetWidth($hImage))
    DllStructSetData($tSize, "Y", _GDIPlus_ImageGetHeight($hImage))
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate($tagBLENDFUNCTION)
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", $iOpacity)
    DllStructSetData($tBlend, "Format", 1)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC($hMemDC)
EndFunc   ;==>SetBitmap

 

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

Thanks UEZ, but...... I see the 2 image but there is some strange black square between the 2 images and the Legemeter.png isn't transparent.

See the screenshot below

Knipsel.JPG

This is made in Photoshop but this is the effect what I'm looking for.

Transparent legemeter.png and not transparent torrus.png

Knipsel2.JPG

 

 

Edited by nend
Link to comment
Share on other sites

Sorry, I understood it vice versa.

2nd try:

#include <GDIPlus.au3>
#include <GuiConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

GUISetState()
$g_hGUI = GUICreate("Test", 250, 250, -1, -1, $WS_POPUP, BitOR($WS_EX_LAYERED,$WS_EX_TOOLWINDOW))

_GDIPlus_Startup()

$g_hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Torus.png")
$hImagetemp = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Legemeter.png")

$hAttribute_Alpha = _GDIPlus_ImageAttributesCreate()
$tColorMatrix = _GDIPlus_ColorMatrixCreateTranslate(0, 0, 0, -0.16) ;0 = opaque, -1 = transparent
_GDIPlus_ImageAttributesSetColorMatrix($hAttribute_Alpha, 0, True, DllStructGetPtr($tColorMatrix))

$g_aDim = _GDIPlus_ImageGetDimension($hImagetemp)

$g_hImage2 = _GDIPlus_BitmapCreateFromScan0($g_aDim[0], $g_aDim[1])
$g_hGfx = _GDIPlus_ImageGetGraphicsContext($g_hImage2)

_GDIPlus_GraphicsDrawImageRectRect($g_hGfx, $hImagetemp, 0, 0, $g_aDim[0], $g_aDim[1], 0, 0, $g_aDim[0], $g_aDim[1], $hAttribute_Alpha)
_GDIPlus_GraphicsDrawImageRect($g_hGfx, $g_hImage, 10, 10, 200, 200)


SetBitmap($g_hGUI, $g_hImage2, 255)
GUISetState()

; Loop until user exits
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_GDIPlus_ImageAttributesDispose($hAttribute_Alpha)
_GDIPlus_ImageDispose($g_hImage)
_GDIPlus_ImageDispose($g_hImage2)
_GDIPlus_ImageDispose($hImagetemp)
_GDIPlus_GraphicsDispose($g_hGfx)
_GDIPlus_Shutdown()

Func SetBitmap($hGUI, $hImage, $iOpacity)
    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend

    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", _GDIPlus_ImageGetWidth($hImage))
    DllStructSetData($tSize, "Y", _GDIPlus_ImageGetHeight($hImage))
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate($tagBLENDFUNCTION)
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", $iOpacity)
    DllStructSetData($tBlend, "Format", 1)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC(0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC($hMemDC)
EndFunc   ;==>SetBitmap

 

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

  • Recently Browsing   0 members

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