动态生成圈形+文字的图片


这个功能是因为要实现winform的自动根据字符串显示treeview节点图片

代码如下:

 private Bitmap TextToBitmap(string text)
        {
            Bitmap bmp = null;
            Graphics g;
            StringFormat format = new StringFormat(StringFormatFlags.NoClip);
//设置图片的宽和高 bmp
= new Bitmap(25, 25); g = Graphics.FromImage(bmp); //使用ClearType字体功能 g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
       //绘制的圆形要在图片范围内 g.DrawEllipse(
new Pen(Color.Red, 1), 1, 1, 20, 20); g.DrawString(text, new Font("Arial", 10), Brushes.Black, 2, 4f, format); return bmp; }
 private void Form1_Load(object sender, EventArgs e)
        {
            Bitmap bmp = TextToBitmap("");
            bmp.Save(@"[路径]\save.png", ImageFormat.Png);           
        }