Jump to content

Placing transparent pic inside of another?


Recommended Posts

Greets All,

  I am attempting to create a transparent image icon file and then create that file inside of another image.  I already have code that can combine images together without any problems however I am having a bit of trouble creating the transparent image.  The error seems to throw at the "_WinAPI_Create32BitHICON" command...the following code is fairly well commented and I think explains how I'm trying to go about this...any helpful hints on the possible problem would be greatly appreciated.  I thank in advance.

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>
#Include <WinAPIEx.au3>

 Global $hGUI

 Global $MainGUI = GUICreate("Example", 425, 540, 450, 250)
 GUISetState(@SW_SHOW) 


 _Combine()


While 1

   $nMsg = GUIGetMsg(1)  ;use advanced mode when using multiple GUIs
   Switch $nMsg[1]  ;Switch on the GUI sending the message

        Case $MainGUI
            Switch $nMsg[0] ; switch on the control from the GUI sending the message.
                Case $GUI_EVENT_CLOSE
                  Exit

            EndSwitch

    EndSwitch
    
Wend    


 Func _Combine()
    
 $hGUI = GUICreate("Preview", 100, 100, 456, 563, $WS_BORDER, -1, $MainGUI)  
 GUISetBkColor(0xFFFFFF)  ;set GUI background to 'White'... 
 GUISetState(@SW_SHOW) 

; Initialize GDI+ library
_GDIPlus_Startup()

 Local $hBitmap = _GDIPlus_BitmapCreateFromFile("C:\My Documents\_Green_Counter.png")   
 $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)  ;the GUI acts as a graphics context in which to draw the image...
 $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap)
   ;the 'Counter' image itself acts as a graphics context in which to draw another image...

 $hIcon = _WinAPI_Create32BitHICON("C:\My Documents\_Star.ico")  
   ;attempt to create/convert icon...not functioning (error returns "0")
 _WinAPI_AddIconTransparency($hIcon, 25, 0)
   ;attempt to make nearly transparent


 _GDIPlus_GraphicsDrawImage($hGraphics, $hIcon, 20, 20)
   ;draw the transparent icon in the original image...  


 _GDIPlus_ImageSaveToFile($hBitmap, "C:\My Documents\test.bmp")   
   ;save image 
 $hImage = _GDIPlus_ImageLoadFromFile("C:\My Documents\test.bmp")  
   ;load for display in window
 _GDIPlus_GraphicsDrawImage($hGraphic, $hImage, 10, 10)  
   ;now can draw the 'Counter' (with its combined 'transparent icon') into the GUI as a 'preview sample'...

    ; Clean up resources
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_GraphicsDispose($hGraphics)    
    _GDIPlus_ImageDispose($hBitmap)
    _GDIPlus_ImageDispose($hImage)  
    _WinAPI_DestroyIcon($hIcon)

; Shut down GDI+ library
_GDIPlus_Shutdown()

 EndFunc

 

 

post-35979-0-72644100-1369757097_thumb.p

_Star.ico

Link to comment
Share on other sites

Thanks for the reply...combining the images into 1 image is no problem...what I'd like to be able to do is have the 'star' image be transparent (low opacity value) when it is combined with the other image.  Is there a command that sets the opacity of the pixels that I could possibly use to accomplish that?

Link to comment
Share on other sites

NutStomper:  Hmm yes you are correct...perhaps "GUICtrlCreateIcon" to make a handle for the icon?

UEZ:  Yes it seems that is along the lines of what I want, just a static image with transparency combined/drawn into another image, no movement animation needed.  I'll have to take a look at that script more closely to see if I can modify it for my needs. 

Thanks to each of you for posting!

Link to comment
Share on other sites

Hello Again,

  I have attempted to do this by modifying the code in the link that UEZ provided...I almost have it.  The 'source' image does in fact load into the 'destination' image with transparency as desired...however in doing so the borders of the original 'destination' image have been turned black, as can be seen in the image I attached.  I'm sure this is related to the GDIP code however this is my first time working with those functions and I am not familiar enough with them in order to correct that.  Can somebody advise me on why the 'white' borders were changed to 'black'?  I need them to retain their original transparency when loaded/displayed in a GUI window.  Thanks in advance. 

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>
#Include <WinAPIEx.au3>
#include <GDIP.au3>

 Global $hGUI

 Global $MainGUI = GUICreate("Example", 425, 540, 450, 250)
 GUISetState(@SW_SHOW) 


 _Combine()


While 1

   $nMsg = GUIGetMsg(1)  ;use advanced mode when using multiple GUIs
   Switch $nMsg[1]  ;Switch on the GUI sending the message

        Case $MainGUI
            Switch $nMsg[0] ; switch on the control from the GUI sending the message.
                Case $GUI_EVENT_CLOSE
                  Exit

            EndSwitch

    EndSwitch
    
Wend    


 Func _Combine()
    
 $hGUI = GUICreate("Preview", 100, 100, 456, 563, $WS_BORDER, -1, $MainGUI)  
 GUISetBkColor(0xFFFFFF)  ;set GUI background to 'White'... 
 GUISetState(@SW_SHOW) 

; Initialize GDI+ library
_GDIPlus_Startup()

 Local $hBitmap = _GDIPlus_BitmapCreateFromFile("C:\My Documents\_Green_Counter.png")  
 Local $_enhancement = _GDIPlus_BitmapCreateFromFile("C:\My Documents\_Star.png")  

 $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)  ;the GUI acts as a graphics context in which to draw the image...
 $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap)
   ;the 'Counter' image itself acts as a graphics context in which to draw another image...

$hAttribute_Alpha = _GDIPlus_ImageAttributesCreate()
$tColorMatrix = _GDIPlus_ColorMatrixCreateTranslate(0, 0, 0, -0.65) ;set alpha channel value
$pColorMatrix = DllStructGetPtr($tColorMatrix)
_GDIPlus_ImageAttributesSetColorMatrix($hAttribute_Alpha, 0, True, $pColorMatrix)


 _GDIPlus_GraphicsDrawImageRectRectIA($hGraphics, $_enhancement, -10, -10, 40, 40, 0, 0, 40, 40, $hAttribute_Alpha)  
   ;draw the transparent icon in the original image...  


 _GDIPlus_ImageSaveToFile($hBitmap, "C:\My Documents\test.bmp")   
   ;save image 

 $hImage = _GDIPlus_ImageLoadFromFile("C:\My Documents\test.bmp")  
   ;load for display in window
 _GDIPlus_GraphicsDrawImage($hGraphic, $hImage, 10, 10)  
   ;now can draw the 'Counter' (with its combined 'transparent icon') into the GUI as a 'preview sample'...

    ; Clean up resources
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_GraphicsDispose($hGraphics)    
    _GDIPlus_ImageDispose($hBitmap)
    _GDIPlus_ImageDispose($hImage)  
    _GDIPlus_ImageDispose($_enhancement)

; Shut down GDI+ library
_GDIPlus_Shutdown()

 EndFunc

post-35979-0-29136100-1369838726_thumb.j

Link to comment
Share on other sites

BMP format doesn't support transparency. Save as test.png instead.

Br,

UEZ

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

OK, that makes sense.  However if I save as .png am I later able to use that image as a GUICtrlSetImage 'swap' with an image created by the GUICtrlCreatePic command?  I was under the impression .png cannot be used with images created with GUICtrlCreatePic...or perhaps simply .png cannot be used to create them initially???  If not, is there a work-around?  Thanks again.

Link to comment
Share on other sites

Yes, PNG is not good for pic controls. There are several posts how to put a png to a pic control using gdi+.

If you cannot find anything, let me know it.

Br,

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

Actually yes I would appreciate a link or two if it's no trouble...looked around some however I'm not even sure how to phrase the search for something like that.

Another thing about the last script I posted to create the transparent image within the other...it seems that re-sizing and moving the image around within the 'destination' image require 'counter-intuitive' values?  To resize the 'source' image (20x20 pixels) to make it smaller I had to set 40x40 in the '_GDIPlus_GraphicsDrawImageRectRectIA' command...and to center it I used -10, -10 because if I used 10, 10 that would place the image off the upper left edge...can you explain why this is??  Thanks again as always!  

Link to comment
Share on other sites

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

