private void LoadTieredClientIcons(WebIconList iconsList, string localPath) { WebIcon defaultTierIcon = (from icon in iconsList.Icons where !string.IsNullOrEmpty(icon.ClientID) && icon.Tier == 1 select icon).FirstOrDefault(); if (!object.ReferenceEquals(defaultTierIcon, null)) { try { m_defaultTierIcon = LoadImageFromFile(Path.Combine(localPath, defaultTierIcon.LocalName)); } catch { // TODO: Log the error or report it. } } var tieredClientIcons = from icon in iconsList.Icons where !string.IsNullOrEmpty(icon.ClientID) && icon.Tier > 1 select icon; foreach (var tcIcon in tieredClientIcons) { // for tiered icons, assume each icon can only have one client. Image currentImage = null; try { currentImage = LoadImageFromFile(Path.Combine(localPath, tcIcon.LocalName)); } catch { // TODO: Log the error or report it. continue; } if (!m_tieredClientImages.ContainsKey(tcIcon.ClientID)) m_tieredClientImages.Add(tcIcon.ClientID, new Dictionary<int, Dictionary<char, Image>>()); Dictionary<int, Dictionary<char, Image>> tierList = m_tieredClientImages[tcIcon.ClientID]; if (!tierList.ContainsKey(tcIcon.Tier)) tierList.Add(tcIcon.Tier, new Dictionary<char, Image>()); Dictionary<char, Image> raceList = tierList[tcIcon.Tier]; raceList.Add(tcIcon.Race, currentImage); } }
private void DownloadFile(Icon icon) { string localPath = Path.Combine(JinxBotConfiguration.ApplicationDataPath, "Icons"); if (!Directory.Exists(localPath)) { Directory.CreateDirectory(localPath); } string targetDirectory = localPath; string temporaryPath = Path.GetTempFileName(); localPath = Path.Combine(localPath, icon.LocalName); WebClient client = new WebClient(); client.DownloadFile(icon.Uri, temporaryPath); if (icon.LocalAnimationName != null) { string animationLocalPath = Path.Combine(targetDirectory, icon.LocalAnimationName); client.DownloadFile(icon.AnimationUri, animationLocalPath); } using (Image source = Image.FromFile(temporaryPath)) using (Image target = new Bitmap(64, 44, PixelFormat.Format32bppArgb)) using (Graphics g = Graphics.FromImage(target)) using (Brush back = new SolidBrush(Color.Black)) { g.FillRectangle(back, new Rectangle(Point.Empty, target.Size)); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; if (icon.Crop) { Rectangle sourceRect = new Rectangle(0, icon.Top, 64, 64 - icon.Bottom - icon.Top); Rectangle destinationRectangle = new Rectangle(0, (44 - sourceRect.Height) / 2, 64, sourceRect.Height); g.DrawImage(source, destinationRectangle, sourceRect, GraphicsUnit.Pixel); } else { int requestedWidth = 64 * source.Height / 44; // should be 55; if not, we'll need to do complex scaling Rectangle sourceRect = new Rectangle(Point.Empty, source.Size); Rectangle destRect; if (requestedWidth == source.Width) { // simple scaling destRect = new Rectangle(Point.Empty, target.Size); } else { // first try to scale at 64 pixels wide. int resultHeight = 64 * source.Height / source.Width; if (resultHeight <= 44) { destRect = new Rectangle(0, (44 - resultHeight) / 2, 64, resultHeight); } else { // image is taller than destination proportions allow; // scale by 44 tall then int resultWidth = source.Width * 44 / source.Height; destRect = new Rectangle(0, 0, resultWidth, 44); } } g.DrawImage(source, destRect, sourceRect, GraphicsUnit.Pixel); } target.Save(localPath, ImageFormat.Png); } File.Delete(temporaryPath); }
private void DownloadFile(Icon icon) { string localPath = Path.Combine(JinxBotConfiguration.ApplicationDataPath, "Icons"); if (!Directory.Exists(localPath)) Directory.CreateDirectory(localPath); string targetDirectory = localPath; string temporaryPath = Path.GetTempFileName(); localPath = Path.Combine(localPath, icon.LocalName); WebClient client = new WebClient(); client.DownloadFile(icon.Uri, temporaryPath); if (icon.LocalAnimationName != null) { string animationLocalPath = Path.Combine(targetDirectory, icon.LocalAnimationName); client.DownloadFile(icon.AnimationUri, animationLocalPath); } using (Image source = Image.FromFile(temporaryPath)) using (Image target = new Bitmap(64, 44, PixelFormat.Format32bppArgb)) using (Graphics g = Graphics.FromImage(target)) using (Brush back = new SolidBrush(Color.Black)) { g.FillRectangle(back, new Rectangle(Point.Empty, target.Size)); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; if (icon.Crop) { Rectangle sourceRect = new Rectangle(0, icon.Top, 64, 64 - icon.Bottom - icon.Top); Rectangle destinationRectangle = new Rectangle(0, (44 - sourceRect.Height) / 2, 64, sourceRect.Height); g.DrawImage(source, destinationRectangle, sourceRect, GraphicsUnit.Pixel); } else { int requestedWidth = 64 * source.Height / 44; // should be 55; if not, we'll need to do complex scaling Rectangle sourceRect = new Rectangle(Point.Empty, source.Size); Rectangle destRect; if (requestedWidth == source.Width) { // simple scaling destRect = new Rectangle(Point.Empty, target.Size); } else { // first try to scale at 64 pixels wide. int resultHeight = 64 * source.Height / source.Width; if (resultHeight <= 44) { destRect = new Rectangle(0, (44 - resultHeight) / 2, 64, resultHeight); } else { // image is taller than destination proportions allow; // scale by 44 tall then int resultWidth = source.Width * 44 / source.Height; destRect = new Rectangle(0, 0, resultWidth, 44); } } g.DrawImage(source, destRect, sourceRect, GraphicsUnit.Pixel); } target.Save(localPath, ImageFormat.Png); } File.Delete(temporaryPath); }