asp.net collection of web page images specific methods

  • 2020-06-12 08:54:31
  • OfStack

When I looked for img tags on the Internet, Most of them were found by string operation, which was troublesome to operate, and the code looked tired. The method I'm using here is to load a page through WebBrowser, and then the HTMLDocument class does the operation eliminating the string operation and calling GetElementsByTagName directly to return all the image addresses to an HtmlElementCollection object.
The code is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public class GatherPic
    {
        private string savePath;
        private string getUrl;
        private WebBrowser wb;
        private int iImgCount;
        // Initialization parameter 
        public GatherPic(string sWebUrl, string sSavePath)
        {
            this.getUrl = sWebUrl;
            this.savePath = sSavePath;
        }
        // Start collecting 
        public bool start()
        {
            if (getUrl.Trim().Equals(""))
            {
                MessageBox.Show(" Where did the shrimp even lose the web site! ");
                return false;
            }
            this.wb = new WebBrowser();
            this.wb.Navigate(getUrl);
            // Delegated events 
            this.wb.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
            return true;
        }
        //WebBrowser.DocumentCompleted Delegated events 
        private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            // Frame in page iframe The load is complete and does not drop SearchImgList()
            if (e.Url != wb.Document.Url) return;
            SearchImgList();
        }
        // Check out all the images and collect them locally 
        public void SearchImgList()
        {
            string sImgUrl;
            // Get all image addresses 
            HtmlElementCollection elemColl = this.wb.Document.GetElementsByTagName("img");
            this.iImgCount = elemColl.Count;
            foreach (HtmlElement elem in elemColl)
            {
                sImgUrl = elem.GetAttribute("src");
                // Call save remote image function 
                SaveImageFromWeb(sImgUrl, this.savePath);
            }
        }
        // Save the remote image function 
        public int SaveImageFromWeb(string imgUrl, string path)
        {
            string imgName = imgUrl.ToString().Substring(imgUrl.ToString().LastIndexOf("/") + 1);
            path = path + "\\" + imgName;
            string defaultType = ".jpg";
            string[] imgTypes = new string[] { ".jpg", ".jpeg", ".png", ".gif", ".bmp" };
            string imgType = imgUrl.ToString().Substring(imgUrl.ToString().LastIndexOf("."));
            foreach (string it in imgTypes)
            {
                if (imgType.ToLower().Equals(it))
                    break;
                if (it.Equals(".bmp"))
                    imgType = defaultType;
            }
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imgUrl);
                request.UserAgent = "Mozilla/6.0 (MSIE 6.0; Windows NT 5.1; Natas.Robot)";
                request.Timeout = 10000;
                WebResponse response = request.GetResponse();
                Stream stream = response.GetResponseStream();
                if (response.ContentType.ToLower().StartsWith("image/"))
                {
                    byte[] arrayByte = new byte[1024];
                    int imgLong = (int)response.ContentLength;
                    int l = 0;
                    // CreateDirectory(path);
                    FileStream fso = new FileStream(path, FileMode.Create);
                    while (l < imgLong)
                    {
                        int i = stream.Read(arrayByte, 0, 1024);
                        fso.Write(arrayByte, 0, i);
                        l += i;
                    }
                    fso.Close();
                    stream.Close();
                    response.Close();
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            catch (WebException)
            {
                return 0;
            }
            catch (UriFormatException)
            {
                return 0;
            }
        }
    }
}
//----------------- The calling code --------------------
GatherPic gatherpic = new GatherPic( " http://www.baidu.com " ,"C:\test");
// Please make sure that c:\ There are test The path 
gatherpic.start()

Related articles: