Jump to content

[SOLVED] How to clear image as GDI control


nend
 Share

Recommended Posts

I made a test script which sends the png image to a picture control.
I want to use this as a UDF but how do I clear the old image every time I change this image and still use as 1 command ( _Image_to_pic($image, $pic_control) )? 
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>

_GDIPlus_Startup()

Local $hGUI = GUICreate("Test", 400, 300)
Local $iPic = GUICtrlCreatePic("", 0, 0, 41, 36)
_Image_to_pic(_Pausepng(), $iPic)
GUISetBkColor(0xFFFFFF)
GUICtrlSetCursor(-1, 0)

GUISetState(@SW_SHOW)

Sleep(3000)

_Image_to_pic(_Playpng(), $iPic)

Do
    Sleep(10)
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_GDIPlus_Shutdown()

Func _Image_to_pic($image, $pic_control)

    Local $bitmap = _GDIPlus_BitmapCreateFromMemory($image)
    Local $width = _GDIPlus_ImageGetWidth($bitmap)
    Local $height = _GDIPlus_ImageGetHeight($bitmap)

    Local $graphics = _GDIPlus_ImageGetGraphicsContext($bitmap)

    _GDIPlus_GraphicsDrawImageRect($graphics, $bitmap, 0, 0, $width, $height)

    Local $hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($bitmap)
    _WinAPI_DeleteObject(GUICtrlSendMsg($pic_control, 0x0172, 0, $hHBitmap)) ;$STM_SETIMAGE = 0x0172

    _WinAPI_DeleteObject($hHBitmap)
    _GDIPlus_GraphicsDispose($graphics)
    _GDIPlus_BitmapDispose($bitmap)
EndFunc

Func _Pausepng()
    Local $sFileBin = 'iVBORw0KGgoAAAANSUhEUgAAACkAAAAkCAYAAAAU/hMoAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA2lpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEge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+ygtXwQAAAddJREFUeNrsmE1uwkAMhScJSRBZpDtU9QJsuuk1uEdP0U13vUB7A87AJWhPkUXbTYUQEgk/qV9VJOJ4QiYwoZViyYuMHvGHZ+wxOHmeq79urvoH1kF2kB2kbUjHca7JX8lzwSdMG2t0KfmjSdye4Ze6H41Gd+SFxSRJ1Gw2K4l931fj8biwlqZpMJ1OHwj0mXr0pw3IYRRFKgiC4kt6+tdwLZ7jOHbn8/mQHq1AKs/zToL8PQrK5nb/APHAANec4ctAuq5bypwOUpdl65AIwINUBTUFOgskpiY+OVVNUueYsowhd7ud2m63pTWdcW0TcGNIBF2v10dB9sa1rWQSQFmWFdY2m412q7m2lUwCiG6N2tni2mPHo7IQ6jjZCz6i8QnTxhVa+G3duI1akEkVS3rr243mzBu0dE73gP1+XzwCJltuDBmGYSkwgkqQMAwkUsVbhcRdLAVeLBZiJiUtTUB2txuQg8GgVguCce3+/m99wNAFRSYvMmCgMvl5qqpW457Y9rUIeKnR225B76jk1WpVWNRVNoxrkVl6Byg/bN04N+RvSB6dw/zQpRsHvw4F3ZL8ySSu02Teo+AhCpctZ/Su5YEG1XElJOXLOF73118H2UGa27cAAwApWoU8wbQoOQAAAABJRU5ErkJggg=='
    $sFileBin = Binary(_Base64Decode($sFileBin))
    Return $sFileBin
EndFunc   ;==>_Pauzepng

