Rex

Remove image created with _GDIPlus_GraphicsDrawImage

11 posts in this topic

Hi

I'm using _GDIPlus_GraphicsDrawImage to add an Image to my gui, and that works perfect :)

But I can't find a way to remove the image, once I have Drawn it to the gui :(

I'd tried _GDIPlus_GraphicsClear but that just made my gui turn all black.

Then I tried _GDIPlus_BitmapCreateFromResource course I already have an Image included in my exe by using ResourcesEx UDF, but that didn't work either :'(

 

Can any one help me, by pointing me in right direction so I can either delete the image drawn or update it with the one I have in my exe - or maybe both :D one could alway get the use of it later ;)

 

Cheers

/Rex

Share this post


Link to post
Share on other sites



By default GDIPlus_GraphicsClear() fills the graphic with black color. You can set a different color e.g. 0xFFF0F0F0 which looks like the default gui color.

If you have transparent images then it makes sense to clear the graphic before drawing the image onto the graphic. Otherwise just copy the new image to the graphic and it will overwrite the pixels.

Maybe a short example of what you are trying can help to provide a better solution.


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

#3 ·  Posted (edited)

I'm setting the image from a websource using this

$hBmp = _GDIPlus_BitmapCreateFromMemory(InetRead(_ArrayToString($dImg, "")), True) ;to load an image from the net
            $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hBmp) ; Creating img thats just been loaded from web
            
Local $hBitmap_Scaled = _GDIPlus_ImageResize($hBitmap, 150, 222) ; Resizing the image to 140 x 200 pixels
Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hNGS_GUI) ; create a graphics object from a window handle
    _GDIPlus_GraphicsDrawImage($hGraphics, $hBitmap_Scaled, 213, 73) ; display scaled image on the gui
    
    ; Cleaning up mem and releases resources
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_BitmapDispose($hBitmap_Scaled)
    _WinAPI_DeleteObject($hBmp)

I first started out with (what if I don't remember wrong) was a code ex of you'rs from a thread about using an image from the web without downloade it

$hBmp = _GDIPlus_BitmapCreateFromMemory(InetRead(_ArrayToString($dImg, "")), True) ;to load an image from the net
            $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hBmp) ; Creating img thats just been loaded from web
            _WinAPI_DeleteObject(GUICtrlSendMsg($idPic_Prew, $STM_SETIMAGE, $IMAGE_BITMAP, $hBmp)) ; Showing newly created img at the gui

But I couldn't find a way to make the scale function work on it, so I diggede into the help file and found the above and combined it with the ex from you :)

What I'm trying is if the user clicks on a clear button, all input ect. should be cleared to nothing - but I can't clear the img :/

I have tried looking threw the help file, and found

Local $sAut2Exe = StringRegExpReplace(@AutoItExe, "\\\w+.exe", "\\Aut2Exe\\Aut2Exe.exe") ;get path to the Aut2Exe.exe file
    If @AutoItX64 Then $sAut2Exe = StringRegExpReplace(@AutoItExe, "\\\w+.exe", "\\Aut2Exe\\Aut2Exe_X64.exe")
    Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hNGS_GUI)
    Local $hInst = _WinAPI_LoadLibrary($sAut2Exe) ;maps a specified executable module into the address space of the calling process
     Local $hBitmap = _GDIPlus_BitmapCreateFromResource($hInst, "NGS_JPG_1") ;load bitmap resource "BMP_MAINLOGO" and convert it to GDI+ bitmap format
;~     Local $iW = _GDIPlus_ImageGetWidth($hBitmap), $iH = _GDIPlus_ImageGetHeight($hBitmap)
    _GDIPlus_GraphicsDrawImage($hGraphics, $hBitmap, 212, 72) ;display image in GUI centered

And that I'd tried but still the prew image stays on the gui :'(

But what i'm trying to do, is to clear the drawn image (this way I can show my default img, witch I have included in the exe file) or to "overwrite" the drawn image with the default I have in my exe

Hope that helps to understand what i'm trying to do :)

 

Cheers

/Rex

Edited by Rex

Share this post


Link to post
Share on other sites

You don't need to load the image from the internet converted to GDI and then back again to GDI+.

Use

$hBitmap = _GDIPlus_BitmapCreateFromMemory(InetRead(_ArrayToString($dImg, ""))) ;to load an image from the net

to load it directly to GDI+.

Further

Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hNGS_GUI)

