Hello,
That's because you don't use stream and bitmpa properly, after a few iterations you'll get this exception because you have a memory leak.
MemoryStream implements
IDisposable
and it is good practice to dispose of it when you have finished using it. One easy way to do this is to use
using
What I would do first is refactor a bit to eliminate the leaks:
Dim unsafeCodec As IUnsafeCodec = New UnsafeStreamCodec(80)
Try
While streamDesktop And client.Connected
Using bmp As Bitmap = CaptureDesktop()
Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim size As Size = New System.Drawing.Size(bmp.Width, bmp.Height)
Dim bmpData As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat)
Using stream As New MemoryStream()
unsafeCodec.CodeImage(bmpData.Scan0, rect, size, bmp.PixelFormat, stream) 'no more error here.
If stream.Length > 0 Then
MsgBox(stream.ToArray.Length)
Send("Desktop|", , stream.ToArray)
End If
bmp.UnlockBits(bmpData)
End Using
End Using
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
Valery