Jump to content

GUICtrlCreatePic from _ScreenCapture_Capture w/o file read/write?


Jags
 Share

Recommended Posts

I'm looking for a way to put a  small magnified section of the screen on to a Gui.
I am doing this now with _ScreenCapture_Capture to a file then read the file with GUICtrlCreatePic.
Is there perhaps another way to do this directly without thrashing my SSD?
Link to comment
Share on other sites

Search for "GUICtrlCreatePic STM_SETIMAGE GUICtrlSendMsg ScreenCapture".

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

Couldn't locate anything yet, this will better show you what I'm doing.  Sure there is a better than doing file read/writes every 250ms.

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)

$Main_Gui = GUICreate("", 110, 110, 500, 550, -1, $WS_EX_TOPMOST)
_GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($Main_Gui)
    $hPen = _GDIPlus_PenCreate()
GUISetState()

GUISetOnEvent($GUI_EVENT_CLOSE, "Terminate")

    While 1
        $pos = MouseGetPos()

            _ScreenCapture_Capture("D:\Clip.jpg", $pos[0]-5,$pos[1]-5, $pos[0]+5,$pos[1]+5,False)
                $n = GUICtrlCreatePic("D:\Clip.jpg", 0, 0, 11,11)
                $n = GUICtrlSetPos($n, 0, 0, 110, 110)

                _GDIPlus_GraphicsDrawLine($hGraphic, 50, 50, 60, 50, $hPen)
                _GDIPlus_GraphicsDrawLine($hGraphic, 60, 50, 60, 60, $hPen)
                _GDIPlus_GraphicsDrawLine($hGraphic, 60, 60, 50, 60, $hPen)
                _GDIPlus_GraphicsDrawLine($hGraphic, 50, 60, 50, 50, $hPen)
            Sleep(250)
    WEnd


    Func Terminate()
        _GDIPlus_PenDispose($hPen)
        _GDIPlus_GraphicsDispose($hGraphic)
        _GDIPlus_Shutdown()
        Exit
        EndFunc   ;==>Terminate
Link to comment
Share on other sites

  • Moderators

Jags,

It is a bit more complicated than you need, but this should give you some clues: ;)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <Misc.au3>

Opt("GUICloseOnESC", 0)

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

Global $hMag_GUI, $hMagDC, $hDeskDC, $hPen, $oObj, $aMouse_Pos[2], $iLast_Mouse_X = 0, $iLast_Mouse_Y = 0

; Create GUI
$hMag_Win = GUICreate("MAG", 100, 100, 0, 0, $WS_POPUP)
GUISetState(@SW_SHOW, $hMag_Win)
$hMag_GUI = WinGetHandle("MAG")
; Get device context for Mag GUI
$hMagDC = _WinAPI_GetDC($hMag_GUI)
If @error Then Exit
; Get device context for desktop
$hDeskDC = _WinAPI_GetDC(0)
If @error Then
    _WinAPI_ReleaseDC($hMag_GUI, $hMagDC)
    Exit
EndIf
; Create pen
$hPen = _WinAPI_CreatePen($PS_SOLID, 5, 0x7E7E7E)
$oObj = _WinAPI_SelectObject($hMagDC, $hPen)


While 1
    ; Check if cursor has moved
    $aMouse_Pos = MouseGetPos()
    If $aMouse_Pos[0] <> $iLast_Mouse_X Or $aMouse_Pos[1] <> $iLast_Mouse_Y Then
        ; Redraw Mag GUI
        Loupe($aMouse_Pos)
        ; Reset position
        $iLast_Mouse_X = $aMouse_Pos[0]
        $iLast_Mouse_Y = $aMouse_Pos[1]
    EndIf
WEnd

Func On_Exit()

    ; Clear up Mag GUI
    _WinAPI_SelectObject($hMagDC, $oObj)
    _WinAPI_DeleteObject($hPen)
    _WinAPI_ReleaseDC(0, $hDeskDC)
    _WinAPI_ReleaseDC($hMag_GUI, $hMagDC)
    GUIDelete($hMag_GUI)

    Exit

EndFunc   ;==>On_Exit

