Jump to content
therks

AutoIt forcing picture transparency when I don't want it?

Recommended Posts

Posted (edited)

I feel dumb even having to ask this, as it seems like something that should be obvious, or at least easy to find but I'm just not seeing it (I'm half expecting the first response to be a link to another forum thread with the exact answer)...

Sometimes AutoIt will designate some color as transparent even though it shouldn't be. This is happening in GIF's mostly but I've seen it with BMP's as well (it's just less consistent). In GIF's with NO transparent color set, it's picking one at random (closest to white in my brief testing), and even in GIF's that DO have transparency set, it's ignoring that color in favor of the other one.

I've attached 3 files that are the same picture visually. One is a BMP. One is a GIF with no transparency set (giftest-notrans.gif) the other is a GIF with the transparency set to the magenta color (#FF00FF).

Here's the code for testing:

$hGUI = GUICreate('', 300, 200)
GUICtrlCreatePic("giftest-notrans.gif", 0, 0, 100, 100)
GUICtrlCreatePic("giftest-trans.gif", 200, 0, 100, 100)
GUICtrlCreatePic("giftest-bmp.bmp", 100, 100, 100, 100)
GUISetState()

AdlibRegister(_RandomBG, 100)

Do
Until GUIGetMsg() = -3

Func _RandomBG()
    GUISetBkColor(Random(0, 0xffffff, 1))
EndFunc

And here's a little animation of how the GUI looks on my end.

 

So what am I doing wrong? I just want to place a GIF on my GUI and not have the background seep through.

giftest-notrans.gif

giftest-trans.gif

giftest-bmp.bmp

Edited by therks
Fixed absolute file paths

Share this post


Link to post
Share on other sites

Here a workaround:

#include <StaticConstants.au3>
#include <GDIPlus.au3>

_GDIPlus_Startup()
Global Const $hImage1 = _GDIPlus_ImageLoadFromFile("giftest-notrans.gif"), $hGDIBmp1 = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage1), _
                     $hImage2 = _GDIPlus_ImageLoadFromFile("giftest-trans.gif"),  $hGDIBmp2 = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage2), _
                     $hImage3 = _GDIPlus_ImageLoadFromFile("giftest-bmp.bmp"), $hGDIBmp3 = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage3)
_GDIPlus_ImageDispose($hImage1)
_GDIPlus_ImageDispose($hImage2)
_GDIPlus_ImageDispose($hImage3)
_GDIPlus_Shutdown()

$hGUI = GUICreate('', 300, 200)
$Pic1 = GUICtrlCreatePic("", 0, 0, 100, 100)
$Pic2 = GUICtrlCreatePic("", 200, 0, 100, 100)
$Pic3 = GUICtrlCreatePic("", 100, 100, 100, 100)
_WinAPI_DeleteObject(GUICtrlSendMsg($Pic1, $STM_SETIMAGE, $IMAGE_BITMAP, $hGDIBmp1))
_WinAPI_DeleteObject(GUICtrlSendMsg($Pic2, $STM_SETIMAGE, $IMAGE_BITMAP, $hGDIBmp2))
_WinAPI_DeleteObject(GUICtrlSendMsg($Pic3, $STM_SETIMAGE, $IMAGE_BITMAP, $hGDIBmp3))
GUISetState()

AdlibRegister(_RandomBG, 100)

Do
Until GUIGetMsg() = -3

_WinAPI_DeleteObject($hGDIBmp1)
_WinAPI_DeleteObject($hGDIBmp2)
_WinAPI_DeleteObject($hGDIBmp3)

Func _RandomBG()
    GUISetBkColor(Random(0, 0xffffff, 1))
EndFunc

 


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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
Posted (edited)

Awesome, thanks very much.

So I guess this is just an issue with AutoIt? There's not like some built-in (ie: Not using GDI or WinAPI) method to keep it from picking a random color to clear? I'm surprised I couldn't find another thread that brought up this issue.

