Пример #1
0
        public static async Task <byte[]> GetFeaturedImage(string url)
        {
            byte[] output;

            try
            {
                // download the page contents as a string
                var request = HttpWebRequest.CreateHttp(url);
                request.Accept = "text/html";

                var result = await request.GetResponseAsync();

                using (var reader = new StreamReader(result.GetResponseStream()))
                {
                    string          pageContents = reader.ReadToEnd();
                    MatchCollection matches      = Regex.Matches(pageContents, @"<img ([^>]+)>");

                    // build a list of all img tags
                    IList <ImageTag> imageTags = new List <ImageTag>();
                    for (int i = 0; i < matches.Count && i < AppSettings.MaxImagesToLoad; i++)
                    {
                        imageTags.Add(await ImageTag.GetFromUrl(matches[i].Value, url));
                    }

                    var featured = imageTags.OrderByDescending(t => t.Width * t.Height).First();
                    output = featured.ImageBytes;
                }
            }
            catch
            {
                string fileName = Path.Combine(_webRootPath, AppSettings.DefaultPreviewImage);
                output = File.ReadAllBytes(fileName);
            }

            return(output);
        }