怎么把一个字符串画成图片?我定义了一个字符串!怎么把这个字符串画进一张图片?
请知道的举例回答下!小弟很菜,请给上注释哈!谢谢了!

解决方案 »

  1.   

            string checkCode = "test";
            System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
            Graphics g = Graphics.FromImage(image);
            //设置字体
            Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
            //设置背景
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.White, 1.2f, true);        g.DrawString(checkCode, font, brush, 2, 2);
            g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
            //画图片的边框线
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            Response.ClearContent();
            Response.ContentType = "image/Gif";
            Response.BinaryWrite(ms.ToArray());
    这代码直接从验证码的代码中抠出来的,用的话自己再改下吧
      

  2.   


            //设置要输出的字符串
            string checkCode = "test";
            //实例化图像对象,(int)Math.Ceiling((checkCode.Length * 12.5))这句是根据字符串计算图片长度的
            System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
            //封装一个 GDI+ 绘图图面
            Graphics g = Graphics.FromImage(image);
            //清空图片背景色
            g.Clear(Color.White);
            //设置字体(Arial字体,12号,加粗,倾斜)
            Font font = new System.Drawing.Font("Arial", 12);
            //设置字体颜色
            Brush brush = Brushes.Black;
            //画字符串
            g.DrawString(checkCode, font,brush,2,2);
            //画图片的边框线
            g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);
            //创建支持支持存储区为内存的流
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            //将图片保存到指定的流 ms
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            //清除缓冲区流中所有的内容输出
            Response.ClearContent();
            //设置输出流的HTTP MIME 类型
            Response.ContentType = "image/Gif";
            //将二进字符串写入HTTP输出流
            Response.BinaryWrite(ms.ToArray());记得在开始添加引用
    using System.Drawing;
      

  3.   

    感谢楼上的!我照这样用了有错误,错误提示如下!可以说下原因吗?
    另外如果我想把画出来的图显示在一个Image控件里应该如何做啊????请帮忙解答下!非常感谢!无法显示 XML 页。 
    使用 样式表无法查看 XML 输入。请更正错误然后单击 刷新按钮,或以后重试。 
    --------------------------------------------------------------------------------文档的顶层无效。处理资源 'http://localhost/website1/Default2.aspx' 时出错。第 1 行,位置: 1 GIF89a<
    ^