>

简单易行算术验证码

- 编辑:金沙国际平台登录 -

简单易行算术验证码

   /// <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
{
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;
Graphics graphicImage = Graphics.FromImage(bitMapImage);
graphicImage.FillRectangle(new SolidBrush(Color.White), 0, 0, 60, 25);

//设置画笔的输出情势
graphicImage.SmoothingMode = SmoothingMode.HighSpeed;

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

//画图片的前景噪音点
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, randomPixel.Next, randomPixel.Next;
}

//设置图像输出的格式
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[0];
var secondNum = GetNumber[1];
var resultNum = GetResult(firstNum, secondNum, oper);
strCode = firstNum.ToString() + oper + secondNum.ToString() + "=";
CreateImg(strCode, context);
}
catch (Exception)
{
throw;
}
}

本文由编程发布,转载请注明来源:简单易行算术验证码