Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Send a picture with winsock

Posted on 2004-08-04
1
Medium Priority
?
501 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 17

Accepted Solution

by:
zzzzzooc earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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 …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

604 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