Solved

Send a picture with winsock

Posted on 2004-08-04
1
472 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

758 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

17 Experts available now in Live!

Get 1:1 Help Now