只有部分图像通过TCP发送 [英] Only part of image sent through TCP

查看:96
本文介绍了只有部分图像通过TCP发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每500毫秒将屏幕截图发送到同一台计算机,但它只收到大约75%的图像,并且收到的金额因屏幕截图而异。当发送到同一网络上的另一台计算机(通过以太网)时,只有一小部分屏幕可以通过。



代码发送截图:



I'm sending screenshots every 500ms to the same computer but it only receives about 75% of the image, and the amount received varies with every screenshot. When sending to another computer on the same network (via ethernet), only a tiny fraction of the screen gets through.

Code to send screenshots:

sendDataToServer("$scr:" & Convert.ToBase64String(New ImageConverter().ConvertTo(screenshot, GetType(Byte()))))

Private Function screenshot() As Bitmap
        Static cap As Bitmap = Nothing
        If cap IsNot Nothing Then GC.RemoveMemoryPressure(cap.Width * cap.Height * 4)
        cap = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppRgb)
        GC.AddMemoryPressure(cap.Width * cap.Height * 4)
        Using graph As Graphics = Graphics.FromImage(cap)
            graph.CopyFromScreen(0, 0, 0, 0, cap.Size, CopyPixelOperation.SourceCopy)
        End Using
        Return cap
    End Function





接收截图:





Receiving screenshots:

Private Sub readCallback(result As IAsyncResult)

        If closeMe Then Return

        Try
            networkStream = clientSocket.GetStream()
            Dim read As Integer = networkStream.EndRead(result)
            If read = 0 Then
                networkStream.Close()
                clientSocket.Close()
                Return
            End If

            Dim buffer As Byte() = TryCast(result.AsyncState, Byte())
            Dim data As String = Encoding.UTF8.GetString(buffer, 0, read)

            Dim cmd As String = Nothing
            If data.Contains(":") Then
                cmd = data.Split(":")(0)
                data = data.Remove(0, cmd.Length + 1)
            Else
                cmd = data
                data = Nothing
            End If

            Select Case (cmd) '! = request, $ = response
                Case "$scr"
                    main.crossThread(Utils.EnumCrossThreadFunction.updateWatchWindow, Utils.getArgs(Utils.bytesToImage(Convert.FromBase64String(data))))                
                Case "$ping"
                    main.getPingData()(username) = 1                              
            End Select

            awaitRequest()

        Catch ex As Exception

        End Try

    End Sub





有谁知道这个的原因?一旦屏幕截图开始发送,客户端也会在几秒钟后超时,告诉我连接已经饱和。



我尝试了什么:



我尝试过使用UDP和各种不同的TCP代码集。



Does anyone know the reason for this? The client also times out after a few seconds once screenshots start getting sent, telling me the connection gets saturated.

What I have tried:

I've tried using UDP and various different sets of TCP code.

推荐答案

scr: & Convert.ToBase64String(New ImageConverter()。ConvertTo(screenshot,GetType(Byte()))))

私函数screenshot()As Bitmap
静态上限As Bitmap = Nothing
如果上限IsNot Nothing则GC.RemoveMemoryPressure(cap.Width * cap.Height * 4)
cap =新位图(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,PixelFormat.Format32bppRgb )
GC.AddMemoryPressure(cap.Width * cap.Height * 4)
使用graph As Graphics = Graphics.FromImage(cap)
graph.CopyFromScreen(0,0,0,0, cap.Size,CopyPixelOperation.SourceCopy)
结束使用
返回上限
结束F unction
scr:" & Convert.ToBase64String(New ImageConverter().ConvertTo(screenshot, GetType(Byte())))) Private Function screenshot() As Bitmap Static cap As Bitmap = Nothing If cap IsNot Nothing Then GC.RemoveMemoryPressure(cap.Width * cap.Height * 4) cap = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppRgb) GC.AddMemoryPressure(cap.Width * cap.Height * 4) Using graph As Graphics = Graphics.FromImage(cap) graph.CopyFromScreen(0, 0, 0, 0, cap.Size, CopyPixelOperation.SourceCopy) End Using Return cap End Function





收到截图:





Receiving screenshots:

Private Sub readCallback(result As IAsyncResult)

        If closeMe Then Return

        Try
            networkStream = clientSocket.GetStream()
            Dim read As Integer = networkStream.EndRead(result)
            If read = 0 Then
                networkStream.Close()
                clientSocket.Close()
                Return
            End If

            Dim buffer As Byte() = TryCast(result.AsyncState, Byte())
            Dim data As String = Encoding.UTF8.GetString(buffer, 0, read)

            Dim cmd As String = Nothing
            If data.Contains(":") Then
                cmd = data.Split(":")(0)
                data = data.Remove(0, cmd.Length + 1)
            Else
                cmd = data
                data = Nothing
            End If

            Select Case (cmd) '! = request,


= response
Case
= response Case "


scr
main.crossThread(Utils.EnumCrossThreadFunction.updateWatchWindow,Utils .getArgs(Utils.bytesToImage(Convert.FromBase64String(data))))
Case
scr" main.crossThread(Utils.EnumCrossThreadFunction.updateWatchWindow, Utils.getArgs(Utils.bytesToImage(Convert.FromBase64String(data)))) Case "


这篇关于只有部分图像通过TCP发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