Jump to content

filedelete file in use


Flum
 Share

Recommended Posts

#include <GDIPlus.au3>
#include <ScreenCapture.au3>
#include <File.au3>
#include <Array.au3>
#include <MsgBoxConstants.au3>
#include <GuiConstantsEx.au3>
#include <WinAPI.au3>

global $possibility1
global $possibility2
global $dek[3] = ["sheet1", "sheet2", "sheet3"]



HotKeySet("{ESC}", "_Terminate")

While 1
sleep(1000)
local $color = PixelGetColor (1376 , 653)
if ($color <> "6590886") then


FileDelete ("1.jpg")
FileDelete ("2.jpg")


_GDIPlus_Startup()
sleep (1000)
_ScreenCapture()
sleep (1000)
For $i = 0 To UBound($dek) -1
   $excelsheet = $dek[$i]
   $bm1 = _GDIPlus_ImageLoadFromFile("1.jpg")
   ConsoleWrite(@ScriptDir & "/links/" & $excelsheet & ".jpg" & @CRLF)
   $bm2 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "/links/" & $excelsheet & ".jpg")
$antwoord = CompareBitmaps($bm1, $bm2)



if ($antwoord = "True") Then
   ;MsgBox(0, "Alert", "We hebben je excelsheet gedetecteerd je excelsheet = " & $excelsheet)
   $possibility1 = $excelsheet

   _GDIPlus_ImageDispose($bm1)
_GDIPlus_ImageDispose($bm2)
 FileClose($bm1)
  FileClose($bm2)
_GDIPlus_Shutdown()
_GDIPlus_Startup()
   ExitLoop
   EndIf

;;MsgBox(0, "bm1==bm2", CompareBitmaps($bm1, $bm2))
Next

For $i = 0 To UBound($dek) -1

   $excelsheet = $dek[$i]
   $bm1 = _GDIPlus_ImageLoadFromFile("2.jpg")
   ConsoleWrite(@ScriptDir & "/rechts/" & $excelsheet & ".jpg" & @CRLF)
   $bm2 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "/rechts/" & $excelsheet & ".jpg")
$antwoord = CompareBitmaps($bm1, $bm2)
if ($antwoord = "True") Then
   ;MsgBox(0, "Alert", "We hebben je excelsheet gedetecteerd je excelsheet = " & $excelsheet)
   $possibility2 = $excelsheet

_GDIPlus_ImageDispose($bm1)
_GDIPlus_ImageDispose($bm2)
 FileClose($bm1)
  FileClose($bm2)
_GDIPlus_Shutdown()

   ExitLoop
   EndIf

;;MsgBox(0, "bm1==bm2", CompareBitmaps($bm1, $bm2))
Next

ConsoleWrite($possibility1 & " - " & $possibility2 & @CRLF)



sleep (1000)
;MouseClick ($MOUSE_CLICK_LEFT, 1042, 808, 1)
endif
WEnd

Func _ScreenCapture()
    Local $hBitmap, $hBitmap2, $hImage, $hImage2

    _GDIPlus_Startup()

    ; Capture full screen
    $hBitmap = _ScreenCapture_Capture(false, 1454, 672, 1454+11, 672+28)
    $hBitmap2 = _ScreenCapture_Capture(false, 1477, 671, 1477+15, 672+45)
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)
    $hImage2 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap2)
    ; Save resultant image
    _GDIPlus_ImageSaveToFile($hImage, @ScriptDir & "\1.jpg")
        _GDIPlus_ImageSaveToFile($hImage2, @ScriptDir & "\2.jpg")

    ; Clean up resources
    _GDIPlus_ImageDispose ($hImage)
   _GDIPlus_ImageDispose ($hImage2)
    _WinAPI_DeleteObject ($hBitmap)
        _WinAPI_DeleteObject ($hBitmap2)
    _GDIPlus_ShutDown ()
 EndFunc   ;==>_Main

Func CompareBitmaps($bm1, $bm2)

    $Bm1W = _GDIPlus_ImageGetWidth($bm1)
    $Bm1H = _GDIPlus_ImageGetHeight($bm1)
    $BitmapData1 = _GDIPlus_BitmapLockBits($bm1, 0, 0, $Bm1W, $Bm1H, $GDIP_ILMREAD, $GDIP_PXF32RGB)
    $Stride = DllStructGetData($BitmapData1, "Stride")
    $Scan0 = DllStructGetData($BitmapData1, "Scan0")

    $ptr1 = $Scan0
    $size1 = ($Bm1H - 1) * $Stride + ($Bm1W - 1) * 4


    $Bm2W = _GDIPlus_ImageGetWidth($bm2)
    $Bm2H = _GDIPlus_ImageGetHeight($bm2)
    $BitmapData2 = _GDIPlus_BitmapLockBits($bm2, 0, 0, $Bm2W, $Bm2H, $GDIP_ILMREAD, $GDIP_PXF32RGB)
    $Stride = DllStructGetData($BitmapData2, "Stride")
    $Scan0 = DllStructGetData($BitmapData2, "Scan0")

    $ptr2 = $Scan0
    $size2 = ($Bm2H - 1) * $Stride + ($Bm2W - 1) * 4

    $smallest = $size1
    If $size2 < $smallest Then $smallest = $size2

    $call = DllCall("msvcrt.dll", "int:cdecl", "memcmp", "ptr", $ptr1, "ptr", $ptr2, "int", $smallest)



    _GDIPlus_BitmapUnlockBits($bm1, $BitmapData1)
    _GDIPlus_BitmapUnlockBits($bm2, $BitmapData2)

    Return ($call[0]=0)


