Solved

Send a picture with winsock

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

790 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