PDA

View Full Version : getting an image from a webbrowser on a form


John-
04-17-2004, 06:46 AM
ok i best explain everything first it will save me time explaining later.

the description for this section is If you know your Shiznit this is the place for you!

well as you know i do not know my shiznit when it comes to programming, but i think the answer to this question isnt a simple one because i have asked so many people and they havent been able to answer me.


basically i was talking on irc with a few friends and decided i would try to make a hotmail account maker, just to save a little time, i had saw one on planet source code a while back but it didnt work anymore.

i didnt want to use the winsock or inet control (i think that was a mistake now)
so i went with the webbrowser control simply because i found an easy method of submitting the information to the webpage.

now when it came to the verification picture i got a little stuck, i thought if there was someway i coulc put that image into the form in a picturebox i would be ok, so i first tried opening a seperate webbrowser just for the verification image alone, but then realise the picture would be different from the one in the other webbrowser which i am submitting the information to.

so basically what i need to do is one of the following:

either copy the image from the webbrowser and paste it into an picturebox on the form.
or save the picture from the webbrowser on the form, to a location on my hard drive, then i could probably just load the picture into a picturebox.


i have asked this question or some variation of it in the programming chat rooms on yahoo. i have visited a few visual basic irc channels, still didnt get what i needed.

i asked on moorer-software.com/forums
mo.silent-interlude.net/forum
vbcity.com/forums
vbforums.com
austin-inc.com/forums/main.php

i have also done searchs on visualbasicforum.com and endless searchs on google with different keywords.

any help would be as usualy greatly appreciated.

John-
04-22-2004, 06:10 PM
hopefully the reason there are not any replies is because you all missed this topic, and not because your not sure of the answer guys (and girls :) ).

baloney_mahoney
05-11-2004, 08:49 AM
On the same Form that has the WebBrowser Control put a picture box.

Add this code to your VB project:

Private Sub Picture1_Click()
Picture1.Picture = Clipboard.GetData
End Sub

Right mouse click on the picture in the WebBrowser control and then select Copy.
Move the mouse over the picture control and click.

John-
05-11-2004, 11:03 AM
thanks for your reply,

is there any way to automate the right click on the picture and copy,
and click on picturebox.

baloney_mahoney
05-11-2004, 12:33 PM
Yes. You will need to parse the HTML code for the link to the security picture, download the image code and then load the image code into the picture box.

Below is some code taken from a ID maker I wrote that should do what you want:

On the Form that has the WebBrowser Control and the PictureBox Control add a Winsock Control and name it 'sckSecurityWord'.

Add the following to the Declarations Area of your code:

Dim SecurityImagePointer As Integer
Dim HTML_Code As String
Dim ImageData As String

Now add the below subroutines to your project:

Private Sub WebBrowser1.TitleChange(ByVal Text As String)


If Instr(Text, "..a string that identifies the title of the page that has the security image on it...") = 0 Then Exit Sub

'
' Now get the inner HTML code from the page
'
HTML_Code = WebBrowser1.Document.documentelement.innerhtml

'
' I am assuming that you are using the Yahoo security picture so you need to find the link to that picture
'
' Below is an example of the security picture link embedded somewhere within the HTML code:
'
' <img src="http://reg.yimg.com/i/HylPdOdZFem6m51n32BGQRJ9KxJ83coFfB1_C8VVftc7Q.UUXP 0RyPriCg--.jpg" width=290 height=80 alt="" border="0">
'
SecurityImagePointer = InStr(1, HTML_Code, "<img src=" & Chr(34) & "http://reg.yimg.com/i/")


If SecurityImagePointer <> 0 Then
GetSecurityImage
Else
MsgBox "No Security Picture Found"
End If
End Sub

'================================================= ==================
' Below code is for retrieving the security word image
'================================================= ====================
Private Sub GetSecurityImage()
sckSecurityWord.Close
sckSecurityWord.Connect "reg.yimg.com", 80
End Sub

Private Sub sckSecurityWord_Connect()
Dim String1 As String
Dim ImageQueryString As String

