Sign in to follow this  
Followers 0
GodlessSinner

Delete image after use

5 posts in this topic

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

Global $hBitmap, $hImage, $hGraphic, $hFamily, $hFont, $tLayout, $hFormat, $aInfo, $hBrush
Global $img, $img2, $text, $textx, $texty, $fontsize, $pic

_GDIPlus_StartUp()
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 508, 245, 270, 524)
$Pic1 = GUICtrlCreatePic("", 24, 56, 401, 153, BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS))
$Load = GUICtrlCreateButton("Load an image", 304, 16, 161, 25, $WS_GROUP)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Load
            addtext(@ScriptDir & "\0.jpg", @ScriptDir & "\9.jpg", "text1", 10, 8, 16, $Pic1)
    EndSwitch
WEnd


Func addtext($img, $img2, $text, $textx, $texty, $fontsize, $pic)

$hImage   = _GDIPlus_ImageLoadFromFile($img)
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
$hBrush = _GDIPlus_BrushCreateSolid (0xFF00007F)
$hFormat = _GDIPlus_StringFormatCreate ()
$hFamily = _GDIPlus_FontFamilyCreate ("Arial")
$hFont = _GDIPlus_FontCreate ($hFamily, $fontsize, 1)
$tLayout = _GDIPlus_RectFCreate ($textx, $texty, 0, 0)
$aInfo = _GDIPlus_GraphicsMeasureString ($hGraphic, $text, $hFont, $tLayout, $hFormat)
_GDIPlus_GraphicsDrawStringEx ($hGraphic, $text, $hFont, $aInfo[0], $hFormat, $hBrush)

_GDIPlus_ImageSaveToFile($hImage, $img2)
GUICtrlSetImage($pic, $img2)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()
FileDelete(@ScriptDir & "\0.jpg"); doesn't deletes:(
EndFunc

After _GDIPlus_ImageDispose($hImage) & _GDIPlus_Shutdown() 0.jpg still in use, how to delete?


_____________________________________________________________________________

Share this post


Link to post
Share on other sites



While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Load
            addtext(@ScriptDir & "\0.jpg", @ScriptDir & "\9.jpg", "text1", 10, 8, 16, $Pic1)
            sleep(5000) ; wait 5 seconds 
            GUICtrlDelete($Pic1)
    EndSwitch
WEnd


[size="5"] [/size]

Share this post


Link to post
Share on other sites

_GDIPlus_GraphicsDispose($hGraphic)

_GDIPlus_ImageDispose($hImage)

- thank's, works, but what to do in this case:

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

Global Const $AC_SRC_ALPHA      = 1

$hGUI2 = GUICreate("", 400, 300, -1, -1, -1, $WS_EX_LAYERED);, $hGUI1)
GUISetCursor (9, 1)
GUISetState(@SW_SHOW, $hGUI2)

_GDIPlus_Startup()

$hImage = _GDIPlus_ImageLoadFromFile(@DocumentsCommonDir & "\1.png")
SetBitMap($hGUI2, $hImage, 255)


While 1
    Sleep(100)
    WEnd

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", $AC_SRC_ALPHA)
  _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 10, $pBlend, $ULW_ALPHA)

  _WinAPI_ReleaseDC   (0, $hScrDC)
  _WinAPI_SelectObject($hMemDC, $hOld)
  _WinAPI_DeleteObject($hBitmap)
  _WinAPI_DeleteDC    ($hMemDC)
    ;_GDIPlus_BitmapDispose($hBitmap)
  ;_GDIPlus_ImageDispose($hImage)
  FileDelete(@DocumentsCommonDir & "\1.png")
EndFunc

Tried _GDIPlus_BitmapDispose($hBitmap) & _GDIPlus_ImageDispose($hImage) - script crashing.


_____________________________________________________________________________

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Works for me:

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

Global Const $AC_SRC_ALPHA      = 1

$hGUI2 = GUICreate("", 400, 300, -1, -1, -1, $WS_EX_LAYERED);, $hGUI1)
GUISetCursor (9, 1)
GUISetState(@SW_SHOW, $hGUI2)

_GDIPlus_Startup()

$hImage = _GDIPlus_ImageLoadFromFile(@DocumentsCommonDir & "\1.png")
SetBitMap($hGUI2, $hImage, 255)
_GDIPlus_BitmapDispose($hImage)
FileDelete(@DocumentsCommonDir & "\1.png")


Do
Until GUIGetMsg() = -3

GUIDelete()
_GDIPlus_Shutdown()
Exit

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", $AC_SRC_ALPHA)
  _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 10, $pBlend, $ULW_ALPHA)

  _WinAPI_ReleaseDC   (0, $hScrDC)
  _WinAPI_SelectObject($hMemDC, $hOld)
  _WinAPI_DeleteObject($hBitmap)
  _WinAPI_DeleteDC    ($hMemDC)
EndFunc

Edit: _GDIPlus_BitmapDispose and _GDIPlus_ImageDispose both call the GdipDisposeImage function. Because Bitmap is a descendant of Image, Bitmap objects are delete by the parent destructor. One thing to not confuse is that _WinAPI_DeleteObject deletes HBITMAP objects and not a Bitmap object (the former is GDI's and the latter is GDI+'s).

Edited by Authenticity

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
Sign in to follow this  
Followers 0