Also, it's funny. The threads I did read through almost all had you weigh in at some point. I had a feeling you'd be the one to solve my problem. 😁

Edited by therks

Share this post


Link to post
Share on other sites
Posted (edited)

Hey @UEZ, do you know if I can adjust the way png's with alpha blending are rendered? I have this image (attached) with a nice smooth fade that looks fine on webpages but has a harsh ugly edge in AutoIt. Any ideas?

png_blend.png

 

*Edit: Here's another example with red background for both. As you can see, the part that's supposed to be a white fade is also rendering to gray.

*Edit 2: Whoops, forgot to add the code example 🤦‍♂️

#include <GUIConstants.au3>
#include <GDIPlus.au3>

_GDIPlus_Startup()
$hMain = GUICreate('', 200, 200)
$pi_Pic = _GUICtrlCreatePNG(@ScriptDir & '\png_blend.png', 50, 50, 100, 100)
;~ GUISetBkColor(0xff0000)
GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_GDIPlus_Shutdown()


Func _GUICtrlCreatePNG($sPath, $iX, $iY, $iW, $iH)
    Local $idPic = GUICtrlCreatePic('', $iX, $iY, $iW, $iH)
    Local $hImage = _GDIPlus_ImageLoadFromFile($sPath)
    Local $hBmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    _WinAPI_DeleteObject(GUICtrlSendMsg($idPic, $STM_SETIMAGE, $IMAGE_BITMAP, $hBmp))
    _GDIPlus_ImageDispose($hImage)
    _WinAPI_DeleteObject($hBmp)
    Return $idPic
EndFunc

 

Edited by therks

Share this post


Link to post
Share on other sites

In this case I would suggest to remove the alpha channel and draw with background color.

#include <GUIConstants.au3>
#include <GDIPlus.au3>

Global $iBgColor = 0xFFFF0000
_GDIPlus_Startup()
$hMain = GUICreate('', 200, 200)
$pi_Pic = _GUICtrlCreatePNG(@ScriptDir & '\png_blend.png', 50, 50, 100, 100, $iBgColor)
GUISetBkColor(BitAND(0xFFFFFF, $iBgColor))
GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_GDIPlus_Shutdown()


Func _GUICtrlCreatePNG($sPath, $iX, $iY, $iW, $iH, $iBgColor)
    Local $idPic = GUICtrlCreatePic('', $iX, $iY, $iW, $iH)
    Local $hImage = _GDIPlus_ImageLoadFromFile($sPath)
    ;Local $hBmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    Local $hBmp = _GDIPlus_Convert2HBitmap($hImage, $iBgColor)
    _WinAPI_DeleteObject(GUICtrlSendMsg($idPic, $STM_SETIMAGE, $IMAGE_BITMAP, $hBmp))
    _GDIPlus_ImageDispose($hImage)
    _WinAPI_DeleteObject($hBmp)
    Return $idPic
EndFunc

Func _GDIPlus_Convert2HBitmap($hBitmap, $iBgColor)
    Local $iWidth = _GDIPlus_ImageGetWidth($hBitmap), $iHeight = _GDIPlus_ImageGetHeight($hBitmap)
    Local $hBitmap_new = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
    Local $hCtx_new = _GDIPlus_ImageGetGraphicsContext($hBitmap_new)
    Local $hBrush = _GDIPlus_BrushCreateSolid($iBgColor)
    _GDIPlus_GraphicsFillRect($hCtx_new, 0, 0, $iWidth, $iHeight, $hBrush)
    _GDIPlus_GraphicsDrawImageRect($hCtx_new, $hBitmap, 0, 0, $iWidth, $iHeight)
    Local $hHBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap_new)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_BitmapDispose($hBitmap_new)
    _GDIPlus_GraphicsDispose($hCtx_new)
    Return $hHBITMAP
EndFunc   ;==>_GDIPlus_Convert2HBitmap

 


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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...