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
Printable View
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
Are you referring to the entire image file, like a .gif file, or just the image data only.
What protocol is being used? HTTP? "Blast the bytes and close?" Something else?
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
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).
Doesn't AsyncRead also save to the user's hard drive?
Not unless you ask it to.
Quote:
vbAsyncTypePicture The data is provided in a Picture object.
OK, got it. Thanks.
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.
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.
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.