先发几个图片,看一下直观的效果。

 

 

这个源码,大多是网上的一个兄弟写的。俺只是整理,稍微修改了一下,更通用而已。再次对他表示感谢。

 

源码如下:

......

using System.Drawing.Drawing2D;

......

 private void MakeImg(string text, int width, int height, Color color, string outPath)
        {
            FontFamily fontFamily = new FontFamily("文鼎特粗黑简");
            GraphicsPath path = new GraphicsPath();

            path.AddString(text, fontFamily, (int)FontStyle.Regular, 47, new Point(-9, 0), new StringFormat());  //字体大小为60 ,根据要生成图片的高度而定

            PointF[] dataPoints = path.PathPoints;  //获取路径的点信息

            byte[] pTypes = path.PathTypes;    //获取路径的点类型信息

            double widthpara = (double)width /( (height + 0.5) * text.Length);    //计算宽度参数
            Matrix matrix = new Matrix((float)widthpara, 0.0f, 0.0f, 1f, 0.0f, 0.0f);  //将文本在水平方向上缩小,在垂直方向上不变

            matrix.TransformPoints(dataPoints); //对points数据中的每一个成员进行矩形运算

            GraphicsPath newpath = new GraphicsPath(dataPoints, pTypes); //根据计算后的点重新构造路径

            Bitmap bmap = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bmap);
            g.Clear(Color.White);   //设置图片的背景
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;   //设置文本输出质量
            g.SmoothingMode = SmoothingMode.AntiAlias;
            SolidBrush redBrush = new SolidBrush(color);

            g.FillPath(redBrush, newpath);  //填充路径
            bmap.Save(outPath, System.Drawing.Imaging.ImageFormat.Png);
            g.Dispose();
            redBrush.Dispose();
        }

 

生成的时候,调用此函数即可。

 

注:您可以在这个网站看到效果(http://www.cetfj.com/

posted on 2008-07-26 17:09  不做懒人  阅读(792)  评论(1编辑  收藏  举报