Jump to content
Sign in to follow this  

Capturing image without saving

Recommended Posts

I would like to get a region of the screen but avoiding having to save to a file first.
I believe it is a lack of knowledge on my part but the GUICtrlSetImage function does not allow you to define an image without being a file.
How to do this:

$Pic3 = GUICtrlCreatePic("", 8, 64, 84, 84, -1, _
        $WS_EX_STATICEDGE) ;para ter borda em baixo relevo

_ScreenCapture_Capture(@ScriptDir & "\areatemp.jpg", 10, 10, 200, 200)
GUICtrlSetImage($Pic3, @ScriptDir & "\areatemp.jpg")


Share this post

Link to post
Share on other sites

If you use 


Without a file argument it will return a handle to a bitmap image.

I see that 


Will draw an image object from a handle to a bitmap image.

I've not tried any of that, and perhaps there is a much nicer way to do it, but until someone tells us.... :-)


Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Share this post

Link to post
Share on other sites

yes I know that if I use the capture it returns me a handle of bitmapH but how to put it in a CreatePic.
I would like to better understand the function of creating a CreatePic instead of using the DrawImage ...
It would be lighter but if it does not have geito we will have to use the draw

If someone has a simpres and immediate suggestion it would be wonderful

Share this post

Link to post
Share on other sites

Here is a modified version of the _GDIPlus_GraphicsDrawImageRectRect() function's example in the AutoIt help file.

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


Func Example()
    _GDIPlus_Startup() ;initialize GDI+
    Local Const $iWidth = 600, $iHeight = 600

    Local $hGUI = GUICreate("GDI+ Example (" & @ScriptName & ")", $iWidth, $iHeight) ;create a test GUI

    Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) ;create a graphics object from a window handle
    Local $pIA = _GDIPlus_ImageAttributesCreate() ;create an ImageAttribute object

    Local $hHBmp = _ScreenCapture_Capture("", 0, 0, $iWidth, $iHeight) ;create a GDI bitmap by capturing an area on desktop
    Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBmp) ;convert GDI to GDI+ bitmap
    _WinAPI_DeleteObject($hHBmp) ;release GDI bitmap resource because not needed anymore
    _GDIPlus_GraphicsDrawImage($hGraphics, $hBitmap, 0, 0)
    ; or
    ;_GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hBitmap, 0, 0, $iWidth, $iHeight, 0, 0, $iWidth*2, $iHeight*2); Magnify use in destination  "$iWidth*2, $iHeight*2)"
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ;cleanup GDI+ resources
EndFunc   ;==>Example


Share this post

Link to post
Share on other sites

Here is an easier one.

;#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>
; Modified from https://www.autoitscript.com/forum/topic/131438-how-to-set-a-bitmap-into-a-pic-control-using-a-bitmap-handle/?do=findComment&comment=915168

Local $width = 100, $height = 100
Local $hGUI = GUICreate("Test", 320, 256)
Local $idPic = GUICtrlCreatePic("", 50, 50, $width, $height)
Local $hBmp = _ScreenCapture_Capture("", 100, 100, $width + 100, $height + 100) ; Create_Bitmap(100, 100)
Local $STM_SETIMAGE = 0x0172


While 1
    Switch GUIGetMsg()
        Case -3 ; $GUI_EVENT_CLOSE = -3
        Case $idPic
            MsgBox(0, "Test", "Picture was clicked!")


Edited by Malkey
There are no GDIPlus functions used, so don't need GDIPlus include file, Startup, or, Shutdown functions.

Share this post

Link to post
Share on other sites

