views:

465

answers:

2

I'm using a WebBrowser control in VB.Net to load a website. At that point, the WebBrowser.Document.Images property returns a collection of HtmlElement that are considered images.

What I'm trying to do at this point, is take a particular HtmlElement that represents an image and turn it into a System.Drawing.Image so that I can manipulate it. But I can't figure out how.

I did try to search for an answer, but came up with nothing. 'WebBrowser', as it turns out, seems to be a really popular keyword.

Can anyone point me in the right direction?

EDIT: It's been suggested that I use the SRC attribute of the HtmlElement to download the image; but the image can be dynamic - meaning the image I download can be separate from the image on the website....so, that won't work for my purposes.

+3  A: 

I haven't worked with the WebBrowser object, but from the image you should be able to get the src-attribute somehow, and using that, you could do a request to that:

HttpWebRequest wr = (HttpWebRequest) WebRequest.Create(url);
wr.Method = "GET";

and then you should be able to treat the response stream as an image:

Image img = System.Drawing.Image.FromStream(wr.GetResponse().GetResponseStream());
img.Save(...);
David Hedlund
+1 HttpWebRequest. I did not realize it had a `GetResponseStream` method.
Sinan Ünür
This is a good answer; but unless I'm misunderstanding, it doesn't really work for my situation. When I load the website in the WebControl the image generated and shown in the web-browser is the one I need. If I use the src-attribute and make a second request, the image returned will not be the same image as the one in the WebBrowser.
Rob P.
+1  A: 

Well, next time, try IWebBrowser as the keyword. That should lead you to the MSDN documentation.

I am not positive but I do not think what you want can be done directly. However, you could use the src property through the IHTMLImgElement to download the image to %TEMP% and initialize a System.Drawing.Image object using the FromStream method

Sinan Ünür