JScript Posted February 18, 2011 Share Posted February 18, 2011 (edited) Hello everyone, Could save a captured image directly into a variable? #include <GuiConstantsEx.au3> #include <ScreenCapture.au3> #include <WinAPI.au3> Opt('MustDeclareVars', 1) _ScreenCapture() Func _ScreenCapture() Local $hBitmap, $hImage _GDIPlus_Startup() ; Capture full screen $hBitmap = _ScreenCapture_Capture("") $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap) ; Save resultant image _GDIPlus_ImageSaveToFile($hImage, @ScriptDir & "\Capture.jpg") ; Clean up resources _GDIPlus_ImageDispose ($hImage) _WinAPI_DeleteObject ($hBitmap) _GDIPlus_ShutDown () EndFunc ;==>_Main It would be possible to save the captured image into a $Variable instead of saving to a File? ; Save resultant image _GDIPlus_ImageSaveToFile($hImage, @ScriptDir & "\Capture.jpg") For something like: $bVariable = _GDIPlus_ImageSavetoVar($hImage) Thanks in advance... Edited February 18, 2011 by jscript http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!) Somewhere Out ThereJames Ingram Download Dropbox - Simplify your life!Your virtual HD wherever you go, anywhere! Link to comment Share on other sites More sharing options...
hannes08 Posted February 18, 2011 Share Posted February 18, 2011 (edited) Hi jscript,adapted from autoit helpfile: _GDIPlus_BitmapCreateFromHBITMAP :expandcollapse popup#include <GuiConstantsEx.au3> #include <GDIPlus.au3> #include <ScreenCapture.au3> Opt('MustDeclareVars', 1) _Main() Func _Main() Local $hGUI, $hBMP, $hBitmap, $hGraphic ; Capture upper left corner of screen $hBMP = _ScreenCapture_Capture ("", 0, 0, 400, 300) ; Create GUI $hGUI = GUICreate("GDI+", 400, 300) GUISetState() ; Initialize GDI+ library _GDIPlus_Startup () ; Draw bitmap to GUI $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP ($hBMP) $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hGUI) _GDIPlus_GraphicsDrawImage ($hGraphic, $hBitmap, 0, 0) ; Clean up resources _GDIPlus_GraphicsDispose ($hGraphic) _GDIPlus_BitmapDispose ($hBitmap) _WinAPI_DeleteObject ($hBMP) ; Shut down GDI+ library _GDIPlus_ShutDown () ; Loop until user exits Do Until GUIGetMsg() = $GUI_EVENT_CLOSE EndFunc ;==>_MainRegards,HannesEDIT: changed autoitcode... Edited February 18, 2011 by Hannes123 Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler] Link to comment Share on other sites More sharing options...
JScript Posted February 18, 2011 Author Share Posted February 18, 2011 Not exactly what I wanted... Here's what I'm trying to do: Server expandcollapse popup#AutoIt3Wrapper_Compression=0 ;Compression parameter 0-4 0=Low 2=normal 4=High. Default=2 #AutoIt3Wrapper_UseUpx=n ;(Y/N) Compress output program. Default=Y #include <GDIPlus.au3> #include <GUIConstantsEx.au3> #include <Memory.au3> #include <WinAPI.au3> Global $hForm1 Global $picbox ; Send Data functions Global $TCP_MainDataSocket Global $TCP_DataSocket = -1 Global $iMaxTCPDataLen = 2 * 1048576;131072 Global $iScrPort = 5909 Global $bBitmap = "" TCPStartup() $hForm1 = GUICreate("", 640, 480, -1, -1) $iCtrlID = GUICtrlCreatePic("", 0, 0, 640, 480) GUISetState() WinSetOnTop($hForm1, "", 1) ; Create a Listening "SOCKET" for message $TCP_MainDataSocket = TCPListen(@IPAddress1, $iScrPort) While 1 $msg = GUIGetMsg() Select Case $msg = $GUI_EVENT_CLOSE Exit EndSelect _Read_DataSocket() WEnd ; #FUNCTION# ==================================================================================================================== ; Name..........: _Read_MsgSocket ; Description ..: Supporte for Send/Receive messages ; Author .......: João Carlos ; =============================================================================================================================== Func _Read_DataSocket() Local $vRecvData ; If no connection for message, look for one... Select Case $TCP_DataSocket = -1 $TCP_DataSocket = TCPAccept($TCP_MainDataSocket) If $TCP_DataSocket < 0 Then $TCP_DataSocket = -1 Case Else ; If connected try to read some data $vRecvData = TCPRecv($TCP_DataSocket, $iMaxTCPDataLen) ; IF ERROR OCCURRED, CLOSE SOCKET AND RESET $TCP_DataSocket TO -1 If @error Or $vRecvData = "" Then Return _ResetSocket($TCP_DataSocket) Switch $vRecvData ;Case ".Start" ; $bBitmap = "" Case ".Done" $bBitmap = "0x" & $bBitmap _SetImageBinaryToCtrl($iCtrlID, $bBitmap) $bBitmap = "" Case Else $bBitmap &= $vRecvData EndSwitch Return 1 EndSelect Return 0 EndFunc ;==>_Read_DataSocket ; #FUNCTION# ==================================================================================================================== ; Name..........: _ResetSocket ; Description ..: Close socket and reset socket variable. ; Parameters ...: SocketID - The variable that contains the socket ID. ; Return values : Success - Returns 1 ; Failure - Returns 0 and set @error according to Windows API WSAGetLastError return. ; Author .......: João Carlos ; Remarks ......: The variable is passed ByRef. ; =============================================================================================================================== Func _ResetSocket(ByRef $iSocketID) Local $vCloseSocket = TCPCloseSocket($iSocketID) $iSocketID = -1 Return $vCloseSocket EndFunc ;==>_ResetSocket ;Authors: Prog@ndy, based on code by Zedna Func _SetImageBinaryToCtrl($CtrlId, ByRef $Binary) Local $picdata = Binary($Binary) ; Fetch the Data Local $piclength = BinaryLen($picdata) ; Get Length Local $picstruct = DllStructCreate("byte[" & $piclength & "]") DllStructSetData($picstruct, 1, $picdata) Local $picmemory = DllStructGetPtr($picstruct) _SetMemoryImageToCtrl($CtrlId, $picmemory, $piclength) DllStructSetData($picstruct, 1, 0) $picstruct = "" EndFunc ;==>_SetImageBinaryToCtrl ; Authors: Zedna, based on code by Prog@ndy Func _SetMemoryImageToCtrl($CtrlId, $Pointer, $nSize) Local $hData, $pData, $pStream, $pBitmap, $hBitmap ; use GDI+ for converting to bitmap first $hData = _MemGlobalAlloc($nSize, 2) $pData = _MemGlobalLock($hData) _MemMoveMemory($Pointer, $pData, $nSize) _MemGlobalUnlock($hData) $pStream = _WinAPI_CreateStreamOnHGlobal($hData) ;Creates a stream object that uses an HGLOBAL memory handle to store the stream contents _GDIPlus_Startup() $pBitmap = _GDIPlus_BitmapCreateFromStream($pStream) ;Creates a Bitmap object based on an IStream COM interface $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($pBitmap) _SetBitmapToCtrl($CtrlId, $hBitmap) If @error Then SetError(3, 0, 0) _GDIPlus_BitmapDispose($pBitmap) _GDIPlus_Shutdown() _WinAPI_DeleteObject($pStream) _MemGlobalFree($hData) EndFunc ;==>_SetMemoryImageToCtrl ; internal helper function ; Out of resources.au3 :) Func _SetBitmapToCtrl($CtrlId, $hBitmap) Local Const $STM_SETIMAGE = 0x0172 Local Const $IMAGE_BITMAP = 0 Local Const $SS_BITMAP = 0xE Local Const $GWL_STYLE = -16 Local $hWnd = GUICtrlGetHandle($CtrlId) If $hWnd = 0 Then Return SetError(1, 0, 0) ; set SS_BITMAP style to control Local $oldStyle = DllCall("user32.dll", "long", "GetWindowLong", "hwnd", $hWnd, "int", $GWL_STYLE) If @error Then Return SetError(2, 0, 0) DllCall("user32.dll", "long", "SetWindowLong", "hwnd", $hWnd, "int", $GWL_STYLE, "long", BitOR($oldStyle[0], $SS_BITMAP)) If @error Then Return SetError(3, 0, 0) Local $oldBmp = DllCall("user32.dll", "hwnd", "SendMessage", "hwnd", $hWnd, "int", $STM_SETIMAGE, "int", $IMAGE_BITMAP, "int", $hBitmap) If @error Then Return SetError(4, 0, 0) If $oldBmp[0] <> 0 Then _WinAPI_DeleteObject($oldBmp[0]) Return 1 EndFunc ;==>_SetBitmapToCtrl Func Load_BMP_From_Mem($mem_image) ;coded by UEZ - thanks to progandy form the MemGlobalAlloc lines Local $memBitmap, $len, $tMem, $hImage $memBitmap = Binary($mem_image) ;load image saved in variable (memory) and convert it to binary $len = BinaryLen($memBitmap) ;get length of image $hData = _MemGlobalAlloc($len, $GMEM_MOVEABLE) ;allocates movable memory ($GMEM_MOVEABLE = 0x0002) $pData = _MemGlobalLock($hData) ;translate the handle into a pointer $tMem = DllStructCreate("byte[" & $len & "]", $pData) ;create struct DllStructSetData($tMem, 1, $memBitmap) ;fill struct with image data _MemGlobalUnlock($hData) ;decrements the lock count associated with a memory object that was allocated with GMEM_MOVEABLE $hStream = _WinAPI_CreateStreamOnHGlobal($pData) ;Creates a stream object that uses an HGLOBAL memory handle to store the stream contents $hBitmapFromStream = _GDIPlus_BitmapCreateFromStream($hStream) ;Creates a Bitmap object based on an IStream COM interface $tMem = "" Return $hBitmapFromStream EndFunc ;==>Load_BMP_From_Mem ; #FUNCTION# ==================================================================================================================== ; Name...........: _WinAPI_CreateStreamOnHGlobal ; Description ...: Creates a stream object that uses an HGLOBAL memory handle to store the stream contents ; Syntax.........: _WinAPI_CreateStreamOnHGlobal([$hGlobal = 0[, $fDeleteOnRelease = True]]) ; Parameters ....: $hGlobal - A memory handle. If 0, a new handle is to be allocated instead ; $fDeleteOnRelease - If True, the release of the IStream interface releases the memory handle as well. ; Otherwise, it's the responsibility of the user to release this memory handle. ; Return values .: Success - A pointer to new stream object ; Failure - 0 ; Remarks .......: None ; Related .......: _MemGlobalFree ; Link ..........; @@MsdnLink@@ CreateStreamOnHGlobal ; Example .......; No ; =============================================================================================================================== Func _WinAPI_CreateStreamOnHGlobal($hGlobal = 0, $fDeleteOnRelease = True) Local $aResult = DllCall("ole32.dll", "int", "CreateStreamOnHGlobal", "hwnd", $hGlobal, "int", $fDeleteOnRelease, "ptr*", 0) If @error Then Return SetError(@error, @extended, 0) Return $aResult[3] EndFunc ;==>_WinAPI_CreateStreamOnHGlobal ; #FUNCTION# ==================================================================================================================== ; Name...........: _GDIPlus_BitmapCreateFromStream ; Description ...: Creates a Bitmap object based on an IStream COM interface ; Syntax.........: _GDIPlus_BitmapCreateFromStream($pStream) ; Parameters ....: $pStream - Pointer to an IStream COM interface ; Return values .: Success - Returns a handle to a new Bitmap object ; Failure - 0 and either: ; |@error and @extended are set if DllCall failed ; |$GDIP_STATUS contains a non zero value specifying the error code ; Remarks .......: After you are done with the object, call _GDIPlus_ImageDispose to release the object resources ; Related .......: _GDIPlus_ImageDispose, _WinAPI_CreateStreamOnHGlobal ; Link ..........; @@MsdnLink@@ GdipCreateBitmapFromStream ; Example .......; Yes ; =============================================================================================================================== Func _GDIPlus_BitmapCreateFromStream($pStream) Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromStream", "ptr", $pStream, "int*", 0) If @error Then Return SetError(@error, @extended, 0) $GDIP_STATUS = $aResult[0] Return $aResult[2] EndFunc ;==>_GDIPlus_BitmapCreateFromStream Cliente expandcollapse popup#AutoIt3Wrapper_Compression=0 ;Compression parameter 0-4 0=Low 2=normal 4=High. Default=2 #AutoIt3Wrapper_UseUpx=n ;(Y/N) Compress output program. Default=Y #include <GDIPlus.au3> #include <GUIConstantsEx.au3> #include <Memory.au3> #include <WinAPI.au3> #include <ScreenCapture.au3> Global $iScrPort = 5909 Local $sCmpName = InputBox("Cliente", "Enter computer name:") If @error Then Exit TCPStartup() While 1 Sleep(20) $sFile = _ScreenCapture() _SendFile($sCmpName, $sFile) WEnd ; #FUNCTION# ==================================================================================================================== ; Name...........: _SendFile() ; Description ...: Sending a File to an Server ; Syntax.........: _SendFile( CmpName, Port, "File" [, Return ] ) ; Parameters ....: CmpName - IP or Name of the target computer. ; Port - The port to connect. The defaul is -1 (5902) ; File - The file to be sent. ; SendID - The identification of send message. Default is "#SendFile". ; Return values .: Success - Emulate MultiThreading ;-) ; Failure - Returns 0. ; Author ........: jscript - João Carlos FROM BRAZIL. ; Example .......; _SendFile( $sCmpName, $iPort, "File" ) ; =============================================================================================================================== Func _SendFile($sCmpName, $sFile = "", $iPort = -1) If $iPort = -1 Then $iPort = $iScrPort $IPAddress = TCPNameToIP($sCmpName) If @error Then Return 0 ; ; Connect to a Listening "SOCKET" and send .Start command... ;$Socket = TCPConnect($IPAddress, $iPort) ;If $Socket = -1 Then Return 0 ;TCPSend($Socket, ".Start") ;TCPCloseSocket($Socket) ; ; Connect to a Listening "SOCKET" and send user picture $Socket = TCPConnect($IPAddress, $iPort) If $Socket = -1 Then Return 0 $MyPic = Binary($sFile) While BinaryLen($MyPic) $SendBinaryFile = TCPSend($Socket, Hex($MyPic)) If @error Then ExitLoop $MyPic = BinaryMid($MyPic, $SendBinaryFile + 1, BinaryLen($MyPic) - $SendBinaryFile) WEnd TCPCloseSocket($Socket) ; ; Connect to a Listening "SOCKET" and send .Done command... $Socket = TCPConnect($IPAddress, $iPort) If $Socket = -1 Then Return 0 TCPSend($Socket, ".Done") TCPCloseSocket($Socket) Return 1 EndFunc ;==>_SendFile Func _ScreenCapture() Local $hBitmap, $hClone, $hImage, $hImage2, $iX, $iY, $CLSID Local $tParams, $tData, $pParams, $iQuality = 25 Local $iWidth = 640, $iHeight = 480 Local $sCapFile = @ScriptDir & "\Capture.jpg" ; Initialize GDI+ library _GDIPlus_Startup() ; Capture 32 bit bitmap $hBitmap = _ScreenCapture_Capture("", 0, 0, -1, -1, False) $hScreen = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap) _WinAPI_DeleteObject($hBitmap) ; Create 16 bit bitmap clone $iActW = _GDIPlus_ImageGetWidth($hScreen) $iActH = _GDIPlus_ImageGetHeight($hScreen) ; $GDIP_PXF04INDEXED ; $GDIP_PXF08INDEXED $hClone = _GDIPlus_BitmapCloneArea($hScreen, 0, 0, $iActW, $iActH, $GDIP_PXF16RGB555) _GDIPlus_ImageDispose($hScreen) ; find current aspect ratio (using width / height) $vAspectR = $iActW / $iActH ; populate values if defaults used. Use "<0" to capture incorrect size entries. Select Case $iHeight < 0 And $iWidth < 0 $iHeight = $iActH $iWidth = $iActW Case $iHeight < 0 $iHeight = $iWidth / $vAspectR Case $iWidth < 0 $iWidth = $iHeight * $vAspectR EndSelect If $iHeight * $vAspectR <= $iWidth Then $iWidth = $iHeight * $vAspectR Else $iHeight = $iWidth / $vAspectR EndIf ; resize picture $hWnd = _WinAPI_GetDesktopWindow() $hDC = _WinAPI_GetDC($hWnd) $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, $iWidth, $iHeight) _WinAPI_ReleaseDC($hWnd, $hDC) $hResPic = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap) $hGraphic = _GDIPlus_ImageGetGraphicsContext($hResPic) _GDIPlus_GraphicsDrawImageRect($hGraphic, $hClone, 0, 0, $iWidth, $iHeight) _GDIPlus_ImageDispose($hClone) _WinAPI_DeleteObject($hBitmap) _GDIPlus_GraphicsDispose($hGraphic) ;Get the encoder of to save the resized image in the format you want. $CLSID = _GDIPlus_EncodersGetCLSID("JPG") ; Set the image quality $tParams = _GDIPlus_ParamInit(1) $tData = DllStructCreate("int Quality") DllStructSetData($tData, "Quality", $iQuality) _GDIPlus_ParamAdd($tParams, $GDIP_EPGQUALITY, 1, $GDIP_EPTLONG, DllStructGetPtr($tData)) $pParams = DllStructGetPtr($tParams) ; Save bitmap to file _GDIPlus_ImageSaveToFileEx($hResPic, $sCapFile, $CLSID, $pParams) _GDIPlus_ImageDispose($hResPic) ; Shut down GDI+ library _GDIPlus_Shutdown() $hFileOpen = FileOpen($sCapFile, 16) $sFileRead = FileRead($hFileOpen) FileClose($hFileOpen) FileDelete($sCapFile) Return $sFileRead EndFunc ;==>_ScreenCapture Just wish I could return the image directly instead of saving to a file first. ; Save bitmap to file _GDIPlus_ImageSaveToFileEx($hResPic, $sCapFile, $CLSID, $pParams) $hFileOpen = FileOpen($sCapFile, 16) $sFileRead = FileRead($hFileOpen) FileClose($hFileOpen) FileDelete($sCapFile) Return $sFileRead That way would be faster to send the captured images to another computer... http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!) Somewhere Out ThereJames Ingram Download Dropbox - Simplify your life!Your virtual HD wherever you go, anywhere! Link to comment Share on other sites More sharing options...
UEZ Posted February 18, 2011 Share Posted February 18, 2011 You can capture the screenshot, save it to the clipboard and send the clipboard data to the remote pc. On remote pc you can display the image (it is a HBITMAP). It is just an idea. 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 More sharing options...
rover Posted February 18, 2011 Share Posted February 18, 2011 Save the image to a stream. I knew there was a GDI+ save image to stream function in Authenticity's UDF, and somewhere on the forum, an example. how to use tcp send this Authenticity's GDIP UDF here: expandcollapse popup#include <GDIPlus.au3> #include <Memory.au3> #include <ScreenCapture.au3> Opt("MustDeclareVars", 1) _GDIPlus_Startup() Local $hBmp = _ScreenCapture_Capture() Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hBmp) _WinAPI_DeleteObject($hBmp) Local $sEncoderCLSID = _GDIPlus_EncodersGetCLSID("jpg") Local $tEncoderCLSID = _WinAPI_GUIDFromString($sEncoderCLSID) Local $pEncoderCLSID = DllStructGetPtr($tEncoderCLSID) Local $pStream = _WinAPI_CreateStreamOnHGlobal(0) _GDIPlus_ImageSaveToStream($hBitmap, $pStream, $pEncoderCLSID) _GDIPlus_BitmapDispose($hBitmap) Local $hMem = _WinAPI_GetHGlobalFromStream($pStream) Local $iSize = _MemGlobalSize($hMem) Local $pMem = _MemGlobalLock($hMem) Local $tData = DllStructCreate("byte[" & $iSize & "]", $pMem) Local $xData = DllStructGetData($tData, 1) ;JPG file in binary format ;Local $hFileOpen = FileOpen(@ScriptDir &"\test0.jpg", 18) ;FileWrite($hFileOpen, $xData) ;FileClose($hFileOpen) _MemGlobalFree($hMem) Local $S_ip = InputBox("please input ipaddress", "please input ipaddress", @IPAddress1) Local $S_Port = 65432 TCPStartup() Local $socket = TCPConnect($S_ip, $S_Port) If $socket <> -1 Then For $i = 0 To Int($iSize / 65536) TCPSend($socket, BinaryMid($xData, $i * 65536 + 1, 65536)) Next TCPCloseSocket($socket) EndIf TCPShutdown() _GDIPlus_Shutdown() Func _GDIPlus_ImageSaveToStream($hImage, $pStream, $pEncoder, $pParams = 0) Local $aResult = DllCall($ghGDIPDll, "uint", "GdipSaveImageToStream", "ptr", $hImage, "ptr", $pStream, "ptr", $pEncoder, "ptr", $pParams) If @error Then Return SetError(1, 0, 0) Return SetError($aResult[0] <> 0, 0, $aResult[0] = 0) EndFunc Func _WinAPI_CreateStreamOnHGlobal($hGlobal, $fDeleteOnRelease = 1) Local $aResult = DllCall("ole32.dll", "uint", "CreateStreamOnHGlobal", "ptr", $hGlobal, "bool", $fDeleteOnRelease, "ptr*", 0) If @error Or $aResult[0] Then Return SetError(1, 0, 0) Return $aResult[3] EndFunc Func _WinAPI_GetHGlobalFromStream($pStream) Local $aResult = DllCall("ole32.dll", "uint", "GetHGlobalFromStream", "ptr", $pStream, "ptr*", 0) If @error Or $aResult[0] Then Return SetError(1, 0, 0) Return $aResult[2] EndFunc I see fascists... Link to comment Share on other sites More sharing options...
UEZ Posted February 18, 2011 Share Posted February 18, 2011 (edited) If you are interested in sending whole desktop as image to another remote pc like Teamviewer does have a look to DeskStream on German AutoIt site!Br,UEZ Edited February 18, 2011 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 More sharing options...
JScript Posted February 18, 2011 Author Share Posted February 18, 2011 Thank you all for the replies, it was just what I needed... http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!) Somewhere Out ThereJames Ingram Download Dropbox - Simplify your life!Your virtual HD wherever you go, anywhere! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now