will use the whole GUI as a graphic (canvas). I would suggest here to use only a portion of your gui to display the image e.g. by using a picture control where you can send always the image to it or use _GDIPlus_BitmapCreateFromGraphics() / _GDIPlus_ImageGetGraphicsContext() / _GDIPlus_GraphicsDrawImageRect()

Example:

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

_GDIPlus_Startup()
Global $hGUI = GUICreate("", 600, 400)
GUISetBkColor(0xF0F0F0, $hGUI)
Global $iBtn = GUICtrlCreateButton("Display", 500, 300, 50, 50)
GUISetState()

$hCanvas = _GDIPlus_GraphicsCreateFromHWND($hGUI)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics(193, 184, $hCanvas) ;size of the torus.png image
$hGfx = _GDIPlus_ImageGetGraphicsContext($hBitmap)

$hImage1 = _GDIPlus_ImageLoadFromFile("c:\Program Files (x86)\AutoIt3\Examples\GUI\merlin.gif") ;size is 68x71
$hImage2 = _GDIPlus_ImageLoadFromFile("c:\Program Files (x86)\AutoIt3\Examples\GUI\Torus.png") ;size is 193x184
_GDIPlus_GraphicsDrawImageRect($hGfx, $hImage1, 0, 0, 68, 71)
_GDIPlus_GraphicsDrawImageRect($hCanvas, $hBitmap, 0, 0, 193, 184)

Do
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $iBtn
            _GDIPlus_GraphicsClear($hGfx, 0xFFF0F0F0)
            _GDIPlus_GraphicsDrawImageRect($hGfx, $hImage2, 0, 0, 193, 184)
            _GDIPlus_GraphicsDrawImageRect($hCanvas, $hBitmap, 0, 0, 193, 184)
    EndSwitch
Until False

_GDIPlus_GraphicsDispose($hCanvas)
_GDIPlus_GraphicsDispose($hGfx)
_GDIPlus_ImageDispose($hImage1)
_GDIPlus_ImageDispose($hImage2)
_GDIPlus_ImageDispose($hBitmap)
_GDIPlus_Shutdown()
GUIDelete()

 


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

#5 ·  Posted (edited)

I do have a pic controle, but I couldn't get the _GDIPlus_ImageResize to work on the image loaded using $hBmp = _GDIPlus_BitmapCreateFromMemory(InetRead(_ArrayToString($dImdbImg, "")), True)

Course the img i get from the web is larger than my pic controle, I need to resize it before displaying it.

Thats why I did it the other way.

Ohh maybe I should remember to tell that when I try to use:

Local $hBitmap_Scaled = _GDIPlus_ImageResize($hBmp, 150, 222)
_WinAPI_DeleteObject(GUICtrlSendMsg($idPic1, $STM_SETIMAGE, $IMAGE_BITMAP, $hBitmap_Scaled)) autoit crashes :'(

Cheers

/Rex

Edited by Rex
more info

Share this post


Link to post
Share on other sites

I do have a pic controle, but I couldn't get the _GDIPlus_ImageResize to work on the image loaded using $hBmp = _GDIPlus_BitmapCreateFromMemory(InetRead(_ArrayToString($dImdbImg, "")), True)

Course the img i get from the web is larger than my pic controle, I need to resize it before displaying it.

Thats why I did it the other way.

Ohh maybe I should remember to tell that when I try to use:

Local $hBitmap_Scaled = _GDIPlus_ImageResize($hBmp, 150, 222)
_WinAPI_DeleteObject(GUICtrlSendMsg($idPic1, $STM_SETIMAGE, $IMAGE_BITMAP, $hBitmap_Scaled)) autoit crashes :'(

Cheers

/Rex

GUICtrlSendMsg needs a GDI bitmap not a GDI+! _GDIPlus_ImageResize returns a GDI+ bitmap.

You have convert the result to a GDI bitmap using _GDIPlus_BitmapCreateFromHBITMAP.


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

I tried this

