Results 1 to 11 of 11

Thread: winsock grab image source in data arrival

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    137

    winsock grab image source in data arrival

    i want an example how winsock can grab image in dataarrival and put on picture1

    i want to se some code for winsock dataarrival grabs image with name imgsrc any winsock example will do

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: winsock grab image source in data arrival

    Are you referring to the entire image file, like a .gif file, or just the image data only.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  3. #3
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: winsock grab image source in data arrival

    What protocol is being used? HTTP? "Blast the bytes and close?" Something else?

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: winsock grab image source in data arrival

    Code:
    Private DownloadData As String
    
    Private Sub Command1_Click()
     '
     ' Close the socket and connect to the HTTP site
     '
     Winsock1.Close
     Winsock1.Connect "www.vbforums.com", 80
    End Sub
    
    Private Sub Winsock1_Connect()
     '
     ' Here you have made a successful connection
     '
     Dim s As String
     
     DownloadData = ""
     
     '
     ' Send the required request to get a file
     '
     s = "GET /images/logo.gif HTTP/1.1" & vbCrLf
    
     s = s & "Connection: Keep-Alive" & vbCrLf
     s = s & "Host: www.vbforums.com" & vbCrLf
      
     Winsock1.SendData s & vbCrLf
    End Sub
    
    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
     '
     ' Here the HTTP server has sent you the requested data
     '
     Dim s As String
     
     Winsock1.GetData s
     
     DownloadData = DownloadData & s
    End Sub
    
    Private Sub Winsock1_Close()
     '
     ' Only here are you sure you have all of the data
     '
     Dim q As Integer
     Dim s As String
      
     Winsock1.Close
      
     '
     ' We have to strip off the HTML header info.
     '
     q = InStr(DownloadData, vbCrLf & vbCrLf)
     
     If q > 0 Then
       '
       ' Now we have the pure image data
       '
       s = Mid(DownloadData, q + 4)
     End If
      
     '
     ' Save the image in the folder
     '
     Open App.Path & "\DownloadedImage.gif" For Binary As #1
     Put #1, 1, s
     Close #1
     
     MsgBox "DONE"
    End Sub


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: winsock grab image source in data arrival

    Why not just look up the VB6 AsyncRead method? This can do an HTTP or HTTPS GET request and return a StdPicture result you can directly assign to your PictureBox.Picture property. No need to write to disk.

    It is also far more efficient than using all those String operations that convert to and from Unicode (and may even work - if you get lucky).

  6. #6
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: winsock grab image source in data arrival

    Doesn't AsyncRead also save to the user's hard drive?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  7. #7
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: winsock grab image source in data arrival

    Not unless you ask it to.

    vbAsyncTypePicture The data is provided in a Picture object.

  8. #8
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: winsock grab image source in data arrival

    OK, got it. Thanks.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  9. #9
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: winsock grab image source in data arrival

    BTW: Even using vbAsyncTypePicture it still saves the image as a file on the hard drive. I know this because I used my Web Site to download a GIF image and AsyncRead copied it directly into Picture1.Picture but when I ran the application the 2nd time it did not retrieve the image from my Web Site but rather from the local hard drive, just like IE does.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  10. #10
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: winsock grab image source in data arrival

    If you want to bypass the WinINet cache that underlies IE, the INet control, AsyncRead, etc. that's a different story. But even then you'd be better off using something like the WinHttpRequest object. That won't create a StdPicture object result, but you can easily feed the downloaded binary into a WIA.ImageFile to create a StdPicture object.

    If you can live without HTTPS support, and you don't think the target Web server might send the image back using HTTP multipart, and many other stars align just right...

    ... you could fix the code you provided by using Byte arrays instead of Strings to avoid the overhead and potential corruption of multiple ANSI to Unicode and back transformations.

    And then you still have the option of using WIA or another method to get a StdPicture without writing anything to disk.

  11. #11
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: winsock grab image source in data arrival

    Actually I am quite pleased with the way AsyncRead works and the caching is transparent to me so that doesn't matter. It never entered my mind to use AsyncRead until you made post #6.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width