using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Security.Cryptography;
using System.Net;
using TradeIdeas.XML;
namespace TradeIdeas.TIProGUI
{
///
/// This class is for handling Company Logos.
///
public static class CompanyLogoManager
{
private static string _companyLogosWebResource = "https://static.trade-ideas.com/CompanyLogos/";
///
/// Loads image of the company's logo
///
public static void LoadCompanyLogo(PictureBox pictureBox, string symbol)
{
_companyLogosWebResource = GuiEnvironment.GlobalSettings.Node("SINGLE_STOCK_WINDOW").Node("SINGLE_STOCK_WINDOW_COMPANYLOGOS").Property("SOURCE", _companyLogosWebResource);
var imageUrl = $"{_companyLogosWebResource}{WebUtility.UrlEncode(symbol)}.png";
pictureBox.Image = null;
pictureBox.LoadAsync(@imageUrl);
}
///
/// Create a color from the symbol.
///
///
///
public static Color ColorFromSymbol(string symbol)
{
var md5 = MD5.Create();
var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(symbol));
return Color.FromArgb(hash[0], hash[1], hash[2]);
}
///
/// Symbol PictureBox control LoadCompleted event handler.
/// This event handler will make the control a solid color based on the symbol if a logo isn't found.
///
///
///
public static void symbolPictureBox_LoadCompleted(object sender, AsyncCompletedEventArgs e)
{
PictureBox pictureBox = sender as PictureBox;
if (pictureBox != null)
{
if (e.Error != null || e.Cancelled)
pictureBox.Image = null;
if (null != pictureBox.Image)
{
// Resize image to fit PictureBox.
pictureBox.Image = CollaborateForm.resizeImage((Image)pictureBox.Image.Clone(), new Size(pictureBox.Width, pictureBox.Height));
}
else
{
// Symbol image was not found, so use a solid color instead.
pictureBox.BackColor = CompanyLogoManager.ColorFromSymbol(pictureBox.Tag.ToString());
}
// Make logo image visible.
pictureBox.Visible = true;
}
}
}
}