Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.IO;
- using System.Text.RegularExpressions;
- using System.Drawing;
- using System.Windows.Forms;
- namespace Memo
- {
- static class HTMLImageParser
- {
- static public List<Image> Parse(string URL)
- {
- List<string> imageLinksCollection = new List<string>();
- WebRequest wr = WebRequest.Create(URL);
- WebClient wc = new WebClient();
- wc.Proxy = null;
- string fileS;
- using (var file = new StreamReader(wr.GetResponse().GetResponseStream()))
- {
- fileS = file.ReadToEnd();
- }
- Regex jpgReg = new Regex(@"<\s*img [^\>]*src\s*=\s*([""\'])(.*?)\1", RegexOptions.IgnoreCase);
- Match jpg = jpgReg.Match(fileS);
- while (jpg.Success)
- {
- string s = jpg.Value;
- if (s.Contains("http"))
- {
- s = s.Substring(s.IndexOf("http"), s.IndexOf("\"", s.IndexOf("http"), s.Length - s.IndexOf("http")) - s.IndexOf("http"));
- imageLinksCollection.Add(s);
- }
- jpg = jpg.NextMatch();
- }
- var imagesLinks = imageLinksCollection.Distinct();
- List<Image> Images = new List<Image>();
- foreach (string link in imagesLinks)
- {
- try
- {
- byte[] bFile = wc.DownloadData(link);
- MemoryStream ms = new MemoryStream(bFile);
- Images.Add(Image.FromStream(ms));
- }
- catch (Exception)
- {
- }
- }
- return Images;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement