Solved

Send a picture with winsock

Posted on 2004-08-04
1
474 Views
Last Modified: 2013-11-13
I have asked this question many times and I can not get a answer that works. How would I send a picture with winsock from a picture box without saveing it first? Please include source code. Heres a link to my other post's so you can get a better idea of what im doing. http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21081481.html
0
Comment
Question by:Rickyman45
1 Comment
 
LVL 17

Accepted Solution

by:
zzzzzooc earned 500 total points
ID: 11723123
This should work (worked for me). Since it's based on the .Image property of the PictureBox, the sizes of the PictureBox need to be correct (use the same sized PictureBoxes for testing!). If not, the images will be distorted.

Just load a picture into Picture1 (and of course other things, such as adding the Winsock controls, etc.).

Form1:
-------------
Option Explicit

Private Type BITMAP
    bmType As Long
    bmWidth As Long
    bmHeight As Long
    bmWidthBytes As Long
    bmPlanes As Integer
    bmBitsPixel As Integer
    bmBits As Long
End Type

Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)

Private bytBitsReceived() As Byte
Private Sub Form_Load()
    'Set sckReceive to listen for connection...
    sckReceive.LocalPort = 5555
    Call sckReceive.Listen
    'Connect to sckReceive...
    Call sckSend.Connect("127.0.0.1", 5555)
End Sub
Private Sub sckReceive_ConnectionRequest(ByVal requestID As Long)
    'Accept connection...
    Call sckReceive.Close
    Call sckReceive.Accept(requestID)
End Sub
Private Sub sckSend_Connect()
    'Connected!
    'Get Picture1's bitmap bits...
    Dim bytBits() As Byte
    If PictureBitsGet(Picture1, bytBits) = True Then
        'Send bits...
        Call sckSend.SendData(bytBits)
    End If
End Sub
Private Sub sckReceive_DataArrival(ByVal bytesTotal As Long)
    Static blnDim As Boolean
    'Incoming data (bits)...
    Dim bytTemp() As Byte, lngPos As Long
    'Get the bits...
    If sckReceive.State = sckConnected Then
        Call sckReceive.GetData(bytTemp)
        'Copy the bits to the "main" bits array...
        If blnDim = False Then
            ReDim bytBitsReceived(1) As Byte
            blnDim = True
        End If
        lngPos = UBound(bytBitsReceived)
        ReDim Preserve bytBitsReceived(UBound(bytBitsReceived) + UBound(bytTemp) + 1) As Byte
        Call CopyMemory(bytBitsReceived(lngPos), bytTemp(0), UBound(bytTemp) + 1)
    End If
End Sub
Private Sub sckSend_SendComplete()
    'Finished sending.. close socket...
    Call sckSend.Close
End Sub
Private Sub sckReceive_Close()
    'Socket closed...
    'Set Picture1's bits to the newly received bits...
    If PictureBitsSet(Picture2, bytBitsReceived) = True Then
        Me.Caption = "Picture received!"
    End If
End Sub
Private Function PictureBitsGet(ByRef objPicBox As PictureBox, ByRef bytBits() As Byte) As Boolean
    Dim typBMP As BITMAP, lngSize As Long
    If GetObject(objPicBox.Image, Len(typBMP), typBMP) <> 0 Then
        lngSize = ((typBMP.bmWidth * 3 + 3) And &HFFFFFFFC)
        lngSize = lngSize * typBMP.bmHeight * 3
        ReDim bytBits(1 To lngSize) As Byte
        If GetBitmapBits(objPicBox.Image, UBound(bytBits), bytBits(1)) <> 0 Then
            PictureBitsGet = True
        End If
    End If
End Function
Private Function PictureBitsSet(ByRef objPicBox As PictureBox, ByRef bytBits() As Byte) As Boolean
    If SetBitmapBits(objPicBox.Image, UBound(bytBits), bytBits(1)) <> 0 Then
        PictureBitsSet = True
        Call objPicBox.Refresh
    End If
End Function
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now