Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an image from mshtml.htmlimg to hard disk

Without using API?

I know there are several way.

I am using mshtml library by the way, which is better than webbrowser control. I am effectively automating internet explorer straight.

Basically I prefer a way to take the image straight without having to know the URL of the htmlimg and download it.

I know I can take URL from the image element and downloading it with webclient. The image changes depending on cookies and IP. So that wouldn't do.

I want the exact images displayed by the htmlimg element to be the one stored.

Basically as if someone is taking a local screenshot of what shows up on screen.

like image 442
user4951 Avatar asked Jan 31 '12 10:01

user4951


1 Answers

There's an old solution for this here:

http://p2p.wrox.com/c/42780-mshtml-how-get-images.html#post169674

These days though you probably want to check out the Html Agility Pack:

http://htmlagilitypack.codeplex.com/

The documentation isn't exactly great however; so this code snippet may help:

HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);

// You can also load a web page by utilising WebClient and loading in the stream - use one of the htmlDoc.Load() overloads

var body = htmlDoc.DocumentNode.Descendants("body").FirstOrDefault();

foreach (var img in body.Descendants("img"))
{
    var fileUrl = img.Attributes["src"].Value;
    var localFile = @"c:\localpath\tofile.jpg";

    // Download the image using WebClient:
    using (WebClient client = new WebClient())
    {
        client.DownloadFile("fileUrl", localFile);
    }
}
like image 137
Robert Foster Avatar answered Oct 04 '22 20:10

Robert Foster