Func Loupe($aMouse_Pos)

    Local $iX, $iY

    ; Fill Mag GUI with 5x expanded contents of desktop area (10 pixels around mouse)
    DllCall("gdi32.dll", "int", "StretchBlt", _
        "int", $hMagDC,  "int", 0,                   "int", 0,                   "int", 100, "int", 100, _
        "int", $hDeskDC, "int", $aMouse_Pos[0] - 10, "int", $aMouse_Pos[1] - 10, "int",  20, "int",  20, _
        "long", $SRCCOPY)

    ; Keep Mag GUI on screen
    If $aMouse_Pos[0] < (@DesktopWidth - 120) Then
        $iX = $aMouse_Pos[0] + 20
    Else
        $iX = $aMouse_Pos[0] - 120
    EndIf
    If $aMouse_Pos[1] < (@DesktopHeight - 150) Then
        $iY = $aMouse_Pos[1] + 20
    Else
        $iY = $aMouse_Pos[1] - 120
    EndIf
    WinMove($hMag_GUI, "", $iX, $iY, 100, 100)

EndFunc   ;==>Loupe
Please ask if anything is unclear. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

@Melba23 - Not really, if you use GDI+ and not GDI.
 

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>
#include <WindowsConstants.au3>

_GDIPlus_Startup()

Opt("GUIOnEventMode", 1)

Global $iCaptRgn = 5
Global $iGUI_Width = 110, $iGUI_Height = 110 ; Stores the width and the height of the GUI

Global $hGUI = GUICreate("", $iGUI_Width, $iGUI_Height, 500, 550)
WinSetOnTop($hGUI, "", 1)
Global $hGUI_Ctxt = _GDIPlus_GraphicsCreateFromHWND($hGUI) ; Used to draw to the GUI
Global $hBmp_BackBuffer = _GDIPlus_BitmapCreateFromGraphics($iGUI_Width, $iGUI_Height, $hGUI_Ctxt) ; Creates an empty bitmap
Global $hBmp_BackBuffer_Ctxt = _GDIPlus_ImageGetGraphicsContext($hBmp_BackBuffer) ; Gets the graphics context from the empty bitmap
_GDIPlus_GraphicsSetInterpolationMode($hBmp_BackBuffer_Ctxt, 5) ; Re-sizing won't use a filter, so we can see each pixel

Global $hPen = _GDIPlus_PenCreate()
Global $hHBmp_ScreenCapt, $hBmp_ScreenCapt
Global $aiMousePos
Global $hTimer = TimerInit() ; Use a timer instead of sleeping, so we can exit immiediatly if need be
Global $fClose = False ; Stores wether or not the close event was tirggerd
GUISetState()

GUISetOnEvent($GUI_EVENT_CLOSE, "Terminate")

While 1
    If TimerDiff($hTimer) >= 30 Then
        $hTimer = TimerInit() ; Re-initializes the timer
        $aiMousePos = MouseGetPos()

        $hHBmp_ScreenCapt = _ScreenCapture_Capture("", $aiMousePos[0] - $iCaptRgn, $aiMousePos[1] - $iCaptRgn, $aiMousePos[0] + $iCaptRgn, $aiMousePos[1] + $iCaptRgn, False) ; Not using a file name causes it to return a hHBitmap handle and not write to disk
        $hBmp_ScreenCapt = _GDIPlus_BitmapCreateFromHBITMAP($hHBmp_ScreenCapt) ; Convert returned hHBitmap (GDI) to a hBitmap (GDI+)

        _GDIPlus_GraphicsClear($hBmp_BackBuffer_Ctxt, 0xFF000000) ; Clear the backbuffer

        _GDIPlus_GraphicsDrawImageRect($hBmp_BackBuffer_Ctxt, $hBmp_ScreenCapt, 0, 0, $iGUI_Width + 5, $iGUI_Height + 5) ; Draws the captured region onto the backbuffer
        _GDIPlus_BitmapDispose($hBmp_ScreenCapt) ; Dispose of the bitmap to avoid memory leaks
        ; _GDIPlus_GraphicsDrawRect($hBmp_BackBuffer_Ctxt, 50, 50, 10, 10, $hPen)
        _GDIPlus_GraphicsDrawRect($hBmp_BackBuffer_Ctxt, $iGUI_Width / 2 - $iCaptRgn, $iGUI_Height / 2 - $iCaptRgn, $iCaptRgn * 2, $iCaptRgn * 2, $hPen) ; Draw the rect in the middle of the GUI

        _GDIPlus_GraphicsDrawImage($hGUI_Ctxt, $hBmp_BackBuffer, 0, 0) ; Copies the backbuffer to the windows
    EndIf

    If $fClose Then ExitLoop
WEnd

_GDIPlus_PenDispose($hPen)
_GDIPlus_GraphicsDispose($hBmp_BackBuffer_Ctxt)
_GDIPlus_BitmapDispose($hBmp_BackBuffer)
_GDIPlus_GraphicsDispose($hGUI_Ctxt)
_GDIPlus_Shutdown()
Exit

Func Terminate()
    $fClose = True
EndFunc   ;==>Terminate

