注册 登录
编程论坛 ASP.NET技术论坛

关于生成图片验证码

golmjie 发布于 2010-09-08 18:24, 714 次点击
大家帮我看看是哪里不对了?我对着教程一步一步写的代码一样就是不出效果。
程序代码:
using System.Drawing;                //引用命名空间
using System.Drawing.Imaging;



protected void Page_Load(object sender, EventArgs e)
    {
        string yzm = Ran();
        Response.Write(yzm);

        Session["yzm"] = yzm;  //随机数写入
        int width = 70;     //图片的长度
        int heith = 20;     //图片的高度
        string fontcolor = "White";  //字体颜色
        float fontsize = 12;    //字体的大小
        string font = "宋体";   //字体

        Bitmap img = new Bitmap(width, heith);  //一个图片框
        Graphics g = Graphics.FromImage(img);   //把图片装截入
        g.DrawString(yzm, new Font(font, fontsize), new SolidBrush(Color.FromName(fontcolor)), 10, 2);
        img.Save(Response.OutputStream, ImageFormat.Jpeg);

    }

    private static string Ran()
    {
        string RanString = "123456789";
        int i = 0;
        string a = "";
        Random Ran = new Random();
        while (i <= 3)
        {
            a += Ran.Next(RanString.Length);
            i++;
        }
        return a;


    }

8 回复
#2
红色警戒2010-09-08 20:38
你也没说明到底是个什么问题,看看这个比较一下吧
程序代码:
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
        Graphics g = Graphics.FromImage(image);

        try
        {
            //生成随机生成器
            Random random = new Random();

            //清空图片背景色
            g.Clear(Color.White);

            //画图片的背景噪音线
            for (int i = 0; i < 2; i++)
            {
                int x1 = random.Next(image.Width);
                int x2 = random.Next(image.Width);
                int y1 = random.Next(image.Height);
                int y2 = random.Next(image.Height);

                g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
            }

            Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
            g.DrawString(checkCode, font, brush, 2, 2);

            //画图片的前景噪音点
            for (int i = 0; i < 100; i++)
            {
                int x = random.Next(image.Width);
                int y = random.Next(image.Height);

                image.SetPixel(x, y, Color.FromArgb(random.Next()));
            }

            //画图片的边框线
            g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

             ms = new ();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            Response.ClearContent();
            Response.ContentType = "image/Gif";
            Response.BinaryWrite(ms.ToArray());
        }
        finally
        {
            g.Dispose();
            image.Dispose();
        }


[ 本帖最后由 红色警戒 于 2010-9-8 20:40 编辑 ]
#3
golmjie2010-09-08 21:13
回复 2楼 红色警戒
就是生不了图片。出现乱码!
我试了下你的代码。说
checkCode
上下文不存在名称
#4
红色警戒2010-09-08 21:23
用个sting类型的字符串代替就可
#5
yms1232010-09-08 22:13
LZ代码调用是怎么写的?
#6
golmjie2010-09-08 23:22
回复 5楼 yms123
<img src="文件名" />
我是这样用这个文件的就是生成不了图片!
#7
golmjie2010-09-08 23:33
回复 4楼 红色警戒
恩!加上了个随机数可以了!可是你的那个复杂哈。。我看不懂哈...
#8
misswang2010-09-09 16:30
#9
maple9902010-09-11 10:38
,,
1