Rob P.
Rob P.

Reputation: 15091

Converting an HtmlElement into an Image?

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.

Upvotes: 0

Views: 3984

Answers (2)

Sinan Ünür
Sinan Ünür

Reputation: 118166

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

Upvotes: 1

David Hedlund
David Hedlund

Reputation: 129832

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

Upvotes: 3

Related Questions