Jump to content
Sign in to follow this  
Andreik

Image streaming

Recommended Posts

Andreik

I am working on a desktop remote application so I made a simple server and client to send binary screen captures from client to server. All it's good if I test both scripts on my computer but when the client is in other network many screen shots are corrupted and some of them looks good. Have any idea why?

 

Client:

#include <ScreenCapture.au3>
#include <Memory.au3>
#include <WinAPI.au3>
#include <GDIPlus.au3>


TCPStartup()
$Client = TCPConnect(@IPAddress1,12100)

_GDIPlus_Startup()

While True
    Local $hHBitmap = _ScreenCapture_Capture('')
    Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap)
    $bData = _GDIPlus_StreamImage2BinaryString($hBitmap)
    _GDIPlus_BitmapDispose($hBitmap)
    _WinAPI_DeleteObject($hHBitmap)
    TCPSend($Client,'~stream:' & BinaryLen($bData))
    While BinaryLen($bData)
        $a = TCPSend($Client, $bData)
        $bData = BinaryMid($bData, $a+1, BinaryLen($bData)-$a)
    WEnd
    Sleep(10)
WEnd

TCPCloseSocket($Client)
TCPShutdown()
_GDIPlus_Shutdown()

Func _GDIPlus_StreamImage2BinaryString($hBitmap, $sFormat = "JPG", $iQuality = 100)  ;UEZ
    Local $sImgCLSID, $tGUID, $tParams, $tData
    Switch $sFormat
        Case "JPG"
            $sImgCLSID = _GDIPlus_EncodersGetCLSID($sFormat)
            $tGUID = _WinAPI_GUIDFromString($sImgCLSID)
            $tData = DllStructCreate("int Quality")
            DllStructSetData($tData, "Quality", $iQuality) ;quality 0-100
            Local $pData = DllStructGetPtr($tData)
            $tParams = _GDIPlus_ParamInit(1)
            _GDIPlus_ParamAdd($tParams, $GDIP_EPGQUALITY, 1, $GDIP_EPTLONG, $pData)
        Case "PNG", "BMP", "GIF", "TIF"
            $sImgCLSID = _GDIPlus_EncodersGetCLSID($sFormat)
            $tGUID = _WinAPI_GUIDFromString($sImgCLSID)
        Case Else
            Return SetError(1, 0, 0)
    EndSwitch
    Local $hStream = _WinAPI_CreateStreamOnHGlobal() ;http://msdn.microsoft.com/en-us/library/ms864401.aspx
    If @error Then Return SetError(2, 0, 0)
    _GDIPlus_ImageSaveToStream($hBitmap, $hStream, DllStructGetPtr($tGUID), DllStructGetPtr($tParams))
    If @error Then Return SetError(3, 0, 0)
    Local $hMemory = _WinAPI_GetHGlobalFromStream($hStream) ;http://msdn.microsoft.com/en-us/library/aa911736.aspx
    If @error Then Return SetError(4, 0, 0)
    Local $iMemSize = _MemGlobalSize($hMemory)
    If Not $iMemSize Then Return SetError(5, 0, 0)
    Local $pMem = _MemGlobalLock($hMemory)
    $tData = DllStructCreate("byte[" & $iMemSize & "]", $pMem)
    Local $bData = DllStructGetData($tData, 1)
    _WinAPI_ReleaseStream($hStream) ;http://msdn.microsoft.com/en-us/library/windows/desktop/ms221473(v=vs.85).aspx
    _MemGlobalFree($hMemory)
    Return $bData
EndFunc   ;==>_GDIPlus_StreamImage2BinaryString

 

Server

Global $Buffer, $BufferSize
Global $Count = 0


TCPStartup()

$Server = TCPListen(@IPAddress1,12100)
If @error Then MsgBox(0,'',@error)

Do
    $Socket = TCPAccept($Server)
    Sleep(10)
Until $Socket <> -1


While True
    $Recv = TCPRecv($Socket,10240)
    If $Recv = -1 Then
        ExitLoop
    ElseIf $Recv Then
        If StringLeft($Recv,7) = '~stream' Then
            $BufferSize = StringSplit($Recv,':')[2]
            $Buffer = '0x'
            Do
                $Recv = TCPRecv($Socket,10240)
                If BinaryLen($Recv) <> 0 Then
                    $BufferSize -= BinaryLen($Recv)
                    $Buffer &= StringTrimLeft($Recv,2)
                EndIf
            Until $BufferSize = 0
            $hFile = FileOpen(@ScriptDir & '\Screen' & $Count & '.jpeg',18)
            FileWrite($hFile,$Buffer)
            FileClose($hFile)
            $Buffer = Null
            $Count += 1
        EndIf
        If $Count = 100 Then ExitLoop
    EndIf
    Sleep(10)
