How to save image(picture) from WebBrowser control
-
Monday, October 29, 2007 9:19 AM
Hi every one
i have a webbrowser control which helps me browsing internet in my application. when i get a picture (image) i needed, i want to save it into local drive. i could do it by right-clicking the image and in the context menu by clicking "Save Picture As...". But i wanted to do it programaticlly. i wanted to save the picture programaticlly. I googeled a lot i cann't find any thing that helps me. please help me.
thanks
All Replies
-
Monday, October 29, 2007 9:48 AM
-
Wednesday, October 31, 2007 10:06 AM
Hi Vien,
This code sample may be helpful.
It can download image file from internet/website (using HttpWebRequest and HttpWebResponse classes) to local computer.
Prerequisites: Button1, Label1 and ProgressBar1 on Form1.
Code BlockPrivate Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim file As String = "http://URL/image.jpg" 'Specify URL of an image
Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create(file)
Dim response As System.Net.HttpWebResponse = request.GetResponse()
Dim stream As System.IO.Stream = response.GetResponseStream()
' Get the length of the content
Dim length As Integer = response.ContentLength
' Set the maximum length of the progress bar.
ProgressBar1.Maximum = length
' Create a temporary array for the content of the file.
Dim bytes(length) As Byte
' Get all bytes of the content and advance the progress bar.
For i As Integer = 0 To length - 1
bytes(i) = stream.ReadByte()
ProgressBar1.Value = i
Label1.Text = i.ToString + "Bytes Downloaded"
Application.DoEvents()
Next
' Write the content to the local file.
Using output As IO.Stream = System.IO.File.Create("D:\newImg.jpg")
output.Write(bytes, 0, bytes.Length)
End Using
End Sub
Regards,
Martin
-
Wednesday, October 31, 2007 10:36 AM
Hi Vien,
I work it out.
An image format in HTML is like this: <img src="http://www.google.co.uk/logos/halloween07.gif">
Step 1. Get all img elements:
Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img")
Step 2. Get each URL of img element:
For Each curElement As HtmlElement In theElementCollection
Dim imgURL As String = curElement.GetAttribute("src").ToString
Next
Step 3. Then download image file using HttpWebRequest and HttpWebResponse classes.
The following code sample can save each image from webpage loaded in WebBrowser1 to local computer.
Prerequisites: WebBrowser1 and Button1 on Form1.
Code BlockPublic Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("http://msdn2.microsoft.com/en-us/default.aspx")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'<img src="http://www.google.co.uk/logos/halloween07.gif">
Dim theElementCollection As HtmlElementCollection = Me.WebBrowser1.Document.GetElementsByTagName("img")
Dim n As Int32 = theElementCollection.Count
Dim index As Int32 = 1
For Each curElement As HtmlElement In theElementCollection
'MessageBox.Show(curElement.GetAttribute("src").ToString)
Dim imgURL As String = curElement.GetAttribute("src").ToString
Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create(imgURL)
Dim response As System.Net.HttpWebResponse = request.GetResponse()
Dim stream As System.IO.Stream = response.GetResponseStream()
' Get the length of the content
Dim length As Integer = response.ContentLength
Dim bytes(length) As Byte
For i As Integer = 0 To length - 1
bytes(i) = stream.ReadByte()
Application.DoEvents()
Next
' Save each image in turn.
Using output As IO.Stream = System.IO.File.Create("D:\Image" + index.ToString + ".jpg")
output.Write(bytes, 0, bytes.Length)
End Using
index += 1
Next
End Sub
End Class
Best regards,
Martin
-
Saturday, November 03, 2007 7:56 PM
Use URLDownloadToCacheFile. The image is likely in the IE cache already.
-
Thursday, August 20, 2009 2:57 PMthank you everybody..this answers also helped me in my code!
Regards -
Saturday, November 07, 2009 5:56 PMagreed much thanks exactly what i needed