EndFunc  ;==>CompareBitmaps

 Func _Terminate()
    Exit
EndFunc   ;==>_Terminate

Goodmorning, i wrote a little script that makes 2 screenshots 1.jpg and 2.jpg and compare them seperatly with 3 other screenshots $dek[3]. I got it working perfectly the only problem i face is that it wont delete 1.jpg and 2.jpg to start a new batch. I found out it is because file 1.jpg and 2.jpg are in use. So i tried a couple of things myself:

_GDIPlus_ImageDispose($bm1)
_GDIPlus_ImageDispose($bm2)
 FileClose($bm1)
  FileClose($bm2)
_GDIPlus_Shutdown()
_GDIPlus_Startup()

but the files wont delete, because they are still in use. Does anyone have a clue how i can solve it. I thought about a workarround myself of storing the images in the windows temporary folder. But i rather have them saved where they are.

 

Yours sincerely, Chris and have a great day

 

 

Link to comment
Share on other sites

If you save your file to a network share (some file server) you could search for and forcibly close this NETWORK file handle.

 

This example should give you a start to do so:

 

 

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Not sure if I remember this correctly, but I think GDI is somehow lazy releasing the handles, sometimes it takes a few ms after closing the gdi handles to release the underlying file handles. Try something like this:

 

_GDI_FileDelete_Ex(@ScriptDir & "\1.jpg")

Func _GDI_FileDelete_Ex($sFile)
    Local $i_Timer_FileDelete = TimerInit()
    While FileExists($sFile)
        FileDelete($sFile)
        Sleep(10)
        If TimerDiff($i_Timer_FileDelete) > 5000 Then
            MsgBox(0, "Error", "File " & $sFile & " was not deleted in 5 seconds")
            Return SetError(1)
        EndIf
    WEnd
    Return 1
EndFunc   ;==>_GDI_FileDelete_Ex

 

Link to comment
Share on other sites

Hi Flum,
I got 2 remarks concerning your script :

1) _GDIPlus_Startup() and _GDIPlus_Shutdown() shouldn't be used and reused constantly within the While 1 loop.
Placing _GDIPlus_Startup() at the very start of the script, then _GDIPlus_Shutdown() at its end seems safer.

2) Return ($call[0]=0)
This return should be read as boolean and not as a string like in the script : If ($antwoord = "True")
Let's try it on a simple script :

$Ret = MyFunction()
MsgBox(0, "Test 1", VarGetType($Ret) & "   " & $Ret)

If $Ret = "True" Then MsgBox(0,"Test 2", "$Ret = True")
If $Ret = "False" Then MsgBox(0,"Test 3", "$Ret = False")

Func MyFunction()
    Return (1=1)
EndFunc

See how the displayed results are wrong ?
* Return (1=1) activates If $Ret = "False" (test 3) when it shouldn't
* And if you change the script to Return (1<>1) then If $Ret = "False" (test 3) isn't activated when it should.

What would work are tests based on If $Ret = True , then If $Ret = False (i.e boolean tests, not string tests)

Try this change in your script, from string test to boolean test, though I'm not sure it will solve your issue because your test If ($antwoord = "True") is always activated when Return ($call[0]=0) is True, and gladly you never tested If ($antwoord = "False") which would have been activated too when Return ($call[0]=0) is True, as shown in the preceding example script.

If this change doesn't make it, let's hope Rudi's or Kafu's answers will.
Good luck :)

Edited by pixelsearch
Link to comment
Share on other sites

Tried to clean-up the code a little based on pixelsearch's remarks. Through the loops you created different handles for the same vars never been closed.

 

#include <GDIPlus.au3>
#include <ScreenCapture.au3>
#include <File.au3>
#include <Array.au3>
#include <MsgBoxConstants.au3>
#include <GuiConstantsEx.au3>
#include <WinAPI.au3>

Global $possibility1
Global $possibility2
Global $dek[3] = ["sheet1", "sheet2", "sheet3"]

_GDIPlus_Startup()

HotKeySet("{ESC}", "_Terminate")

