?
Solved

Send a picture with winsock

Posted on 2004-08-04
1
Medium Priority
?
497 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

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…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

765 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