"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha

@tabhooked

Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation

Link to comment
Share on other sites

Try this here:

#include <GUIConstantsEx.au3>
#include <Screencapture.au3>

Global Const $STM_SETIMAGE = 0x0172
Global Const $hGUI = GUICreate("GDI+ Test", 300, 300)
Global Const $iPic = GUICtrlCreatePic("", 0, 0, 300, 300)
GUISetState()

_GDIPlus_Startup()

Global $x = 0, $iDir = 1

Do
    CaptureAndMagnify($iPic, $x, 0, 30, 30, 10.15)
    $x += $iDir
    If $x > 200 Then $iDir *= -1
    If $x < 0 Then $iDir *= -1
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_GDIPlus_Shutdown()


Func CaptureAndMagnify($iCtrl, $iX, $iY, $iW, $iH, $fZoom)
    Local $hGDI_Bmp = _ScreenCapture_Capture("", $iX, $iY, $iX + $iW, $iY + $iH)
    Local Const $hBmp_Captured = _GDIPlus_BitmapCreateFromHBITMAP($hGDI_Bmp)
    _WinAPI_DeleteObject($hGDI_Bmp)
    Local Const $hBitmap = _GDIPlus_BitmapCreateFromScan0($iW * $fZoom, $iH * $fZoom)
    Local Const $hBmpCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsSetInterpolationMode($hBmpCtxt, $GDIP_INTERPOLATIONMODE_NEARESTNEIGHBOR)
    _GDIPlus_GraphicsDrawImageRect($hBmpCtxt, $hBmp_Captured, 0, 0, $iW * $fZoom, $iH * $fZoom)
    $hGDI_Bmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
    _WinAPI_DeleteObject(GUICtrlSendMsg($iCtrl, $STM_SETIMAGE, $IMAGE_BITMAP, $hGDI_Bmp))
    _WinAPI_DeleteObject($hGDI_Bmp)
    _GDIPlus_GraphicsDispose($hBmpCtxt)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_BitmapDispose($hBmp_Captured)
EndFunc
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

@Melba23, Thanks, I think I understand enough of it to incorporate into my code.

@DatMCEyeBall & UEZ, I must be missing something, I couldn't get either to run. 

 

post-61221-0-47429800-1390527094_thumb.p

 

post-61221-0-60504700-1390527123_thumb.p

Edited by Jags
Link to comment
Share on other sites

Getting closer, Trying to get a 5x5 grid magnified 100x.  This only shows 4-1/2x4-1/2, why? 

#include <GUIConstantsEx.au3>
#include <Screencapture.au3>

Opt("GUIOnEventMode", 1)


Global Const $STM_SETIMAGE = 0x0172
Global Const $hGUI = GUICreate("GDI+ Test", 500, 500)
Global Const $iPic = GUICtrlCreatePic("", 0, 0, 500, 500)
GUISetState()

GUISetOnEvent($GUI_EVENT_CLOSE, "Terminate")

_GDIPlus_Startup()



    Local $hGDI_Bmp = _ScreenCapture_Capture("", 5,5,9,9)
    Local $hBmp_Captured = _GDIPlus_BitmapCreateFromHBITMAP($hGDI_Bmp)
    _WinAPI_DeleteObject($hGDI_Bmp)
    Local $hBitmap = _GDIPlus_BitmapCreateFromScan0(500,500)
    Local $hBmpCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsSetInterpolationMode($hBmpCtxt, $GDIP_INTERPOLATIONMODE_NEARESTNEIGHBOR)
    _GDIPlus_GraphicsDrawImageRect($hBmpCtxt, $hBmp_Captured, 0, 0, 500, 500)
    $hGDI_Bmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
    _WinAPI_DeleteObject(GUICtrlSendMsg($iPic, $STM_SETIMAGE, $IMAGE_BITMAP, $hGDI_Bmp))
    _WinAPI_DeleteObject($hGDI_Bmp)
    _GDIPlus_GraphicsDispose($hBmpCtxt)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_BitmapDispose($hBmp_Captured)


while 1
    sleep(1000)
    WEnd



Func Terminate()
    _GDIPlus_Shutdown()
    Exit
EndFunc   ;==>Terminate
Link to comment
Share on other sites

Just add this line

_GDIPlus_GraphicsSetPixelOffsetMode($hBmpCtxt, $GDIP_PIXELOFFSETMODE_HIGHQUALITY)

after 

Local $hBmpCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap)

and it should work.

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

Thats just it, It does not mark anything

#include <GUIConstantsEx.au3>
#include <Screencapture.au3>

Opt("GUIOnEventMode", 1)


