janirvinfabon / image_grabber

This application uses System.Net namespace. A dot net application that allows the user to download all image source from <IMG> tag in html element.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

image_grabber

A dot net application that download an image using the URL from user. This application uses System.Net namespace. This also uses a Thread, List and linq

source class

	public class link_sources
	{
		public int No { get; set; }
		[DisplayName("Image Source")] public string ImageSource { get; set; }
		[Browsable(false)] public string ImageExtension { get; set; }
	}

main form properties

	private WebBrowser browser = new WebBrowser(); // add a web browser control

	private string defaultLocation = string.Empty;
	private string linkName = string.Empty;

on web browser control's navigating event

	private void OnPreparingNavigation(object sender, WebBrowserNavigatingEventArgs e)
	{
	    new Thread(() => 
	    {
		if (this.InvokeRequired)
		{
		    this.Invoke(new Action(() => { this.Text = string.Format("{0} | Navigating...", this.appName); }));
		}
	    }).Start();
	}

on web browser control's document completion event

	private void OnVirtualBrowserDocsComplete(object sender, WebBrowserDocumentCompletedEventArgs e)
	{
	    try
	    {
		if (this.browser.ReadyState == WebBrowserReadyState.Complete)
		{
		    this.DoLoadLinkResources();
		}
	    }
	    catch { }
	}

on download link sources

	private void DoLoadLinkResources()
        {
            try
            {
                var counter = 1;
                var tmpList = new List<link_sources>();

                foreach (HtmlElement item in this.browser.Document.GetElementsByTagName("IMG"))
                {
                    var uri = new Uri(item.GetAttribute("src"));
                    if (!string.IsNullOrEmpty(Path.GetExtension(uri.AbsolutePath)))
                    {
                        var src = item.GetAttribute("src");
                        tmpList.Add(new link_sources() { No = counter++, ImageSource = src, ImageExtension =  src.Split('.').Last().ToLower() });
                    }
                }

                this.linkSOurceList = tmpList.Distinct().ToList();
            }
            catch { }
        }

download each file

	private void Download(List<link_sources> src_list)
	{
		src_list.ForEach((x) =>
		{
			var link = x.ImageSource;
			var name = x.ImageSource.Split('/').Last().Replace("%", "");
			var file = string.Format(@"{0}\{1}\{2}", this.defaultLocation, this.linkName, name);

			try
			{
				this.check_folder(file);                            // create a seperate directory for each link
				new WebClient().DownloadFile(link, file);           // download the file
				total++;
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
			}
		});
	}

check file dir

	private void check_folder(string file)
	{
		var dir = Path.GetDirectoryName(file);
		if (!Directory.Exists(dir))
		{
			Directory.CreateDirectory(dir);
		}
	}

default download location

	c://../desktop/img_grabber_downloads

About

This application uses System.Net namespace. A dot net application that allows the user to download all image source from <IMG> tag in html element.

License:MIT License


Languages

Language:C# 100.0%