$hBmp = _GDIPlus_BitmapCreateFromMemory(InetRead(_ArrayToString($dImg, "")), True) ;to load an image from the net
            Local $hBitmap_Scaled = _GDIPlus_ImageResize($hBmp, 150, 222) ; Resizing the image to 140 x 200 pixels
            $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap_Scaled) ; Creating img thats just been loaded from web
            _WinAPI_DeleteObject(GUICtrlSendMsg($idPic1, $STM_SETIMAGE, $IMAGE_BITMAP, $hBitmap)) ; Showing newly created img at the gui

But AutoIT still crashes :'(

 

Cheers

/Rex

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Yes, because you are still mixing GDI and GDI+ formats!

$hBmp = _GDIPlus_BitmapCreateFromMemory(InetRead(_ArrayToString($dImg, "")), True) ;this will create a GDI image handle because of the True parameter
Local $hBitmap_Scaled = _GDIPlus_ImageResize($hBmp, 150, 222) ;you are trying to pass a GDI image handle to a GDI+ function which will crash AutoIt
$hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap_Scaled) ;you are converting a GDI+ image to a GDI format
_WinAPI_DeleteObject(GUICtrlSendMsg($idPic1, $STM_SETIMAGE, $IMAGE_BITMAP, $hBitmap)) ;only GDI image format can be sent to a pic control

Read my comment above.

Remove the True parameter and it should work.

GDI  GDI+ 

Edited by UEZ
overseen wrong function call

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

I give up.

$hBmp = _GDIPlus_BitmapCreateFromMemory(InetRead(_ArrayToString($dImg, ""))) ;to load an image from the net in GDI+ format
            $hBitmap_Scaled = _GDIPlus_ImageResize($hBmp, 150, 222) ; Resizing the image to 150 x 222 pixels
            $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap_Scaled) ; This should convert the GDI+ to a GDI image
            GUICtrlSendMsg($idPic1, $STM_SETIMAGE, $IMAGE_BITMAP, $hBitmap) ; Should show pic in pic controle

this should be as instructed above, but no pic is shown :'(

Maybe I'm just not clever enough to use this function.:>

 

Cheers

/Rex

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Sorry, I've overseen that you have used the wrong function.

Here the corrected one:

$hBmp = _GDIPlus_BitmapCreateFromMemory(InetRead(_ArrayToString($dImg, ""))) ;to load an image from the net in GDI+ format
            $hBitmap_Scaled = _GDIPlus_ImageResize($hBmp, 150, 222) ; Resizing the image to 150 x 222 pixels
            $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap_Scaled) ; This should convert the GDI+ to a GDI image
            GUICtrlSendMsg($idPic1, $STM_SETIMAGE, $IMAGE_BITMAP, $hBitmap) ; Should show pic in pic controle

 

$hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap_Scaled) -> $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap_Scaled).

 

Example:

#include <GDIplus.au3>
#include <GUIConstantsEx.au3>
#Include <Memory.au3>

Opt("MustDeclareVars", 1)
_GDIPlus_Startup()
Global $hImage = _GDIPlus_BitmapCreateFromMemory(InetRead("https://www.autoitscript.com/forum/cdn/images/logo_autoit_210x72.png")) ;to load an image from the net
Global $iWidth = _GDIPlus_ImageGetWidth($hImage)
Global $iHeight = _GDIPlus_ImageGetHeight($hImage)
Global $hBitmap_Scaled = _GDIPlus_ImageResize($hImage, $iWidth * 2, $iHeight * 2) ; Resizing the image to 150 x 222 pixels
Global $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap_Scaled) ; This should convert the GDI+ to a GDI image

Global $hWnd = GUICreate("Display image from memory by UEZ 2010", 2 * $iWidth, 2 * $iHeight)
GUISetBkColor(0x264869)
Global $iPic = GUICtrlCreatePic("", 0, 0, 2 * $iWidth, 2 * $iHeight)

GUICtrlSendMsg($iPic, 0x0172, $IMAGE_BITMAP, $hBitmap) ; Should show pic in pic control

GUISetState(@SW_SHOW)


GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            _WinAPI_DeleteObject($hBitmap)
            _GDIPlus_ImageDispose($hBitmap_Scaled)
            _GDIPlus_ImageDispose($hImage)
            _GDIPlus_Shutdown()
            GUIDelete($hWnd)
            Exit
    EndSwitch
WEnd

 

Edited by UEZ
1 person likes 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!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

That works like a charm :huggles:

Thank you so much :)

Cheers

/Rex

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