I imagined something like that but it has not been known for so much ....
I'm going to do my tests but I believe that's it.
I'll post the final product later ...

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Bilgus
      @TexWiller asked in Help & Support how to use _WinAPI_ExtractIconEx instead of GUICtrlSetImage
      This is his reproducer script for displaying his issue I cleaned it up and made it work for _WinAPI_ExtractIconEx
      ;============================================================================================================ ;Program Options ;============================================================================================================ Opt("MustDeclareVars", 1) ;Variables must be pre-declared. Opt("TrayMenuMode", 3) ;The default tray menu items will not be shown, items are not checked when selected. ;============================================================================================================ ;Libraries Inclusions ;============================================================================================================ #include <ColorConstants.au3> #include <TrayConstants.au3> #include <WindowsConstants.au3> #include <FontConstants.au3> #include <GuiConstantsEx.au3> #include <Array.au3> #include <GuiButton.au3> #include <StaticConstants.au3> ;============================================================================================================ ;Global Vars ;============================================================================================================ Global $hMainGUI Global $nMsg Global $IdTrayMenuMain Global $IdTrayMenuExit Main() Func Main() ;------------------------------------------------------------------------------------------------------------ ;Create Main Window ;------------------------------------------------------------------------------------------------------------ $hMainGUI = GUICreate("Sample", 1024, 700, 50, 50, BitOR($WS_SYSMENU, $WS_CAPTION, $DS_SETFOREGROUND)) GUISetFont(8, $FW_NORMAL, $GUI_FONTNORMAL, "Arial", $hMainGUI, $CLEARTYPE_QUALITY) GUISetBkColor(0xDDDDDD, $hMainGUI) GUISetState(@SW_SHOW, $hMainGUI) WinWaitActive($hMainGUI) ;============================================================================================================ ;Create Traymenu ;============================================================================================================ ;Create a Traymenu. $IdTrayMenuMain = TrayCreateItem("Sample", -1, 0) ;Create a Separator line. TrayCreateItem("", -1, -1) ;Create a Menuitem. $IdTrayMenuExit = TrayCreateItem("Exit", -1, -1) ;Show the Traymenu. TraySetState($TRAY_ICONSTATE_SHOW) ;------------------------------------------------------------------------------------------------------------ ;Sample ;------------------------------------------------------------------------------------------------------------ Local $aIconButtons[0] ;Array to manage Buttons Local $aIconBLabels[0] ;Array to manage Buttons Local $iButtonPosLeft = 10 ;Inserting Position X Local $iButtonPosTop = 10 ;Inserting Position Y Local $iButtonCount Local $iIcnPosOffset = 0 Local $iIconCt = _WinAPI_ExtractIconEx("shell32.dll", -1, 0, 0, 0) ;retrieve count of Icons ConsoleWrite("Icons: " & $iIconCt & @CRLF) Local $iIconIdx = 0 ;The Icon Index to begin extracting at Local $tIconsLg = DllStructCreate("HANDLE[" & $iIconCt & "]") Local $pIconsLg = DllStructGetPtr($tIconsLg) Local $tIconsSm = DllStructCreate("HANDLE[" & $iIconCt & "]") Local $pIconsSm = DllStructGetPtr($tIconsSm) $iIconCt = _WinAPI_ExtractIconEx("shell32.dll", $iIconIdx, $pIconsLg, $pIconsSm, $iIconCt) ;Get Large/ Small Icon Handles into arrays ConsoleWrite("Icons Extracted: " & $iIconCt & @CRLF) ; ;; PS Don't Forget _WinAPI_DestroyIcon() For $iButtonCount = 1 To 200 If BitAND($iButtonCount, 1) Then _ArrayAdd($aIconButtons, GUICtrlCreateButton("", $iButtonPosLeft, $iButtonPosTop, 45, 45, $BS_ICON)) ;Icon Only Else _ArrayAdd($aIconButtons, GUICtrlCreateButton($iButtonCount, $iButtonPosLeft, $iButtonPosTop, 45, 45)) ;Icon and Text EndIf _ArrayAdd($aIconBLabels, GUICtrlCreateLabel(1 * ($iButtonCount + $iIcnPosOffset), $iButtonPosLeft, $iButtonPosTop + 45, 45, 15)) GUICtrlSetStyle($aIconBLabels[$iButtonCount - 1], BitOR($SS_CENTER, $SS_CENTERIMAGE)) $iButtonPosLeft = $iButtonPosLeft + 50 If Mod($iButtonCount, 20) = 0 Then $iButtonPosTop = $iButtonPosTop + 70 $iButtonPosLeft = 10 EndIf ;retrieving Icons by positive position ;***************************************************************************************** ;$MessageTemp = GUICtrlSetImage($aIconButtons[$iButtonCount - 1], "shell32.dll", 1 * ($iButtonCount + $iIcnPosOffset) , 1) ;***************************************************************************************** If $iButtonCount < $iIconCt Then If BitAND($iButtonCount, 1) Then GUICtrlSendMsg($aIconButtons[$iButtonCount - 1], $BM_SETIMAGE, $IMAGE_ICON, DllStructGetData($tIconsLg, 1, $iButtonCount)) Else GUICtrlSendMsg($aIconButtons[$iButtonCount - 1], $BM_SETIMAGE, $IMAGE_ICON, DllStructGetData($tIconsSm, 1, $iButtonCount)) EndIf EndIf Next ;Destroy all the extracted icons ConsoleWrite("Destroying Icons" & @CRLF) For $i = 1 To $iIconCt _WinAPI_DestroyIcon(DllStructGetData($tIconsLg, 1, $i)) _WinAPI_DestroyIcon(DllStructGetData($tIconsSm, 1, $i)) Next ;============================================================================================================ ;GUI Loop ;============================================================================================================ While True ;------------------------------------------------------------------------------------------------------------ ;Get GUI-Messages ;------------------------------------------------------------------------------------------------------------ $nMsg = GUIGetMsg() If $nMsg = $GUI_EVENT_CLOSE Then ExitLoop ;------------------------------------------------------------------------------------------------------------ ;Get Traymenu Messages ;------------------------------------------------------------------------------------------------------------ Switch TrayGetMsg() Case $IdTrayMenuExit ;Exit. ExitLoop EndSwitch ;------------------------------------------------------------------------------------------------------------ WEnd EndFunc ;==>Main  
    • By sharkeye
      #include <ScreenCapture.au3> Local $hBmp ; Capture full screen $hBmp = _ScreenCapture_Capture("") _ScreenCapture_SaveImage("C:\Software\autoitscripts\scripts\images\prp\ocr\"&"ocr_01.PNG",$hBmp) Sleep(1000) _ScreenCapture_Capture("C:\Software\autoitscripts\scripts\images\prp\ocr\"&"ocr_02.PNG") Sleep(1000) ShellExecute("C:\Software\autoitscripts\scripts\images\prp\ocr\"&"ocr_01.PNG") Sleep(1000) ShellExecute("C:\Software\autoitscripts\scripts\images\prp\ocr\"&"ocr_02.PNG") Hi All,
      This is my first post and I have checked the forum.  If this question has been asked and answered I apologise.
      The code above is intended to save the whole screen to a directory; it doesn't work.
      I have downloaded and copied and renamed the files referenced in https://www.autoitscript.com/forum/topic/192254-33143-fixes/ to my includes directory.
      Clearly there is some step that I am missing; do I need to download and register a dll?
      I know that I am using two different methods above but neither works for me.
    • By milos83
      If I set the transparency with WinSetTrans, screenshot can't be made.
      This is Windows 7 related.
      Win 10 works fine
      Run the attached file
      #include <ScreenCapture.au3> ;GUI1 $gui1 = GUICreate("gui1", 200, 200, 200, 200) GUICtrlCreateLabel("GUI 1 LABEL", 50, 50 ) GUISetState() ;GUI 2 with transparency is completely ignored by _ScreenCapture_Capture $gui2 = GUICreate("gui1", 200, 200, 200, 200, -1, -1, $gui1) GUICtrlCreateLabel("GUI 2 LABEL", 100, 100 ) GUISetState() WinSetTrans($gui2, "", 150) Sleep(2000) _ScreenCapture_Capture(@MyDocumentsDir & "\GDIPlus_Image1.jpg", 200, 200, 405, 410) ShellExecute(@MyDocumentsDir & "\GDIPlus_Image1.jpg") As you can see, the semi transparent window will not be visible in the screenshot.
      I noticed that a couple of screen recording tools are acting the same. (But there are those that do work)
      I tried different ways on getting the screenshot and none worked.
      Printscreen does work.
      Is there a way to solve this without using Printscreen?
    • By ashly
      i'm trying to use _ScreenCapture_Capture($sFileName) to cature full screen and save as a jpg file, but it always only capture the left-top about 3/4 area.
      but the print screen keyboard woks fine.
      and if i changed the display resolution from 150% to 100%, the jpg file is OK as well.
      so the issue only occurred when resolution is 150%.
      is there anybody know why or how to change resolution percentage by scripts?
    • By timmy2
      To save future readers a little sweat I'm editing my original post to insert the following:
      After trying various suggested scripting approaches, some of which introduced new problems and some which had the same problem described here, I tried my script and a few others on another computer. No white flashes, tears, glitches. So I brought it back to my main box and disabled the Intel GPU that was feeding a 3rd monitor. Down to two monitors connected to a single NVidia GPU the visual glitches disappeared. Who would'a thought?!
      That said, some of the replies that follow provide valuable insights into the various ways to skin this cat using AutoIt.  Some suggested scripts introduced new problems so they might be instructive about approaches to avoid. Ultimately, JohnOne suggested an elegantly simple script that perfectly accomplishes my goal.
      I want to display a  800x600 picture (i.e., not full-screen) without a border, close-box, title, etc.  I then want to replace that picture with another one -- cleanly. Think of an old-fashioned dual-projector slide show where there's no black-out or other interruption to the image on screen. This way I can start with a background image, then add elements to it seamlessly (once called "a build", i.e., pseudo-animation).  The image format doesn't matter to me.
      I can't use an animated GIF or video because my code is doing things in the background between images, so timing varies.
      I'm having bad luck using an AutoIt form to accomplish this (GUICtrlCreatePic, followed by subsequent GUICtrlSetImage's).  The result is a glitch -- usually a white flash or tear in the picture -- at almost every image change. (see 10/10/13 post entitled "random visual glitch when using GUICtrlSetImage"). So I guess that's out.
      SplashImageOn  blanks out the first image before displaying the next (like a single-projector slide show), so it's not seamless -- plus there's still a tiny border visible.
      $var = Default SplashImageOn("","image1.bmp",$var,$var,$var,$var,1) sleep(2000) SplashImageOn("","image2.bmp",$var,$var,$var,$var,1) sleep(2000) SplashImageOn("","image3.bmp",$var,$var,$var,$var,1) sleep(2000) Any suggestions?
  • Create New...