WEnd

TCPCloseSocket($Socket)
TCPCloseSocket($Server)
TCPShutdown()

Exit

 


When the words fail... music speaks

Share this post


Link to post
Share on other sites
TheSaint

I can only imagine it is a timing issue, but I am no expert, especially with Network stuff.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

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  

  • Similar Content

    • Atoxis
      By Atoxis
      Howdy, I've gone through a lot of au3 forums, and I once had a working Imagesearch script that I got from here.  However, and i'm just totally not sure how but my imagesearch scripts aren't working anymore.
      I'm not new to au3 but i'm not the most experienced with it's syntax/commands.

      Anyways, I've looked over the big threads involving imagesearch.

      Does anyone have a working Imagesearch x64 for win10 that is currently working as of the date with the post.

      Dll's and what not is fine, just when I tell the script to run, I want to be able to find the image on the screen!
      Can't find a working copy so if anyone has one please send it my way lol.

      I've taken all the imagesearch downloads and what not and have played with them but I can't get any of them working on my end, despite others saying they're working.
      Thanks.
    • Spartan117
      By Spartan117
      Hi everyone,
      I wonder if there is a cloud service that can offer a physical server so that AutoIT can turn as it is in a physical machine.
      Thank you
    • Xandy
      By Xandy
      MapIt is a tile world editor.  MapIt was built around the concept of reversing Dragon Warrior map images.  MapIt can take image input and produce a tile and world array.
      MapIt features Unity style dragable labels that adjust property values.

      MapParser is a C++ project that scans images for unique tiles.  MapParser is very fast.  Due to hard drive failure, many bugs were restored b/c I had to rewind many years.  Frustrated with the design, I wrote a new version from the ground up.  This New Version:  AutoIt Front-end, command line controls, and shared with the world; so that I can't lose it again.
      You can toggle the C++ MapParser off to see the difference in speeds between the MapParser CPP verse AutoIt function.  Function is named Scan_Tiles() in AutoIt.  You can also chose to download without MapParser.exe.
      At the moment Scanning a image resets the arrays, but you can add tiles after scanning.
      Images can be added as tiles without scan image at all.  Then configure settings to give your world parameters and manually fill the world data with tile indexes.
      Using the settings you can change tile size after a scan.  Example: you wanted to replace a map with different sized tiles.
      Changing and replacing tile / world data is easy.  B/c tile world editor.

      Hotkeys, I use CTRL+R in image above to signal replace tile action and I use "G" to Get the tile under mouse.
      Hotkeys are not saved to disk and thus are set to default between sessions.
      I might draw the world to pre-rendered surfaces and use them as multi-layer someday.  I do that in my AutoIt, DragonWarrior Remake but I could spend forever unsure what features are important for this.  The DW_Remake has a method of replacing a tile with a tile on two layers.  So you could replace a tree on the first layer with a grass, and a tree in the second layer.  This is all getting very confusing.
      I attempted to write the good code.  If something could be better, please advise.
      Fifth release.  Enjoy.
      For download, videos, and example of created world file data; please visit the MapIt webpage: http://songersoft.com/programming/mapit/mapit_about.phtml
      Special thanks: @AdmiralAlkex, @Melba23, @MrCreatoR
      Main AutoIt source file: Will not run without other Includes and SDL DLLs.
      Last Update: 5/26/2018 3:45 PM EST
      REMOVED CODE BLOCK:  I was informed the this page loaded very slowly, one solution so far has been to remove the 2k lines in the code block.
      When I recieve more feedback from the User I may reduce image size or remove images.
      Next Version Added: $eSETTING_TILE_LAST_PATH I have the weekend, I want to write world layers with aBoard surfaces.  
    • lenclstr746
      By lenclstr746
      HELLO GUYS
      I'm a work on a background see and click bot project 
      I can complete it if your help me
      (using imagesearch , gdi+ and  fastfind)
    • dadalt95
      By dadalt95
      I would like to know if it's possible to pass an image recognition (captcha) system.
       
      What are the ways to achieve this?
      Just the references or links is enough for me by now.
       
      Thanks by now!
       
      Thanks!
×