我正在编写远程网络摄像头服务器/客户端套件。我尝试运行时遇到上述错误。附件是CaptureImage()子和ReceiveMessage()子似乎抛出错误。注意我在CaptureImage子中的Image = ms.GetBuffer和ReceiveMessage子中的SendData(Image)上抛出了错误。
Public Sub CaptureImage()
Dim data As IDataObject
Dim bmap As Image
Dim ms As New IO.MemoryStream()
SendMessage(hWnd, WM_CAP_EDIT_COPY, 0, 0)
data = Clipboard.GetDataObject()
If data Is Nothing Then Exit Sub
If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
bmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
Image = ms.GetBuffer
End If
End Sub
Public Sub ReceiveMessage(ByVal ar As IAsyncResult)
Dim bytesRead As Integer
Try
SyncLock _client.GetStream
bytesRead = _client.GetStream.EndRead(ar)
End SyncLock
If bytesRead < 1 Then
AllClients.Remove(_clientIP)
Exit Sub
Else
Dim messageRecieved As String
Dim i As Integer = 0
Dim start As Integer = 0
While data(i) <> 0
If i + 1 > bytesRead Then Exit While
If data(i) = LF Then
messageRecieved = partialStr & System.Text.Encoding.ASCII.GetString(data, start, i - start)
If messageRecieved.StartsWith("Send") Then
SendData(Image)
End If
start = i + 1
End If
i += 1
End While
If start <> i Then
partialStr = System.Text.Encoding.ASCII.GetString(data, start, i - start)
End If
End If
SyncLock _client.GetStream
_client.GetStream.BeginRead(data, 0, CInt(_client.ReceiveBufferSize), AddressOf ReceiveMessage, Nothing)
End SyncLock
Catch ex As Exception
AllClients.Remove(_clientIP)
Console.WriteLine(ex.ToString)
End Try
End Sub
非常感谢您的帮助。