I tried the following code (courtesy of strongm) to receive 25k .jpg camera capture pictures from web cameras from 20 other computers via a local network and put in in an image box.
I send the size of the stream first then accumulate the picture up to the size of the stream.
I use indexed winsocks and streams to handle 20 computers effectively at the same time without getting any interference.
The pictures are fine BUT I find I get what looks like a memory leak of about 4k every time a stream is used to show the picture.
The computer soon "runs out of resources" as the available memory shrinks even with only one computer connected.
The winsock part doesnt leak memory (if I stop feeding the accumulated data to the stream)
I use what I presumed to be the clean up routine after every receive and the picture has been fed to the image box.
Can anybody suggest what I have missed?
The following is the guts of the code that processes the picture.
I send the size of the stream first then accumulate the picture up to the size of the stream.
I use indexed winsocks and streams to handle 20 computers effectively at the same time without getting any interference.
The pictures are fine BUT I find I get what looks like a memory leak of about 4k every time a stream is used to show the picture.
The computer soon "runs out of resources" as the available memory shrinks even with only one computer connected.
The winsock part doesnt leak memory (if I stop feeding the accumulated data to the stream)
I use what I presumed to be the clean up routine after every receive and the picture has been fed to the image box.
Can anybody suggest what I have missed?
The following is the guts of the code that processes the picture.
Code:
Sub Winsock_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim StreamStats As olelib.STATSTG
Dim gdiImageTarget As Long
Dim GpInput As GdiplusStartupInput
Dim Token As Long
Dim Buffer() As Byte
Dim MessageBuffer() As String
Dim Message As String
If SentStreamSize(Index) = 0 Then
'no data detected yet
Winsock(Index).GetData SentStreamSize(Index), vbLong 'size of received data received in advance
Else
'Start accumulating picture data into the stream
ReDim Buffer(0) As Byte ' reset input buffer
Winsock(Index).GetData Buffer, vbArray Or vbByte ' read the bytes of picture array
myStream(Index).Seek 0, STREAM_SEEK_END
myStream(Index).Write Buffer(0), bytesTotal ' append to a stream
myStream(Index).Stat StreamStats, STATFLAG_NONAME
If SentStreamSize(Index) <= StreamStats.cbSize * 10000 Then
'we've accumulated all the data
GpInput.GdiplusVersion = 1
If GdiplusStartup(Token, GpInput) <> Ok Then
‘Fault
MsgBox "Error loading GDI+!", vbCritical
BusPicturePresent(Index) = 0
Set MainForm.Image1(Index).Picture = LoadPicture("")
Else
‘show picture
GdipLoadImageFromStream myStream(Index),
gdiImageTarget 'load GDI+ image from stream (JPEG)
ResetStream Index 'reset that stream for reuse
'working with a StdPicture object created from the GDI+ image
Set MainForm.ImageCamera(Index).Picture = CreatePictureFromGDIPlusImage(gdiImageTarget)
End if
GdipDisposeImage gdiImageTarget
SentStreamSize(Index) = 0
End If
End If
If Token Then GdiplusShutdown Token
End Sub
Sub ResetStream(Index As Integer)
Dim b(0) As Byte ' declaration to get an HGlobal
Set myStream(Index) = Nothing
Set myStream(Index) = CreateStreamOnHGlobal(b(0), True)
End Sub
Function CreatePictureFromGDIPlusImage(gdiImage As Long) As StdPicture
Dim lpPictDesc As olelib.PICTDESC
Dim myUUID As olelib.UUID
Dim hBitmap As Long
Dim background As Long
olelib.CLSIDFromString IPictureGUID, myUUID
GdipCreateHBITMAPFromBitmap gdiImage, hBitmap, background
With lpPictDesc
.cbSizeofStruct = Len(lpPictDesc)
.hBitmap = hBitmap
.hpal_or_xExt = 0&
.picType = PICTYPE_BITMAP
End With
Set CreatePictureFromGDIPlusImage = olelib.OleCreatePictureIndirect(lpPictDesc, myUUID, False)
End Function