Jump to content

Using GDI+, how can you destory the image?


 Share

Recommended Posts

I modified the example in the help file....

Essentially, everytime the function runs, it can read a $Model that gets returned earlier, and display that image.  However, the previous image does not seem to go away.

_GDIPlus_GraphicsDispose($g_hGraphic)
_GDIPlus_ImageDispose($g_hImage)
 

Do not seem to work, and $RDW_ERASENOW seems to have do nothing.

; Draw PNG image
Func MY_WM_PAINT($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam
If _GDIPlus_ImageLoadFromFile("C:\Images\Raw\" & $PModel & ".png") = 0 Then
   $g_hImage = _GDIPlus_ImageLoadFromFile("C:\Images\Raw\noimage.png")
   Else
   $g_hImage = _GDIPlus_ImageLoadFromFile("C:\images\Raw\" & $PModel & ".png")
   EndIf

$g_hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_ERASENOW)
    _GDIPlus_GraphicsDrawImage($g_hGraphic, $g_hImage, 100, 115)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_VALIDATE)
    ; Clean up resources
_GDIPlus_GraphicsDispose($g_hGraphic)
_GDIPlus_ImageDispose($g_hImage)

    Return $GUI_RUNDEFMSG
EndFunc   ;==>MY_WM_PAINT
Link to comment
Share on other sites

_WinAPI_DeleteObject

?

My recommendation as well.

Try redrawing the window after you have deleted the objects.

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

I am sure that I am doing something incorrectly.  So I have

_WinAPI_DeleteObject($g_hImage)
_WinAPI_DeleteObject($g_hGraphic)

 _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_ERASENOW)

I am sure this is understood, but the problem happens when the previous image is larger than the next....you can still see the overlap

Link to comment
Share on other sites

What are you trying to do?

What you did in the function MY_WM_PAINT is not a good approach!

 

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

How is the query adapted? An inputbox where you type in a path to an image?

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

How is the query adapted? An inputbox where you type in a path to an image?

Br,

UEZ

 

yes that is correct. - Well its an input box where I type a serial number.  The Model Number gets returned in an Array and there is a PNG file with the exact model that I want to display

Edited by wisem2540
Link to comment
Share on other sites

I took out the _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_VALIDATE), and ;_WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_ERASENOW)    which keeps my other data from being destroyed now.  :)

It apprears everything is working as intended if I minimize and restore the window between searches.  or between new images appearing.  Does that still point to a drawing problem?

Edited by wisem2540
Link to comment
Share on other sites

Yes, I think, but I'd heed what UEZ says "What you did in the function MY_WM_PAINT is not a good approach!"

he is the GDI guru around here.

If you're lucky. he'll return with some tips.

be patient.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Here an example which you can work with:

 

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

_GDIPlus_Startup()
Global Const $hGUI = GUICreate("GDI+ Test", 800, 600)
Global Const $iBtn_Load = GUICtrlCreateButton("Load", 10, 10, 60, 60)
Global Const $iInput_Path = GUICtrlCreateInput("", 80, 28, 700, 24)
Global Const $iLabel_Text = GUICtrlCreateLabel("Preview 640x480", 10, 85, 100, 12)
Global Const $iPic_Preview = GUICtrlCreatePic("", 10, 100, 640, 480)
Global Const $iBtn_Exit = GUICtrlCreateButton("Exit", 700, 520, 60, 60)
GUISetState()

Global $sFile

Do
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $iBtn_Exit
            GUIDelete()
            _GDIPlus_Shutdown()
            Exit
        Case $iBtn_Load
            $sFile = FileOpenDialog("Selet an GDI+ supported image", "", "Images (*.bmp;*.jpg;*.png;*.gif;*.tif)", BitOR($FD_FILEMUSTEXIST, $FD_PATHMUSTEXIST), "", $hGUI)
            If @error Then ContinueCase
            GUICtrlSetData($iInput_Path, $sFile)
            LoadAndDisplayImage($sFile, $iPic_Preview)
        Case $iInput_Path
            $sFile = GUICtrlRead($iInput_Path)
            If Not FileExists($sFile) Then ContinueCase
            LoadAndDisplayImage($sFile, $iPic_Preview)
    EndSwitch
Until False

Func LoadAndDisplayImage($sFile, $iCtrl, $bScale = False, $iW = 640, $iH = 480)
    If Not FileExists($sFile) Then Return SetError(1, 0, 0)
    Local Const $hImage = _GDIPlus_ImageLoadFromFile($sFile)
    If @error Then Return SetError(2, 0, 0)
    Local Const $iW_Img = _GDIPlus_ImageGetWidth($hImage), $iH_Img = _GDIPlus_ImageGetHeight($hImage)
    Local Const $hBitmap = _GDIPlus_BitmapCreateFromScan0($iW, $iH)
    Local Const $hGfx = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsSetInterpolationMode($hGfx, 7)
    Local $f
    Switch $bScale
        Case False
            If $iW_Img < $iW And $iH_Img < $iH Then ;loaded image is smaller than preview control
                _GDIPlus_GraphicsDrawImageRect($hGfx, $hImage, ($iW - $iW_Img) / 2, ($iH - $iH_Img) / 2, $iW_Img, $iH_Img)
            Else ;loaded image is larger than preview control
                If $iW_Img > $iH_Img Then
                    $f = $iW / $iW_Img
                    If Not ($iH_Img * $f < $iH) Then $f = $iH / $iH_Img
                Else
                    $f = $iH / $iH_Img
                    If Not ($iW_Img * $f < $iW) Then $f = $iW / $iW_Img
                EndIf
                _GDIPlus_GraphicsDrawImageRect($hGfx, $hImage, ($iW - $iW_Img * $f) / 2, ($iH - $iH_Img * $f) / 2, $iW_Img * $f, $iH_Img * $f)
            EndIf
    EndSwitch
    Local Const $hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
    _GDIPlus_GraphicsDispose($hGfx)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_ImageDispose($hImage)
    _WinAPI_DeleteObject(GUICtrlSendMsg($iCtrl, 0x0172, 0x0000, 0)) ;delete previous image in pic control
    _WinAPI_DeleteObject(GUICtrlSendMsg($iCtrl, 0x0172, 0x0000, $hHBitmap))
    _WinAPI_DeleteObject($hHBitmap)
EndFunc
 

For $bScale = True the code isn't implemented yet. ;)

 

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

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