case:Str.Length
     
    Str.Font……

解决方案 »

  1.   

    using System;
    using System.Web;
    using System.IO;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Drawing.Drawing2D;namespace WebApplication1
    {
    /// <summary>
    /// ToImage 的摘要说明。
    /// </summary>
    public class ToImage
    {
    public ToImage()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
    //此函数的原型是实现网页计数器
    public void stringToImage(string Content,string Style)
    {
    /*
    Content  表示要显示的内容
    style  表示计数器样式 
    */
    string  strFontName; //字体名
    int intFontSize; //字体大小
    int intWidth; //图像宽
    int intHeight; //图像长
    //根据样式确定字体和大小
    switch(Style)
    {
    case "1":
    strFontName = "仿宋_GB2312";
    intFontSize = 8;
    intWidth = 40;
    intHeight = 20;
    break;
    case "2":
    strFontName = "宋体";
    intFontSize = 10;
    intWidth = 50;
    intHeight = 20;
    break;
    case "3":
    strFontName = "黑体";
    intFontSize = 12;
    intWidth = 60;
    intHeight = 25;
    break;
    case "4":
    strFontName = "楷体_GB2312";
    intFontSize = 100;
    intWidth = 280;
    intHeight = 30;
    break;
    default:
    strFontName = "新宋体";
    intFontSize = 10;
    intWidth = 50;
    intHeight = 20;
    break;
    }
    //背景颜色
    Color bgColor = Color.Yellow;
    //前景颜色
    Color foreColor = Color.Red; //字体产生
    Font foreFont = new Font(strFontName,intFontSize,FontStyle.Bold); /*
    从这里开始产生
    首先产生一个画布
    */
    Bitmap newBitmap = new Bitmap(intWidth,intHeight,PixelFormat.Format32bppArgb);
    Graphics g = Graphics.FromImage(newBitmap);
    Rectangle newRect = new Rectangle(0,0,intWidth,intHeight);
    //涂上背景色
    g.FillRectangle(new SolidBrush(bgColor),newRect);
    //写字
    g.DrawString(Content.ToString(),foreFont,new SolidBrush(foreColor),2,2);
    MemoryStream mStream = new MemoryStream();
    //存入MemoryStream
    newBitmap.Save(mStream,ImageFormat.Gif);
    g.Dispose();
    newBitmap.Dispose();
    //发送
    System.Web.HttpContext.Current.Response.ClearContent();
    System.Web.HttpContext.Current.Response.ContentType = "image/GIF";
    System.Web.HttpContext.Current.Response.BinaryWrite(mStream.ToArray());
    System.Web.HttpContext.Current.Response.End();
    }
    //
    public void textToImage(string text)
    {
    Color bgcolor=Color.Blue;
    Color forecolor=Color.Red;
    Font forefont=new Font("楷体_GB2312",23,FontStyle.Bold);
    Bitmap newbitmap=new Bitmap(200,400,PixelFormat.Format32bppArgb);
    Graphics g=Graphics.FromImage(newbitmap);
    Rectangle newrect=new Rectangle(0,0,200,400);
    g.FillRectangle(new SolidBrush(bgcolor),newrect);
    //绘制字符串图片
    g.DrawString(text.ToString(),forefont,new SolidBrush(forecolor),2,2);
    //画弧
    Pen newpen=new Pen(new SolidBrush(forecolor),5);
    g.DrawArc(newpen,newrect,180,270);
    //绘制贝赛尔样条
    g.DrawBezier(newpen,new Point(12,45),new Point(56,89),new Point(123,200),new Point(56,67));
    //绘制ico
    Icon newicon=new Icon(System.Web.HttpContext.Current.Server.MapPath("img/project.ico"));
    g.DrawIcon(newicon,new Rectangle(0,0,80,80));
    //绘制image
    // Image newimage=Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("imgs/097.GIF"));
    Image newimage=Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("img/project.ico"));
    g.DrawImage(newimage,new Rectangle(50,50,150,150),0,0,100,100,GraphicsUnit.Pixel);
    g.DrawImageUnscaled(newimage,100,100,200,200);
    //绘制一系列由直线、曲线等组成的图像路径
    GraphicsPath graphPath=new GraphicsPath();
    graphPath.AddEllipse(100,100,50,50);
    graphPath.AddLine(12,89,120,200);
    g.DrawPath(new Pen(new SolidBrush(Color.Black),3),graphPath);
    // //回放增强型图元文件
    // Metafile metafile=new Metafile(System.Web.HttpContext.Current.Server.MapPath("imgs/TN.emf"));
    // g.DrawImage(metafile,50,50);
    //将图片存入内存流
    MemoryStream ms=new MemoryStream();
    newbitmap.Save(ms,ImageFormat.Gif);
    //释放资源
    g.Dispose();
    newbitmap.Dispose();
    //显示图片
    System.Web.HttpContext.Current.Response.ClearContent();
    System.Web.HttpContext.Current.Response.ContentType = "image/GIF";
    System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
    System.Web.HttpContext.Current.Response.End();
    }
    }
    }
      

  2.   

    谢谢楼上的两位,但我觉得这种方法如果我允许user设置字体的画,太累了。我google了之后发现了graphics.MessureString和messureCharacter的方法。我想也许那样更好些。
    还是非常感谢两位。