Global Const $STM_SETIMAGE = 0x0172
Global Const $hGUI = GUICreate("GDI+ Test", 500, 500)
Global Const $iPic = GUICtrlCreatePic("", 0, 0, 500, 500)
GUISetState()

GUISetOnEvent($GUI_EVENT_CLOSE, "Terminate")


_GDIPlus_Startup()

    $hPen = _GDIPlus_PenCreate()

    Local $hGDI_Bmp = _ScreenCapture_Capture("", 5,5,9,9)
    Local $hBmp_Captured = _GDIPlus_BitmapCreateFromHBITMAP($hGDI_Bmp)
    _WinAPI_DeleteObject($hGDI_Bmp)
    Local $hBitmap = _GDIPlus_BitmapCreateFromScan0(500,500)
    Local $hBmpCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsSetPixelOffsetMode($hBmpCtxt, $GDIP_PIXELOFFSETMODE_HIGHQUALITY)
    _GDIPlus_GraphicsSetInterpolationMode($hBmpCtxt, $GDIP_INTERPOLATIONMODE_NEARESTNEIGHBOR)

    _GDIPlus_GraphicsDrawImageRect($hBmpCtxt, $hBmp_Captured, 0, 0, 500, 500)
    $hGDI_Bmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)


    ;Center Square
    _GDIPlus_GraphicsDrawRect($hBmpCtxt, 200, 200, 100, 100,$hPen)
    
    
    ;Tried:===========================
    ;_GDIPlus_GraphicsDrawRect($hGUI, 200, 200, 100, 100,$hPen)
    ;_GDIPlus_GraphicsDrawRect($iPic, 200, 200, 100, 100,$hPen)
    ;_GDIPlus_GraphicsDrawRect($hGDI_Bmp, 200, 200, 100, 100,$hPen)
    ;_GDIPlus_GraphicsDrawRect($hBmpCtxt, 5, 5, 10, 10, $hPen)


    ;Cleanup
    _WinAPI_DeleteObject(GUICtrlSendMsg($iPic, $STM_SETIMAGE, $IMAGE_BITMAP, $hGDI_Bmp))
    _WinAPI_DeleteObject($hGDI_Bmp)
    _GDIPlus_GraphicsDispose($hBmpCtxt)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_BitmapDispose($hBmp_Captured)
    _GDIPlus_PenDispose($hPen)



while 1
    sleep(1000)
    WEnd



Func Terminate()
    _GDIPlus_Shutdown()
    Exit
EndFunc   ;==>Terminate 
Link to comment
Share on other sites

This code produces a retancle around the center magnified pixel:

#include <GUIConstantsEx.au3>
#include <Screencapture.au3>

Opt("GUIOnEventMode", 1)


Global Const $STM_SETIMAGE = 0x0172
Global Const $hGUI = GUICreate("GDI+ Test", 500, 500)
Global Const $iPic = GUICtrlCreatePic("", 1, 1, 500, 500)
GUISetState()

GUISetOnEvent($GUI_EVENT_CLOSE, "Terminate")

_GDIPlus_Startup()



Global $hGDI_Bmp = _ScreenCapture_Capture("", 0, 4, 4, 8)
Global $hBmp_Captured = _GDIPlus_BitmapCreateFromHBITMAP($hGDI_Bmp)
_WinAPI_DeleteObject($hGDI_Bmp)

Global $hBitmap = _GDIPlus_BitmapCreateFromScan0(500, 500)
Global $hBmpCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsSetPixelOffsetMode($hBmpCtxt, $GDIP_PIXELOFFSETMODE_HIGHQUALITY)
_GDIPlus_GraphicsSetInterpolationMode($hBmpCtxt, $GDIP_INTERPOLATIONMODE_NEARESTNEIGHBOR)
_GDIPlus_GraphicsDrawImageRect($hBmpCtxt, $hBmp_Captured, 0, 0, 500, 500)
Global $hPen = _GDIPlus_PenCreate(0xFFFFFF00, 4)
_GDIPlus_GraphicsDrawRect($hBmpCtxt, 200, 200, 100, 100, $hPen)
$hGDI_Bmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
_WinAPI_DeleteObject(GUICtrlSendMsg($iPic, $STM_SETIMAGE, $IMAGE_BITMAP, $hGDI_Bmp))
_WinAPI_DeleteObject($hGDI_Bmp)
_GDIPlus_GraphicsDispose($hBmpCtxt)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_BitmapDispose($hBmp_Captured)
_GDIPlus_PenDispose($hPen)

While 1
    Sleep(1000)
WEnd



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

The result looks like this here:

1zz2y49.png

 

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

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