Global Const $STM_SETIMAGE = 0x0172
_GDIPlus_Startup()
Global Const $hImage = _GDIPlus_ImageLoadFromFile("YOUR IMAGE.png")
Global Const $hHBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
Global Const $iW = _GDIPlus_ImageGetWidth($hImage)
Global Const $iH = _GDIPlus_ImageGetHeight($hImage)
_GDIPlus_ImageDispose($hImage)
Global Const $hGUI = GUICreate("", $iW, $iH, -1 , -1, $WS_POPUP, $WS_EX_LAYERED)
Global Const $cPic = GUICtrlCreatePic("", 0, 0, $iW, $iH)
GUICtrlSetState(-1, $GUI_DISABLE)
Global Const $cButton = GUICtrlCreateButton("Exit", $iW - 40, $iH - 40, 30, 30)
Global Const $cLabel = GUICtrlCreateLabel("", 0, 0, $iW, $iH, -1, $GUI_WS_EX_PARENTDRAG)
GUICtrlSetBkColor(-1, -2)
_WinAPI_DeleteObject(GUICtrlSendMsg($cPic, $STM_SETIMAGE, 0, $hHBITMAP))
_GDIPlus_ShutDown()
GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE  Or $msg = $cButton Then ExitLoop
WEnd
_WinAPI_DeleteObject($hHBITMAP)
GUIDelete($hGUI)
Exit 

To delete the image just send 0 instead of $hHBITMAP or for a new image load the image, convert it to hHBitmap and send it to the control

Br,

UEZ

Try this:

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 for the info...however I am a bit confused about making the image sensitive to being 'clicked'...in GUICtrlCreatePic you can set the '$SS_NOTIFY' flag so the image will be sensitive to mouse clicks...using the above method will lose that functionality, correct?   

Link to comment
Share on other sites

Line 14 disables the pic control and thus no message will be sent when clicking to the pic control.

The reason for line 14 is that the picture is a background picture and if you want to create more controls over the picture control you have to disable it otherwise you will get more problems with the controls.

Br,

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

Thanks again for the reply.  So in order to enable 'clicking' sensitivity I just add the '$SS_NOTIFY' into line 13 in the appropriate location, correct?

Can you explain a couple of questions I have about your code please...I just want to be sure I understand it: 

1.  What is the Global declaration "Global Const $STM_SETIMAGE = 0x0172" doing?

2.  I understand the "_WinAPI_DeleteObject(GUICtrlSendMsg($cPic, $STM_SETIMAGE, 0, $hHBITMAP))" is sending a message to the 'picture' control to change the image and delete the previous one...however why do you place the "$hBITMAP" in the second parameter, and "0" in the first parameter?  Also how does the "_WinAPI_DeleteObject" know to delete a bitmap?  It says in the Help File it can also be a pen, brush, font, region, or palette...is the 'SendMsg' returning the handle to the previous bitmap used for the control??  Thanks again for your help.

Link to comment
Share on other sites

Thanks again for the reply.  So in order to enable 'clicking' sensitivity I just add the '$SS_NOTIFY' into line 13 in the appropriate location, correct?

 

Can you explain a couple of questions I have about your code please...I just want to be sure I understand it: 

 

1.  What is the Global declaration "Global Const $STM_SETIMAGE = 0x0172" doing?

2.  I understand the "_WinAPI_DeleteObject(GUICtrlSendMsg($cPic, $STM_SETIMAGE, 0, $hHBITMAP))" is sending a message to the 'picture' control to change the image and delete the previous one...however why do you place the "$hBITMAP" in the second parameter, and "0" in the first parameter?  Also how does the "_WinAPI_DeleteObject" know to delete a bitmap?  It says in the Help File it can also be a pen, brush, font, region, or palette...is the 'SendMsg' returning the handle to the previous bitmap used for the control??  Thanks again for your help.

$SS_NOTIFY is set by default, thus you don't need to set it explicitly.

 

1. GUICtrlSendMsg(<control id>, $STM_SETIMAGE, $IMAGE_BITMAP, <GDI bitmap>) is the correct syntax for this kind of operation whereas

$STM_SETIMAGE = 0x0172 and$IMAGE_BITMAP = 0. Look in the helpfile or at MSDN for a detailed description what GUICtrlSendMsg does.

 

2. see 1. what the 0 is. I don't know how the DeleteObject function in gdi32.dll knows what type of handles it receives.

Br,

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

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...