Esempio n. 1
0
 /// <summary>
 /// Initialises a new instance of <see cref="DisplayIconAttribute"/>
 /// </summary>
 /// <param name="faviconIcon"></param>
 public DisplayIconAttribute(FontIcons faviconIcon)
 {
     this.Icon = faviconIcon.ToString();
 }
Esempio n. 2
0
        /// <summary>
        /// 获取图标.
        /// </summary>
        /// <param name="iconText">图标名称.</param>
        /// <param name="imageSize">图标大小.</param>
        /// <param name="foreColor">前景色</param>
        /// <param name="backColor">背景色.</param>
        /// <returns>Bitmap.</returns>
        /// <exception cref="FileNotFoundException">Font file not found</exception>
        public static Bitmap GetImage(FontIcons iconText, int imageSize = 32, Color?foreColor = null, Color?backColor = null)
        {
            Dictionary <string, Font> _fs;

            if (iconText.ToString().StartsWith("A_"))
            {
                _fs = m_fontsAwesome;
            }
            else
            {
                if (m_fontsElegant.Count <= 0)
                {
                    lock (m_fontsElegant)
                    {
                        if (m_fontsElegant.Count <= 0)
                        {
                            string strPath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase.ToLower().Replace("file:///", "");
                            string strDir  = System.IO.Path.GetDirectoryName(strPath);
                            if (!Directory.Exists(Path.Combine(strDir, "IconFont")))
                            {
                                Directory.CreateDirectory(Path.Combine(strDir, "IconFont"));
                            }
                            string strFile = Path.Combine(strDir, "IconFont\\ElegantIcons.ttf");
                            if (!File.Exists(strFile))
                            {
                                var        fs = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("HZH_Controls.IconFont.ElegantIcons.ttf");
                                FileStream sw = new FileStream(strFile, FileMode.Create, FileAccess.Write);
                                fs.CopyTo(sw);
                                sw.Close();
                                fs.Close();
                            }
                            m_fontCollection.AddFontFile(strFile);

                            float size = MinFontSize;
                            for (int i = 0; i <= (MaxFontSize - MinFontSize) * 2; i++)
                            {
                                m_fontsElegant.Add(size.ToString("F2"), new Font(m_fontCollection.Families[0], size, FontStyle.Regular, GraphicsUnit.Point));
                                size += 0.5f;
                            }
                        }
                    }
                }
                _fs = m_fontsElegant;
            }

            if (!foreColor.HasValue)
            {
                foreColor = Color.Black;
            }
            Font  imageFont = _fs[MinFontSize.ToString("F2")];
            SizeF textSize  = new SizeF(imageSize, imageSize);

            using (Bitmap bitmap = new Bitmap(48, 48))
                using (Graphics graphics = Graphics.FromImage(bitmap))
                {
                    //float size = MaxFontSize;
                    float fltMaxSize = MaxFontSize;
                    if (m_cacheMaxSize.ContainsKey(imageSize))
                    {
                        fltMaxSize = Math.Max(MaxFontSize, m_cacheMaxSize[imageSize] + 5);
                    }
                    while (fltMaxSize >= MinFontSize)
                    {
                        Font  font = _fs[fltMaxSize.ToString("F2")];
                        SizeF sf   = GetIconSize(iconText, graphics, font);
                        if (sf.Width < imageSize && sf.Height < imageSize)
                        {
                            imageFont = font;
                            textSize  = sf;
                            break;
                        }

                        fltMaxSize -= 0.5f;
                    }

                    if (!m_cacheMaxSize.ContainsKey(imageSize) || (m_cacheMaxSize.ContainsKey(imageSize) && m_cacheMaxSize[imageSize] < fltMaxSize))
                    {
                        m_cacheMaxSize[imageSize] = fltMaxSize;
                    }
                }

            Bitmap srcImage = new Bitmap(imageSize, imageSize);

            using (Graphics graphics = Graphics.FromImage(srcImage))
            {
                if (backColor.HasValue && backColor.Value != Color.Empty && backColor.Value != Color.Transparent)
                {
                    graphics.Clear(backColor.Value);
                }
                string s = char.ConvertFromUtf32((int)iconText);
                graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
                graphics.SetGDIHigh();
                using (Brush brush2 = new SolidBrush(foreColor.Value))
                {
                    graphics.DrawString(s, imageFont, brush2, new PointF((imageSize - textSize.Width) / 2.0f + 1, (imageSize - textSize.Height) / 2.0f + 1));
                }
            }

            return(srcImage);
        }