Jump to content
Sign in to follow this  
myspacee

Image viewer with some request

Recommended Posts

myspacee

Hello,

to automatize a process i want to drag&drop an image on a GUI and cut a selected area.

try to write a simple image viewer :

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("Image Viewer", 615, 438, 192, 124)
$Group1 = GUICtrlCreateGroup("Image Viewer", 8, 8, 601, 385)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button1 = GUICtrlCreateButton("Load", 328, 400, 75, 25)
$Input1 = GUICtrlCreateInput("", 8, 400, 265, 21)
$Image=GUICtrlCreatePic('',9,9,600,384)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $Button1
   $file = FileOpenDialog("Choose image...", @TempDir, "All (*.*)")
   GUICtrlSetData($Input1, $file)
   _setImage($Image,$file)
EndSwitch
WEnd
Func _setImage($Control,$Image)
If GUICtrlSetImage($Control,$Image)=0 Then Return @error
Return 0
EndFunc

 

Two request :

- enable drag&drop for GUI ( i resize long side image to 1024px, for every image dragged, so we have 'static' measures )

- create a trasparent windows floating over viewer to let me see what i cut (using printscreen)

 

I know last is a big request but this let me speed a very long work activity

 

Thank you,

m.

 

 

 

 

 

Share this post


Link to post
Share on other sites
myspacee

Hello,

make little steps :

(sorry for spaghetti)

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

#include <GUIConstantsEx.au3>
#include <WinAPISys.au3>
#include <WindowsConstants.au3>

#include <MsgBoxConstants.au3>
#include <WinAPI.au3>
#include <Array.au3>

#include <GuiEdit.au3>

#include <GDIPlus.au3>
#include <String.au3>
#include <misc.au3>
#include <ScreenCapture.au3>


;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;//     Var
;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
global $viewer_pic_Width = 600
global $viewer_pic_Height = 384

global $viewer_pic_left = 9
global $viewer_pic_top = 9



;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;//     GUI - viewer
;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$Viewer = GUICreate("Image Viewer", 615, 438, 192, 124)
$Group1 = GUICtrlCreateGroup("Image Viewer", 8, 8, 601, 385)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button1 = GUICtrlCreateButton("Load", 328, 400, 75, 25)
$Input1 = GUICtrlCreateInput("", 8, 400, 265, 21)
$Image=GUICtrlCreatePic('', $viewer_pic_left, $viewer_pic_top, $viewer_pic_Width, $viewer_pic_Height)
GUISetState(@SW_SHOW)



;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;//     GUI - WinSetTrans
;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Global $GUI_selector, $CopyWin, $PixelArray, $CopyWin, $e, $d, $PixelsReady = 0, $Start = 0
Global $my_Width, $my_Height, $my_Width_parameterized, $my_Height_parameterized, $Ext = "jpg"
dim $apos[4]
Opt("GUIResizeMode", 1)
Opt("WinTitleMatchMode", 2)
$GUI_selector = GUICreate("Floating_selector", $viewer_pic_Width, $viewer_pic_Height, -1, -1, BitOR($WS_POPUP, $WS_SIZEBOX, $WS_CLIPCHILDREN), $WS_EX_TOPMOST)
WinSetTrans("Floating_selector", "", 100)
GUISetState(@SW_HIDE)





While 1
   $nMsg = GUIGetMsg()
   Switch $nMsg
      Case $GUI_EVENT_CLOSE
      Exit
   Case $Button1
      $file = FileOpenDialog("Choose image...", @TempDir, "All (*.*)")
      GUICtrlSetData($Input1, $file)
      _setImage($Image,$file)


      $my_Width_parameterized = $viewer_pic_Width
      $my_Height_parameterized = $viewer_pic_Height


      Save2JpgAlt()

   EndSwitch
WEnd









;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;  Func
;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Func _setImage($Control,$Image)
   If GUICtrlSetImage($Control,$Image)=0 Then Return @error
   Return 0
EndFunc



