C# – Converting an HtmlElement into an Image

cimagenetvb.netwebbrowser-control

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.

Best Answer

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(...);