Func _Playpng()
    Local $sFileBin = 'iVBORw0KGgoAAAANSUhEUgAAACkAAAAkCAYAAAAU/hMoAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA2lpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo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uvsOveuu9mBH96j452PM2fOzBmTpmm07e0WXYF2I1JWM2/6ApPJtNH/RdbE9ZlueCsDPoI0UAxXye4+CzQrGAYCAc3n8/EXJfCAtctIX+vGZ9YOBM/tWCyW41gsNrObzSbVajVqNBrfYT4CX7cmJq1W6wyv10vhcJhCodB9j8fzBT99Bve2ISZPRC7w+/0UiUQoGAzuoksOfAB39RApNN0QdZxKpc7sVyqVKJvNqng8AE/BD1nTLSwyk8kIDVgoFCiXy41gvgEvQMswkel0Wnh6ptMp5fN55jfMPfAa9HQXmUwmzx1Lg8GAisUie7cN8+Xcu390E5lIJC4c+L1ej8rlMsftT5jPwVswkSqS82Q8Ht94lXa7XapUKlStViswn4D3moBKYZHRaFRaSmm1WrMNoV6vv8P4a1ek8CloOBxKE+lyucjpdPLjHVl79w53k8win94W2buFPWk2b3b05ME4NeHzE8zHoCj10Mvbot1uv5C4yWRC4/GYVFX9BvMhyOp2Mnc4HOd68Wg0on6/z7kyP/fcka7lA3tSVCQvMM6L4CTNzONQ/xrHZrOt9RznwU6nU4f5bDlhG1KIsScVRTk15trtNuc+3vpegf3Vrc+wapFX5upq5aQMFocI5tellbQsiKdzeXuD94SPY0YUYg5Mt4ayQXO73do8zjjefEYVYmKdiA7BeFEirPx2+dXi0uJR0Ffd6huMfwm8MndBRtxv3lz9/Vci/wowALi8Ftb9p4m2AAAAAElFTkSuQmCC'
    $sFileBin = Binary(_Base64Decode($sFileBin))
    Return $sFileBin
EndFunc   ;==>_Afspelenpng

Func _Base64Decode($input_string) ; by trancexx
    Local $struct = DllStructCreate('int')
    Local $a_Call = DllCall('Crypt32.dll', 'int', 'CryptStringToBinary', 'str', $input_string, 'int', 0, 'int', 1, 'ptr', 0, 'ptr', DllStructGetPtr($struct, 1), 'ptr', 0, 'ptr', 0)
    If @error Or Not $a_Call[0] Then Return SetError(1, 0, '')
    Local $a = DllStructCreate('byte[' & DllStructGetData($struct, 1) & ']')
    $a_Call = DllCall('Crypt32.dll', 'int', 'CryptStringToBinary', 'str', $input_string, 'int', 0, 'int', 1, 'ptr', DllStructGetPtr($a), 'ptr', DllStructGetPtr($struct, 1), 'ptr', 0, 'ptr', 0)
    If @error Or Not $a_Call[0] Then Return SetError(2, 0, '')
    Return DllStructGetData($a, 1)
EndFunc   ;==>_Base64Decode

 

Edited by nend
Link to comment
Share on other sites

Why not add a check right when the function is called? I'm not sure if your question is how to check to remove the image or how to actually delete it from the control. I don't know enough about GDI+ images to delete the pic but I can look around.

Local $pixExists = False
Func _Image_to_pic($image, $pic_control)
    If $picExists Then
        ;delete existing picture from control
    EndIf
    Local $bitmap = _GDIPlus_BitmapCreateFromMemory($image)
    Local $width = _GDIPlus_ImageGetWidth($bitmap)
    Local $height = _GDIPlus_ImageGetHeight($bitmap)

    Local $graphics = _GDIPlus_ImageGetGraphicsContext($bitmap)

    _GDIPlus_GraphicsDrawImageRect($graphics, $bitmap, 0, 0, $width, $height)

    Local $hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($bitmap)
    _WinAPI_DeleteObject(GUICtrlSendMsg($pic_control, 0x0172, 0, $hHBitmap)) ;$STM_SETIMAGE = 0x0172

    _WinAPI_DeleteObject($hHBitmap)
    _GDIPlus_GraphicsDispose($graphics)
    _GDIPlus_BitmapDispose($bitmap)
EndFunc
Edited by anthonyjr2

UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Link to comment
Share on other sites

My question is how to delete the existing image from this control before updating it with a new picture.
If you run the script you see the both image ontop on each other.

I can do this with  _GDIPlus_GraphicsClear but I don't know a way to include this in this script.

Edited by nend
Link to comment
Share on other sites

Try

...
GUISetState(@SW_SHOW)

Sleep(3000)

GUICtrlSendMsg($iPic, 0x0172, 0, 0) ;should delete previous image

_Image_to_pic(_Playpng(), $iPic)

...

 

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