|
-
Nov 13th, 2012, 06:11 PM
#1
Thread Starter
Addicted Member
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
-
Nov 14th, 2012, 08:45 PM
#2
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.
-
Nov 14th, 2012, 09:49 PM
#3
Re: winsock grab image source in data arrival
What protocol is being used? HTTP? "Blast the bytes and close?" Something else?
-
Nov 15th, 2012, 01:08 PM
#4
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.
-
Nov 15th, 2012, 01:41 PM
#5
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).
-
Nov 15th, 2012, 03:21 PM
#6
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.
-
Nov 15th, 2012, 03:22 PM
#7
Re: winsock grab image source in data arrival
Not unless you ask it to.
vbAsyncTypePicture The data is provided in a Picture object.
-
Nov 15th, 2012, 03:42 PM
#8
Re: winsock grab image source in data arrival
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.
-
Nov 15th, 2012, 09:03 PM
#9
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.
-
Nov 15th, 2012, 10:35 PM
#10
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.
-
Nov 16th, 2012, 12:19 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|