;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;  SALVO Jpg - QUANDO PREMO ALT
;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Func Save2JpgAlt()

    $aMPos = MouseGetPos()
    $GuisInfo = _WinInfoFromPoint($aMPos[0], $aMPos[1])
    $apos = WinGetPos("Floating_selector")
    $apos_viewer = WinGetPos("Viewer")


    ;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ;  aiuto utente e ricreo Floating_selector - porto altezza e base = in modo che la Floating_selector risulti quadrata (vince l'altezza)
    ;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    GUIDelete("Floating_selector")
    $GUI_selector = GUICreate("Floating_selector", $apos_viewer[2]-$viewer_pic_left, $apos_viewer[3]-$viewer_pic_top, $apos_viewer[0]+$viewer_pic_left, $apos_viewer[1]+$viewer_pic_top, BitOR($WS_POPUP, $WS_SIZEBOX, $WS_CLIPCHILDREN), $WS_EX_TOPMOST)
    WinSetTrans("Floating_selector", "", 100)
    GUISetState(@SW_SHOW, $GUI_selector)

    Sleep(1000) ; Sleep to give tooltip time to display
    ToolTip("", 0, 0)
    ;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ;  catturo area selezionata
    ;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    _ScreenCapture_Capture(@ScriptDir & "\temp.jpg", $apos[0], $apos[1], $apos[0] + $apos[3], $apos[1] + $apos[3], False)

    ;_ScreenCapture_Capture("temp.jpg", $apos[0], $apos[1], $apos[0] + $apos[2], $apos[1] + $apos[3], False)
    Sleep(500)
    ;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ;// calcolo altezza e base del .jpg appena catturato per successivo resize
    ;// dell'immagine alle dimensioni da me specificate ($my_Width_parameterized e $my_Height_parameterized)
    ;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    $my_Width = ($apos[0] + $apos[3]) - $apos[0]
    $my_Height = ($apos[1] + $apos[3]) - $apos[1]
    ;ShellExecute('temp.jpg')
    _resizeME($my_Width_parameterized, $my_Height_parameterized)
EndFunc   ;==>Save2JpgAlt






Func _resizeME($my_Width_parameterized, $my_Height_parameterized)
    ;Start up GDIPlus
    _GDIPlus_Startup ()

    ;Create canvas to put the resized image onto
    ;$hBMP = _CreateBitmap($oSize[1], $oSize[2])
    $hBMP = _CreateBitmap($my_Width_parameterized, $my_Height_parameterized)
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)

    ;Load an image file that you want to resize
    $hImage2 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\temp.jpg")
    $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage1)

    ;Put 2nd image onto the canvas at the desired size
     _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hImage2, 0, 0, $my_Width, $my_Height, 0, 0, $my_Width_parameterized, $my_Height_parameterized)
    ;_GDIPlus_GraphicsDrawImageRectRect ($hGraphic2, $hImage, 0, 0, 200, 200, 0, 0, 70, 70)

    ;The encoder to use for the output image
    $CLSID = _GDIPlus_EncodersGetCLSID(StringUpper($Ext))

    ;Save the new resized image to a file
    ;~ _GDIPlus_ImageSaveToFileEx($hImage1, '"' & $sPath & "cool." & '"'  & StringLower($Ext), $CLSID)
    _GDIPlus_ImageSaveToFileEx($hImage1,@ScriptDir & '\Temp_resized.jpg', $CLSID)

    ;Clean up resources
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_ImageDispose($hImage2)
    _GDIPlus_GraphicsDispose($hGraphics)
    _WinAPI_DeleteObject($hBMP)

    ;Shut down GDIPlus
    _GDIPlus_Shutdown()

EndFunc   ;==>_Main






Func _CreateBitmap($iW, $iH)
    Local $hWnd, $hDDC, $hCDC, $hBMP
    $hWnd = _WinAPI_GetDesktopWindow()
    $hDDC = _WinAPI_GetDC($hWnd)
    $hCDC = _WinAPI_CreateCompatibleDC($hDDC)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDDC, $iW, $iH)
    _WinAPI_ReleaseDC($hWnd, $hDDC)
    _WinAPI_DeleteDC($hCDC)
    Return $hBMP
EndFunc  ;==>_CreateBitmap



Func _screenShotActiveWindow($path)
    Local $opt = AutoItSetOption("WinTitleMatchMode", 4)
    _ScreenCapture_CaptureWnd($path, WinGetHandle(WinGetTitle('active')))
    AutoItSetOption("WinTitleMatchMode", $opt)
EndFunc  ;==>_screenShotActiveWindow




;Return :   $GuisInfo[0] = Window Text
;         $GuisInfo[1] = Class Name
;         $GuisInfo[2] = Handle of windowWnd
;         $GuisInfo[3] = Owner Class Name
;         $GuisInfo[4] = Handle of Owner Window
; A modified Siao Function
Func _WinInfoFromPoint($nX, $nY)
    Local $tStrBuff, $pStrBuff, $aRet, $hWnd, $Info[5]
    $tStrBuff = DllStructCreate("char[150]")
    $pStrBuff = DllStructGetPtr($tStrBuff)
    $aRet = DllCall("user32.dll", "hwnd", "WindowFromPoint", "uint", $nX, "uint", $nY)
    $Info[2] = $aRet[0] ;$hWnd
    $aRet = DllCall("user32.dll", "int", "GetClassName", "hwnd", $Info[2], "ptr", $pStrBuff, "int", 100)
    $Info[1] = DllStructGetData($tStrBuff, 1) ; ClassName
    DllStructSetData($tStrBuff, 1, "")
    DllCall("user32.dll", "int", "GetWindowText", "hwnd", $Info[2], "ptr", $pStrBuff, "int", 100)
    $Info[0] = DllStructGetData($tStrBuff, 1) ; Window Text
    DllStructSetData($tStrBuff, 1, "")
    $aRet = DllCall("user32.dll", "hwnd", "GetAncestor", "hwnd", $Info[2], "uint", 2) ;$GA_ROOT = 2
    $Info[4] = $aRet[0] ; Handle of Owner Window
    $aRet = DllCall("user32.dll", "int", "GetClassName", "hwnd", $Info[4], "ptr", $pStrBuff, "int", 100)
    $Info[3] = DllStructGetData($tStrBuff, 1) ;Owner ClassName
    DllStructSetData($tStrBuff, 1, "")
    ;ConsoleWrite("text= " &$Info[0] & @CRLF)
    Return $Info
EndFunc   ;==>_WinInfoFromPoint

; Drag Window 3 of 3 addin
Func _WinMove($hWnd, $Command, $wParam, $lParam)
    If BitAND(WinGetState($hWnd), 32) Then Return $GUI_RUNDEFMSG
    ;DllCall("user32.dll", "long", "SendMessage", "hwnd", $HWnd, "int", $WM_SYSCOMMAND, "int", 0xF009, "int", 0)
    DllCall("user32.dll", "int", "SendMessage", "hWnd", $hWnd, "int", $WM_NCLBUTTONDOWN, "int", $HTCAPTION, "int", 0)
    Return 1
EndFunc   ;==>_WinMove

try to obtain some info about image area ($image), interested on its updated coordinates.

To superimpose transparent gui i must know its exact position ( user might have moved the viewer window)

 

thank you for any help,

m.

Share this post


Link to post
Share on other sites
UEZ

Have a look to https://www.autoitscript.com/forum/topic/170461-visual-image-crop-gui/ which looks similar to your project.


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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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  

×