String1 = GetSecurityImagePath()

ImageQueryString = "GET /" & String1 & " HTTP/1.0" & vbCrLf & vbCrLf

sckSecurityWord.SendData ImageQueryString
End Sub

Private Sub sckSecurityWord_DataArrival(ByVal bytesTotal As Long)
sckSecurityWord.GetData s1, vbString
ImageData = ImageData & s1
End Sub

Private Sub sckSecurityWord_Close()
Dim i As Integer

sckSecurityWord.Close

i = InStr(1, ImageData, "Content-Type: image/jpeg")
ImageData = Mid(ImageData, i + 28)

On Error Resume Next

Open App.Path & "\security_image.jpg" For Binary As #1
Put #1, 1, ImageData
Close #1

Picture1.Picture = LoadPicture(App.Path & "\security_image.jpg")

Kill App.Path & "\security_image.jpg"
End Sub

Private Function GetSecurityImagePath() As String
Dim Ptr1 As Long
Dim Ptr2 As Long

Ptr1 = SecurityImagePointer + 30
Ptr2 = InStr(Ptr1, HTML_Code, ".jpg" & Chr(34))

GetSecurityImagePath = Mid(HTML_Code, Ptr1, (Ptr2 + 4) - Ptr1)
End Function

John-
05-11-2004, 03:58 PM
i really appreciate you taking this time to help me with this.

i skimmed through the code really quickly and noticed yahoos authentication image link is different to hotmails.