While 1
    Sleep(1000)
    Local $color = PixelGetColor(1376, 653)
    If ($color <> "6590886") Then

        FileDelete(@ScriptDir & "\1.jpg")
        FileDelete(@ScriptDir & "\2.jpg")

        Sleep(1000)
        _ScreenCapture()
        Sleep(1000)
        
        $bm1 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\1.jpg")
        
        For $i = 0 To UBound($dek) - 1
            $excelsheet = $dek[$i]
            
            ConsoleWrite(@ScriptDir & "/links/" & $excelsheet & ".jpg" & @CRLF)
            $bm2 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "/links/" & $excelsheet & ".jpg")
            $antwoord = CompareBitmaps($bm1, $bm2)
            _GDIPlus_ImageDispose($bm2)

            If $antwoord = True Then
                ;MsgBox(0, "Alert", "We hebben je excelsheet gedetecteerd je excelsheet = " & $excelsheet)
                $possibility1 = $excelsheet
                ExitLoop
            EndIf

            ;;MsgBox(0, "bm1==bm2", CompareBitmaps($bm1, $bm2))
        Next
        
        _GDIPlus_ImageDispose($bm1)


        $bm1 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\2.jpg")

        For $i = 0 To UBound($dek) - 1

            $excelsheet = $dek[$i]
            
            ConsoleWrite(@ScriptDir & "/rechts/" & $excelsheet & ".jpg" & @CRLF)
            $bm2 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "/rechts/" & $excelsheet & ".jpg")
            $antwoord = CompareBitmaps($bm1, $bm2)
            _GDIPlus_ImageDispose($bm2)
            
            If $antwoord = True Then
                ;MsgBox(0, "Alert", "We hebben je excelsheet gedetecteerd je excelsheet = " & $excelsheet)
                $possibility2 = $excelsheet

                ExitLoop
            EndIf

            ;;MsgBox(0, "bm1==bm2", CompareBitmaps($bm1, $bm2))
        Next
        
        _GDIPlus_ImageDispose($bm1)

        ConsoleWrite($possibility1 & " - " & $possibility2 & @CRLF)

        Sleep(1000)
        ;MouseClick ($MOUSE_CLICK_LEFT, 1042, 808, 1)
    EndIf
WEnd

Func _ScreenCapture()
    Local $hBitmap, $hBitmap2, $hImage, $hImage2

    ; Capture full screen
    Local $hBitmap = _ScreenCapture_Capture(False, 1454, 672, 1454 + 11, 672 + 28)
    Local $hBitmap2 = _ScreenCapture_Capture(False, 1477, 671, 1477 + 15, 672 + 45)
    
    Local $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)
    Local $hImage2 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap2)
    
    ; Save resultant image
    _GDIPlus_ImageSaveToFile($hImage, @ScriptDir & "\1.jpg")
    _GDIPlus_ImageSaveToFile($hImage2, @ScriptDir & "\2.jpg")

    ; Clean up resources
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_ImageDispose($hImage2)
    
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteObject($hBitmap2)

EndFunc   ;==>_ScreenCapture

Func CompareBitmaps($bm1, $bm2)

    Local $Bm1W = _GDIPlus_ImageGetWidth($bm1)
    Local $Bm1H = _GDIPlus_ImageGetHeight($bm1)
    
    Local $BitmapData1 = _GDIPlus_BitmapLockBits($bm1, 0, 0, $Bm1W, $Bm1H, $GDIP_ILMREAD, $GDIP_PXF32RGB)
    Local $Stride = DllStructGetData($BitmapData1, "Stride")
    Local $Scan0 = DllStructGetData($BitmapData1, "Scan0")

    Local $ptr1 = $Scan0
    Local $size1 = ($Bm1H - 1) * $Stride + ($Bm1W - 1) * 4

    Local $Bm2W = _GDIPlus_ImageGetWidth($bm2)
    Local $Bm2H = _GDIPlus_ImageGetHeight($bm2)
    Local $BitmapData2 = _GDIPlus_BitmapLockBits($bm2, 0, 0, $Bm2W, $Bm2H, $GDIP_ILMREAD, $GDIP_PXF32RGB)
    $Stride = DllStructGetData($BitmapData2, "Stride")
    $Scan0 = DllStructGetData($BitmapData2, "Scan0")

    Local $ptr2 = $Scan0
    Local $size2 = ($Bm2H - 1) * $Stride + ($Bm2W - 1) * 4

    Local $smallest = $size1
    If $size2 < $smallest Then $smallest = $size2

    Local $call = DllCall("msvcrt.dll", "int:cdecl", "memcmp", "ptr", $ptr1, "ptr", $ptr2, "int", $smallest)

    _GDIPlus_BitmapUnlockBits($bm1, $BitmapData1)
    _GDIPlus_BitmapUnlockBits($bm2, $BitmapData2)

    if $call[0] = 0 Then Return True
    Return False
    
EndFunc   ;==>CompareBitmaps

Func _Terminate()
    _GDIPlus_Shutdown()
    Exit
EndFunc   ;==>_Terminate

 

Link to comment
Share on other sites

@KaFu @rudi @pixelsearch

This makes way more sense, i was loading up the same image trough the for loop. Also loading up _GDIPlus _Startup() and closing it multiple times, not one of my brides ideas.

I am sending you all a bunch of love from The Netherlands.

I will see if i can return the favor and help some other people on the forums as a return!

Have a wonderfull day!

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