VS2005/2008出错判断-生成验证码

1,

---------------------------
Microsoft Visual Studio
---------------------------
无法打开项目文件“C:/Documents and Settings/Administrator/Local Settings/Temp/5plaia1i.o42/Temp/WpfApplication1.csproj”。

此安装不支持该项目类型。
---------------------------
确定   帮助  
---------------------------

 

2,

c#设计视图不能用

 

 

3,

在创建一个silverlight application时,总是报错:
无法打开项目文件“c:/Documents and settings /Administrator/localsettings/Temp/silverlightApplication.csproj”
此安装不支持该项目类型

 

上面三个问题的解决办法:

开始菜单–>Microsoft Visual Studio 2008–>Visual Studio Tools->Visual Studio 2008 Command Prompt(命令提示符)
运行: devenv /ResetSkipPkgs

 

 

--------------------------------

引用命名空间 using System.Drawing;
产生随机字符串
    public string MakeValidateCode()
    {
        char[] s = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };//枚举数组
        string num = "";
        Random r = new Random();
        for (int i = 0; i < 5; i++)
        {
            num += s[r.Next(0, s.Length)].ToString();
        }
        return num;
    }

产生图片

    private void CreateCheckCodeImage(string checkCode)
    { //将验证码生成图片显示
        if (checkCode == null || checkCode.Trim() == String.Empty)
            return;
        System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
        Graphics g = Graphics.FromImage(image);
        //生成随机生成器
        Random random = new Random();
        //清空图片背景色
        g.Clear(Color.White);
        //画图片的背景噪音线
        for (int i = 0; i < 25; 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.Silver), x1, y1, x2, y2);
        }
        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.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);
        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());
    }

用这两个函数放在一个GetValCode.aspx里面,用一个session["curValCode"]保存随机字符串:

    protected void Page_Load(object sender, EventArgs e)
    {
        string valCode = MakeValidateCode();
        Session["curValCode"] = valCode;
        CreateCheckCodeImage(valCode);
    }

Register.aspx调用

在Register.aspx里用session["curValCode"]来检查

 

 


 

你可能感兴趣的:(VS2005/2008出错判断-生成验证码)