where as yahoo`s look something similar to this

http://reg.yimg.com/i/kuy.XudZFekf_ocn3Oza9yucwjM55E4lVXwId8SIwxiLJhqQ1l cBEHVf.jpg

if you put that link into the browser it would take you to a particular image, but with hotmails there verification link looks like this,

https://registernet.passport.net/hip.srf?ns=hotmail.com&int=reg

it is always that same link, but a different picture each time you visit the link.

so if i download the picture using a winsock control it will be different from the picture in the webbrowser on the form, and i am submitting the information like name password email address and verification code to the webbrowser.

which means the codes would not match.

baloney_mahoney
05-11-2004, 04:31 PM
So, in essense, you really need to be able to take the image directly from the page that is shown in your WebBrowser Control window and transfer it directly to a picture box.

Post the part of your code that makes the initial connection. I want to run a small VB program that displays the security image in a WebBrowser Control window so I can visually see what I have to do to capture that same image in a picture box and I will re-post the results.

John-
05-12-2004, 02:38 AM
here you go

Private Sub About_Click()
Form2.Show
End Sub
'this part here is the part that submits the information to the webbrowser
'it is using the aspen2k method which you can find here http://www.xtremevbtalk.com/showthread.php?t=149732&highlight=form+webpage+aspen2k
Private Sub Command1_Click()
With WB2.Document
.All("FirstName").Value = Text1.Text
.All("LastName").Value = Text2.Text
.All("UpdateLanguage").Value = "False"
.All("UpdateCountry").Value = "False"
.All("Region").Value = "33145"
.All("PostalCode").Value = "77351"
.All("TimeZone").Value = "0"
.All("Gender").Value = "m"
.All("Day").Value = "10"
.All("Month").Value = "7"
.All("Year").Value = "1976"
.All("Occupation").Value = "A"
.All("SignInName").Value = Text3.Text
.All("Password").Value = Text4.Text
.All("ConfirmedPassword").Value = Text4.Text
.All("SecretQuestion").Value = "Favorite movie?"
.All("SecretAnswer").Value = Text5.Text
.All("newuser").Value = "yes"
.All("HIPImageSolution").Value = Text6.Text

.All("Submit").Click
End With
End Sub

Private Sub Form_Load()
'when the form loads the webbrowser on the form will navigate to the hotmail page which is used to signup
WB2.Navigate "https://register.passport.net/reg.srf?lc=2057&id=2&cbid=24325&tw=20&kpp=2&svc=mail&msppjph=1"
End Sub

Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next
Unload Form2
Unload Me
End Sub
'this was by baloney mahoney
Private Sub Picture1_Click()
Picture1.Picture = Clipboard.GetData
End Sub



i can upload the project files if you like.

baloney_mahoney
05-12-2004, 10:50 AM
Well, John, I can see your dilemma. But, there is more than one way to kill a cat. Using the method that you are using I can see no direct way to capture the security image from the WebBrowser window and copy it to a picture box control. So, I ask you this. Are you using the WebBrowser to show the registration page to the user of your program and have the user fill in the fields on the page itself? If so, then why do you need the security picture copied to a picture box? Why wouldn't the user just fill in the security word directly on the document showing in the WebBrowser window? If not, then are you trying to show both the Webbrowser window and the picture box at the same time on the same form? Or, is the WebBrowser window invisible to the user and only the picture box with the security image and text boxes on your form visible to the user? I have a couple of ideas but they tend to be more complex and I don't want to start experimenting with them unless I have a clear idea of exactly what your intentions are. If explaining your exact intentions is too involved then perhaps it would help if you sent me your VB project then I can see more clearly what your overall idea is. If you do, email me your project to baloney_mahoney@futureedge.com. I know you mentioned that you would rather not use Winsock but maybe that is a better approach to your problem.

John-
05-12-2004, 04:04 PM
ok i sent the files, by email.

baloney_mahoney
05-12-2004, 05:37 PM
OK, I got your project via email. I made some changes to it and sent you an email with the link so that you can download the modified program.

In addition to changing a few things (to get it to work the way you want it to) I also made the following changes:

1) Changed Form to Fixed Single to prevent users from expanding it
2) Centered Form on Desktop
3) Changed Form Scale Mode to Pixels

Have question. After user clicks on the 'Submit' button then what? How do you intend to handle the return response if only the picture box is displaying the image and not the WebBrowser Control itself?

John-
05-12-2004, 06:40 PM
ok thank you, i just replied again :)

baloney_mahoney
05-12-2004, 06:54 PM
OK, let me know if that is acceptable. I am assuming that the image will always be the same size and that it will always be in the same location on the Web page sent to your browser control. I tested it about 50 times and always it was same size and same location.

Since you cannot, per se, extract the image from the page and transfer it to a picture box using the WebBrowser control the only other way was to use Winsock(also maybe Inet, but not sure). However, using Winsock will definitly be very complex and maybe not worth the time and effort if what I did is satisfactory with you.

John-
05-13-2004, 04:23 AM
yep the image is always the same size and always in the same place or at least it was when i tested it before.

i have replied again to your email address mate.

baloney_mahoney
05-13-2004, 09:02 AM
OK, I got your email with the 5 pictures and I now know what the problem is.

Because you are using WebBrowser Control you are at the mercy of your Internet Explorer browser and how it wants to display the HTML document.

I have Windows 98 using IE 6.0 with my own browser settings (text size set to normal, for example). Someone else will have different settings and/or a different version of IE. The WebBrowser Control simply takes what the IE browser gives it and the page will look different because their settings will be different from yours and mine. This causes the location of the image to shift either up or down and therefore there is no guarantee that the image will always align correctly on different PCs using IE with different settings.

Unfortunately this defeats the idea of what I did by putting the WebBrowser control inside the picture box.

I know of no way to 'extract the image' from the WebControl data and transfer that image to a picture box. I am not an expert on using WebBrowser because I always use Winsock (more complex but has the power to do anything).

So, unless I am wrong and it can be 'extracted' then the only other way to accomplish what you want is to use a Winsock Control instead of the WebBrowser Control.

The Winsock Control will read the HTML data into a String. From the String you will parse the data until you find the link that is used to retrieve the image. You will now use that link and with the Winsock Control retrieve the image and save the image data either in a String or on the hard drive.

Now, you will copy the image data into the picture box.

Now you need to set up a parameter string containing the same information that you had when you 'Submitted' the information from your VB program. Again using Winsock, you send that data string back to the server. The server responds by sending data back to your program via Winsock.

That's it. A little somewhat complex but it will do what you want to accomplish.

I will help you with the Winsock coding if you want to take this approach.

Let me know.