Re: Image data via Winsock



Mike Williams wrote:
"Steinar" <leebouf@xxxxxxxxxxx> wrote in message
news:E7WdnXBYIOeuCW_fRVnzvA@xxxxxxxxxxxxxx


. . . and then the client component would have to write it
to disk.  Then that same component would have to open
the file as bitmap [by reading it back from disk] . . .


Actually I've never written a DLL so I'm a bit out of my depth here, but
perhaps a routine that will transfer a picture file from a byte array into
the Picture property of VB picture box may be of help to you. In your case,
for example, you would transfer the picture file into a byte array (the
entire file, as it would appear on disk) and send that byte array over your
Winsock connection. Then at the receiving end you use the routine to
transfer the byte array into a VB picture box. The routine effectively does
the same job as the VB LoadPicture function, except that instead of loading
a file from disk it instead loads it from a Byte array. This method will
work for both bitmap and jpeg files. I'm not sure whether this will help you
to solve your current specific problem, but its sometimes a useful thing to
be able do do anyway, so it'll be worth hanging on to. Paste the following
code into a VB Form containing a picture box and a command button:

Mike

Option Explicit
Private Const GMEM_MOVEABLE = &H2
Private Declare Function GlobalAlloc Lib "kernel32" _
  (ByVal uFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" _
  (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" _
  (ByVal hMem As Long) As Long
Private Declare Sub MoveMemory Lib "kernel32" _
  Alias "RtlMoveMemory" (pDest As Any, _
  pSource As Any, ByVal dwLength As Long)
Private Declare Function CLSIDFromString Lib "ole32" _
  (ByVal lpsz As Any, pclsid As GUID) As Long
Private Declare Function OleLoadPicture Lib "olepro32" _
  (pStream As Any, ByVal lSize As Long, _
  ByVal fRunmode As CBoolean, riid As GUID, _
  ppvObj As Any) As Long
Private Type GUID
  dwData1 As Long
  wData2 As Integer
  wData3 As Integer
  abData4(7) As Byte
End Type
Private Enum CBoolean
  CFalse = 0
  CTrue = 1
End Enum
Private Declare Function CreateStreamOnHGlobal _
  Lib "ole32" (ByVal hGlobal As Long, _
  ByVal fDeleteOnRelease As CBoolean, _
  ppstm As Any) As Long
Private Const sIID_IPicture = _
  "{7BF80980-BF32-101A-8BBB-00AA00300CAB}"
Private b1() As Byte

Private Function PictureFromBits(abPic() As Byte) _
  As IPicture  ' *not* a StdPicture
Dim nLow As Long
Dim cbMem  As Long
Dim hMem  As Long
Dim lpMem  As Long
Dim IID_IPicture As GUID
Dim istm As stdole.IUnknown
Dim ipic As IPicture
On Error GoTo Out
nLow = LBound(abPic)
On Error GoTo 0
cbMem = (UBound(abPic) - nLow) + 1
hMem = GlobalAlloc(GMEM_MOVEABLE, cbMem)
If hMem Then
  lpMem = GlobalLock(hMem)
  If lpMem Then
    MoveMemory ByVal lpMem, abPic(nLow), cbMem
    Call GlobalUnlock(hMem)
    If (CreateStreamOnHGlobal(hMem, CTrue, istm) = 0) Then
      If (CLSIDFromString(StrPtr(sIID_IPicture), _
        IID_IPicture) = 0) Then
        Call OleLoadPicture(ByVal ObjPtr(istm), _
          cbMem, CFalse, IID_IPicture, PictureFromBits)
      End If
    End If
  End If
End If
Out:
End Function

Private Sub Command1_Click()
' Test the PictureFromBits routine by loading a picture
' file (jpg in this example) from a disk file into a byte
' array (the entire file) and use the function to assign
' the byte array data to a VB picture box Picture property.
Open "c:\tulips.jpg" For Binary As #1
ReDim b1(1 To LOF(1)) 'set byte array to size of file
Get 1, , b1 ' get the file into the array
Close 1
Picture1.Picture = PictureFromBits(b1)
End Sub




This is really an excellent piece of code!
If you didn't write it yourself, where did you find it? Programming Examples like this are awesome!



Sinna .