Moving a captured bitmap picture across a LAN nework

daneilzee

New member
Joined
Jan 11, 2008
Messages
1
Programming Experience
3-5
i writing vb.net application for webcam
a program to captured a bitmap from one computer and send it to another computer on a LAN network
the problem is that i alway get a prefect first bitmap pic on the picturebox on the client computer but one that follows it will be distorted with the first images
i tried flushing nothing works


this the server code fragment


VB.NET:
Public Sub CaptureImage()
Dim data As IDataObject
Dim bmap As Image

'---copy the image to the clipboard---
SendMessage(hWnd, WM_CAP_EDIT_COPY, 0, 0)

'---retrieve the image from clipboard and convert it 
' to the bitmap format
data = Clipboard.GetDataObject()
If data Is Nothing Then Exit Sub
If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
'---convert the data into a Bitmap---
bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
'---save the Bitmap into a memory stream---
bmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
'---write the Bitmap from stream into a byte array---
Image = ms.GetBuffer


End If
End Sub

this the client code


VB.NET:
Public Function ReceiveImage() As Boolean

Try
Dim counter As Integer = 0
Dim totalBytes As Integer = 0
Dim bytesRead As Integer = 0
Do
'---read the incoming data---
bytesRead = nws.Read(data, 0, client.ReceiveBufferSize)
totalBytes += bytesRead
'---write the byte() array into the memory stream---

s.Write(data, 0, bytesRead)
counter += 1
'Loop Until totalBytes >= SIZEOFIMAGE
Loop Until totalBytes >= SIZEOFIMAGE

'---display the image in the PictureBox control---

i += 1

Select Case i
Case 2
PictureBox2.Image = Image.FromStream(s)

End Select

PictureBox1.Image = Image.FromStream(s)
PictureBox1.Refresh()
nws.Flush()
s.Flush()
client.Close()
:confused:
 
Last edited by a moderator:
Here a sample with one of several ways to do it, using serialization with BinaryFormatter.

Client form has a PictureBox and a ToolStrip with a button.
VB.NET:
Imports System.Net.Sockets
Public Class client
    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Dim client As New TcpClient
        client.BeginConnect("127.0.0.1", 8888, AddressOf getimage, client)
    End Sub
    Private Sub getimage(ByVal ia As IAsyncResult)
        Dim client As TcpClient = ia.AsyncState
        client.EndConnect(ia)
        Dim ser As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
        safeSetImage(ser.Deserialize(client.GetStream))
        client.GetStream.Close()
        client.Close()
    End Sub
    Private safeSetImage As New delSetImage(AddressOf setImage)
    Private Delegate Sub delSetImage(ByVal bmp As Bitmap)
    Private Sub setImage(ByVal bmp As Bitmap)
        Me.PictureBox1.Image = bmp
    End Sub
End Class
Server is here an otherwise empty form.
VB.NET:
Imports System.Net.Sockets
Public Class server
    Private t As New Threading.Thread(AddressOf listen)
    Private cancel As Boolean
    Private Sub server_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        cancel = True
        t.Join()
    End Sub
    Private Sub server_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        t.IsBackground = True
        t.Start()
    End Sub
    Private Sub listen()
        Dim l As New TcpListener(Net.IPAddress.Parse("127.0.0.1"), 8888)
        l.Start()
        Do Until cancel
            If l.Pending Then
                l.BeginAcceptTcpClient(AddressOf cb, l)
            End If
            Threading.Thread.Sleep(500)
        Loop
        l.Stop()
    End Sub
    Private Sub cb(ByVal ia As IAsyncResult)
        Dim client As TcpClient = DirectCast(ia.AsyncState, TcpListener).EndAcceptTcpClient(ia)
        Dim ser As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
        ser.Serialize(client.GetStream, screenshot)
        client.GetStream.Close()
        client.Close()
    End Sub
    Private Function screenshot() As Bitmap
        Dim rct As Rectangle = Screen.PrimaryScreen.Bounds
        Dim bmp As New Bitmap(rct.Width, rct.Height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        g.CopyFromScreen(0, 0, 0, 0, rct.Size)
        g.Dispose()
        Return bmp
    End Function
End Class
 
Back
Top