コード例 #1
0
        /// <summary>
        /// Return image for specific Id and Hex Color
        /// </summary>
        /// <param name="symbolId">Symbol Id</param>
        /// <param name="hexColor">Symbol color as a hex string</param>
        /// <returns>Image as a byte array</returns>
        public static byte[] GetSymbolImage(long symbolId, string hexColor = "")
        {
            var connectionString = ConnectionManager.DefaultInstance.ConnectionString;
            var symbolSet        = new SymbolSet(connectionString);
            var ms  = new MemoryStream();
            var img = symbolSet.GetImage(symbolId);

            if (img == null)
            {
                img = new Bitmap(16, 16);
                //((Bitmap)img).MakeTransparent(Color.White);
                var g = Graphics.FromImage(img);
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.Clear(Color.Transparent);
                Color color;
                try
                {
                    color = hexColor == "" ? Color.Green : ColorTranslator.FromHtml(hexColor);
                }
                catch (Exception ex)
                {
                    color = Color.Green;
                    Trace.WriteLine(ex);
                }

                var rect = new Rectangle(0, 0, 15, 15);
                switch (symbolId)
                {
                case 0:
                    g.FillEllipse(new SolidBrush(color), rect);
                    g.DrawEllipse(new Pen(Color.Black, 1), rect);
                    break;

                case 1:
                    var pntArrey = new PointF[3];
                    pntArrey[0] = new PointF(0, 14);
                    pntArrey[1] = new PointF(7f, 0);
                    pntArrey[2] = new PointF(14, 14);
                    g.FillPolygon(new SolidBrush(color), pntArrey);
                    g.DrawPolygon(new Pen(Color.Black, 1), pntArrey);
                    break;

                default:
                    g.FillRectangle(new SolidBrush(color), rect);
                    g.DrawRectangle(new Pen(Color.Black, 1), rect);
                    break;
                }
                g.Dispose();
            }
            //img.Save(@"C:/test.png", ImageFormat.Png);
            img.Save(ms, ImageFormat.Png);

            symbolSet.Dispose();

            return(ms.ToArray());
        }