static string GetCachedOrDownloadImage(Uri url) { if (url == null) { return(string.Empty); } try { using (var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider()) { string iconUrlHash = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(url.ToString()))); string thumbsPath = ServiceRegistration.Get <IPathManager>().GetPath(@"<DATA>\News\Thumbs"); if (!System.IO.Directory.Exists(thumbsPath)) { System.IO.Directory.CreateDirectory(thumbsPath); } string originalExtension = System.IO.Path.GetExtension(url.LocalPath); string extension = originalExtension.ToLower() == ".gif" ? ".png" : originalExtension; string fileName = System.IO.Path.Combine(thumbsPath, iconUrlHash) + extension; if (!System.IO.File.Exists(fileName)) { using (var client = new CompressionWebClient()) { if (extension == originalExtension) { client.DownloadFile(url, fileName); } else { using (var imageData = client.OpenRead(url)) { System.Drawing.Image image = System.Drawing.Image.FromStream(imageData, true, true); image.Save(fileName, System.Drawing.Imaging.ImageFormat.Png); } } } } return(fileName); } } catch (Exception ex) { ServiceRegistration.Get <ILogger>().Warn("Error downloading image for feed or item '{0}': {1}", url.ToString(), ex); return(string.Empty); } }
static string GetCachedOrDownloadImage(Uri url) { if (url == null) return string.Empty; try { using (var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider()) { string iconUrlHash = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(url.ToString()))); string thumbsPath = ServiceRegistration.Get<IPathManager>().GetPath(@"<DATA>\News\Thumbs"); if (!System.IO.Directory.Exists(thumbsPath)) System.IO.Directory.CreateDirectory(thumbsPath); string originalExtension = System.IO.Path.GetExtension(url.LocalPath); string extension = originalExtension.ToLower() == ".gif" ? ".png" : originalExtension; string fileName = System.IO.Path.Combine(thumbsPath, iconUrlHash) + extension; if (!System.IO.File.Exists(fileName)) { using (var client = new CompressionWebClient()) { if (extension == originalExtension) { client.DownloadFile(url, fileName); } else { using (var imageData = client.OpenRead(url)) { System.Drawing.Image image = System.Drawing.Image.FromStream(imageData, true, true); image.Save(fileName, System.Drawing.Imaging.ImageFormat.Png); } } } } return fileName; } } catch (Exception ex) { ServiceRegistration.Get<ILogger>().Warn("Error downloading image for feed or item '{0}': {1}", url.ToString(), ex); return string.Empty; } }