简单算术验证码_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 简单算术验证码

简单算术验证码

 2015/3/12 12:58:17  孤狼灬  程序员俱乐部  我要评论(0)
  • 摘要:///<summary>///随机获取运算符///</summary>///<returns></returns>privatestringGetOperator(){stringOperatorArray="+,-,×,÷";String[]VcArray=OperatorArray.Split(',');Randomrandom=newRandom();intiNum=random.Next(VcArray
  • 标签:

    /// <summary>
        /// 随机获取运算符
        /// </summary>
        /// <returns></returns>
        private string GetOperator()
        {
            string OperatorArray = "+,-,×,÷";
            String[] VcArray = OperatorArray.Split(',');
            Random random = new Random();
            int iNum = random.Next(VcArray.Length);
            return VcArray[iNum];
        }

 

  /// <summary>
        /// 随机获取数并添加到数组
        /// </summary>
        /// <returns></returns>
        private int[] GetNumber(string oper)
        {
            int[] numList = new int[2];
            string OperatorArray = "1,2,3,4,5,6,7,8,9,10,12,15,18,20";
            String[] VcArray = OperatorArray.Split(',');
            Random random = new Random();
            int iNum = random.Next(VcArray.Length);
            int iNum1 = random.Next(VcArray.Length);
            if (oper == "-" || oper == "÷")
            {
                numList[0] = Convert.ToInt32(VcArray[iNum]) >= Convert.ToInt32(VcArray[iNum1]) ? Convert.ToInt32(VcArray[iNum]) : Convert.ToInt32(VcArray[iNum1]);
                numList[1] = Convert.ToInt32(VcArray[iNum]) < Convert.ToInt32(VcArray[iNum1]) ? Convert.ToInt32(VcArray[iNum]) : Convert.ToInt32(VcArray[iNum1]);
                if (oper == "÷")
                {
                    //必须整除
                    while (numList[0] % numList[1] > 0)
                    {
                        iNum = random.Next(VcArray.Length);
                        iNum1 = random.Next(VcArray.Length);
                        numList[0] = Convert.ToInt32(VcArray[iNum]) >= Convert.ToInt32(VcArray[iNum1]) ? Convert.ToInt32(VcArray[iNum]) : Convert.ToInt32(VcArray[iNum1]);
                        numList[1] = Convert.ToInt32(VcArray[iNum]) < Convert.ToInt32(VcArray[iNum1]) ? Convert.ToInt32(VcArray[iNum]) : Convert.ToInt32(VcArray[iNum1]);
                    }
                }
            }
            else
            {
                numList[0] = Convert.ToInt32(VcArray[iNum]);
                numList[1] = Convert.ToInt32(VcArray[iNum1]);
            }
            return numList;
        }

 

/// <summary>
        /// 获取运算结果
        /// </summary>
        /// <param name="firstNum"></param>
        /// <param name="secondNum"></param>
        /// <param name="oper"></param>
        /// <returns></returns>
        private int GetResult(int firstNum, int secondNum, string oper)
        {
            int result = 0;
            switch (oper)
            {
                case "+":
                    result = firstNum + secondNum;
                    break;
                case "-":
                    result = firstNum - secondNum;
                    break;
                case "×":
                    result = firstNum * secondNum;
                    break;
                case "÷":
                    result = firstNum / secondNum;
                    break;
            }
            return result;
        }

 

 /// <summary>
        /// 根据产生的随机数生成图象
        /// </summary>
        /// <param name="strCode"></param>
        private void CreateImg(string strCode, HttpContext context)
        {
            //也可以不用指定的图片,而是画一个背景图片
            Bitmap bitMapImage = new Bitmap(60, 25);
            Graphics graphicImage = Graphics.FromImage(bitMapImage);
            graphicImage.FillRectangle(new SolidBrush(Color.White), 0, 0, 60, 25);

            //设置画笔的输出模式
            graphicImage.SmoothingMode = SmoothingMode.HighSpeed;

            //添加文本字符串
            graphicImage.DrawString(strCode, new Font("Arial", 15, FontStyle.Italic), SystemBrushes.WindowText, new Point(0, 0));

            //画图片的前景噪音点
            Random randomPixel = new Random();
            for (int i = 0; i < 220; i++)
            {
                int x = randomPixel.Next(bitMapImage.Width);
                int y = randomPixel.Next(bitMapImage.Height);

                bitMapImage.SetPixel(x, y, Color.FromArgb(randomPixel.Next(0, 255), randomPixel.Next(0, 255), randomPixel.Next(0, 255)));
            }

            //设置图像输出的格式
            context.Response.ContentType = "image/jpeg";

            //保存数据流
            bitMapImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);

            //释放占用的资源
            graphicImage.Dispose();
            bitMapImage.Dispose();

        }

 

   /// <summary>
        /// 调用
        /// </summary>

 public void ProcessRequest(HttpContext context)
        {
            try
            {
                string strCode = string.Empty;
                var oper = GetOperator();
                var firstNum = GetNumber(oper)[0];
                var secondNum = GetNumber(oper)[1];
                var resultNum = GetResult(firstNum, secondNum, oper);
                strCode = firstNum.ToString() + oper + secondNum.ToString() + "=";
                CreateImg(strCode, context);
            }
            catch (Exception)
            {
                throw;
            }
        }

  • 相关文章
发表评论
用户名: 匿名