当前位置导航:炫浪网>>网络学院>>编程开发>>Visual C#教程

C#创建简单的验证码操作

 首先,创建一个CLASS类,然后需要add Reference的方式添加 System.Drawing(画画的类)
    方法代码如下:
    /**//// <summary>
    /// 定义显示的随机字符
    /// </summary>
    /// <param name="strList"></param>
    /// <returns></returns>
    private string imageStr(char[] strList)
        ...{

            if (strList == null)
                strList = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();

            int codeLengh = 4;
            string radomCode = "";
            Random r = new Random();
            for (int i = 0; i < codeLengh;i++)
            ...{
                radomCode += strList[r.Next(strList.Length)];
            }
            return radomCode;
        }

    /**//// <summary>
    /// 创建随机验证字符的IMAGE,并保存,同时返回随机字符串
    /// </summary>
    /// <param name="iWidth">图片宽度 0时,默认为55</param>
    /// <param name="iHeight">图片高度 0时,默认为22</param>
    /// <param name="font">字符字体 null时,默认为 "Arial", 12, FontStyle.Bold</param>
    /// <param name="sb">字符颜色 null时,默认为红</param>
    /// <param name="ImagePath">需要保存的文件绝对路径</param>
    /// <param name="strList">随即字符库 null时,默认为0-9A-Z</param>
    /// <returns>返回随机字符串</returns>
        public string createImgWithStr(int iWidth,int iHeight,Font font,SolidBrush sb ,string ImagePath,char[] strList)
        ...{
            if (font == null)
                font = new Font("Arial", 12, FontStyle.Bold);
            if (sb == null)
                sb = new SolidBrush(Color.Red);
            if (iWidth == 0)
                iWidth = 55;
            if (iHeight == 0)
                iHeight = 22;
            //得到随机字符串
            string imageString = imageStr(strList);
            //定义横向竖向都画4跳线
            int lineCount = 4;
            这2支笔用来画线条的
            Pen pen1 = new Pen(Color.Gold, 1);
            Pen pen2 = new Pen(Color.Black, 2);
    //定义图片
            Bitmap image = new Bitmap(iWidth, iHeight);
            //跟J2ME一样的画笔
            Graphics g = Graphics.FromImage(image);
            //先画背景色 当然你可以自定义下
            g.Clear(ColorTranslator.FromHtml("#F0F0F0"));
            //确定写字的落点
            Rectangle rect = new Rectangle(5, 2, iWidth, iHeight);

            Random r = new Random();

            //默认随机画横向竖向4条线
            for(int i =0;i<lineCount;i++)
            ...{
                Point p1 = new Point(0, r.Next(iHeight));
                Point p2 = new Point(iWidth, r.Next(iHeight));
                Point p3 = new Point(r.Next(iWidth), 0);
                Point p4 = new Point(r.Next(iWidth), iHeight);
                g.DrawLine(pen1, p1, p2);
                g.DrawLine(pen2, p3, p4);
            }
            //写字
            g.DrawString(imageString, font, sb, rect); //删除源文件
            if (File.Exists(ImagePath))
                File.Delete(ImagePath);
            //保存文件,我定义为jpeg格式
            image.Save(ImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
            //释放资源
            g.Dispose();
            image.Dispose();

            return imageString;
        }
    另外,我在实际运用过程中总是发现重新生成了图片,但是显示却还是以前那张,

共2页 首页 上一页 1 2 下一页 尾页 跳转到
相关内容
赞助商链接