Jump to content

Image streaming


Andreik
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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

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

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

×
×
  • Create New...