Jump to content

Clear a graphic from screen


jcpetu
 Share

Recommended Posts

Hi people,

I'm testing some graphic functions to enhance pictures and photos. In this case I got an example from Help file to transform images in negative colors and I use a function from UEZ to resize it. Up to here evrything is OK, but when I try to draw a new picture I can't erase the previously one.

I attempted with _WinAPI_RedrawWindow(WinGetHandle, GDIPlus_GraphicsClear and _GDIPlus_GraphicsDrawImageRectRect without success, and I wonder if anyone could help me.

Thanks in advance and regards.

 

#include <APIConstants.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>
#include <WinAPI.au3>
;------------------------------------------------

Global $PWidth = 600, $PHeight = 384
Global $hGUI = GUICreate("Image Inverter", 615, 438, -1, -1)
Global $BLoadFile = GUICtrlCreateButton("Load Image File", 328, 400, 125, 25)
GUISetState(@SW_SHOW)

_GDIPlus_Startup() ;initialize GDI+
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _GDIPlus_Shutdown()
            GUIDelete($hGUI)
            Exit
        Case $BLoadFile
            DrawImage()
    EndSwitch
WEnd

Func DrawImage()
    $file = FileOpenDialog("Select image file", @ScriptDir, "Images (*.bmp;*.ico;*.jpg)")
    $hImage = _GDIPlus_ImageLoadFromFile($file)
    $Width = _GDIPlus_ImageGetWidth($hImage)
    $Height = _GDIPlus_ImageGetHeight($hImage)

    ;Resize image to fit in screen----------------------------------------------------------
    $P2Width = $PWidth
    If $Width <= $PWidth Then $P2Width = $Width
    $P2Height = $PHeight
    If $Height <= $PHeight Then $P2Height = $Height
    $hBitmap = _GDIPlus_ScaleImage($file, $P2Width, $P2Height)
    $hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)

    ;Prepare graphic context and invert colors----------------------------------------------
    Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND(WinGetHandle($hGUI)) ;create a graphics object from a window handle
    Local $hIA = _GDIPlus_ImageAttributesCreate() ;create an ImageAttribute object
    Local $tColorMatrix = _GDIPlus_ColorMatrixCreateNegative() ;create negative color matrix
    _GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $tColorMatrix) ;set negative color matrix

    ;=====>>>Delete previous image------------------------------------------------------------------
    _WinAPI_RedrawWindow(WinGetHandle($hGUI), 0, 0, $RDW_ERASENOW )
    ;_GDIPlus_GraphicsClear($hGraphics) ;clear bitmap for repaint
    ;_GDIPlus_GraphicsDrawImageRectRect($hGraphics, "", 0, 0, $PWidth, $PHeight, 0, 0, $PWidth, $PHeight, $hIA) ;draw the bitmap while applying the color adjustment
    ;=====>>>Delete previous image------------------------------------------------------------------

    ;Show image ----------------------------------------------------------------------------
    _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hBitmap, 0, 0, $PWidth, $PHeight, 0, 0, $PWidth, $PHeight, $hIA) ;draw the bitmap while applying the color adjustment
    _WinAPI_DeleteObject($hImage) ;release GDI bitmap resource because not needed anymore
    _WinAPI_DeleteObject($hHBitmap)
    _GDIPlus_ImageAttributesDispose($hIA)
    _GDIPlus_GraphicsDispose($hGraphics)
    Return
EndFunc   ;==>DrawImage


Func _GDIPlus_ScaleImage($sFile, $iW, $iH, $iInterpolationMode = 7) ;coded by UEZ 2012
    If Not FileExists($sFile) Then Return SetError(1, 0, 0)
    Local $hImage = _GDIPlus_ImageLoadFromFile($sFile)
    If @error Then Return SetError(2, 0, 0)
    Local $hBitmap = DllCall($__g_hGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iW, "int", $iH, "int", 0, "int", 0x0026200A, "ptr", 0, "int*", 0)
    If @error Then Return SetError(3, 0, 0)
    $hBitmap = $hBitmap[6]
    Local $hBmpCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    DllCall($__g_hGDIPDll, "uint", "GdipSetInterpolationMode", "handle", $hBmpCtxt, "int", $iInterpolationMode)
    _GDIPlus_GraphicsDrawImageRect($hBmpCtxt, $hImage, 0, 0, $iW, $iH)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_GraphicsDispose($hBmpCtxt)
    Return $hBitmap
EndFunc   ;==>_GDIPlus_ScaleImage

 

Link to comment
Share on other sites

_GDIPlus_GraphicsClear will clear the entire graphic object that you drew. After you clear it you then need to re-draw (or in your case just draw) everything that you want shown. The reason it cleared the button is because the graphic object is the size of the whole GUI and you're not using _WinApi_RedrawWindow Properly.

  1. $gGUI is already a Window Handle
  2. 0, 0 are coordinates, not a tagRect variable nor a
  3. You don't need the flag
;=====>>>Delete previous image------------------------------------------------------------------
    _GDIPlus_GraphicsClear($hGraphics)
    ;=====>>>Delete previous image------------------------------------------------------------------

    ;=====>>>Redraw the main GUI -------------------------------------------------------------------
    _WinAPI_RedrawWindow($hGUI)
    ;=====>>>Redraw the main GUI -------------------------------------------------------------------

Try this

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