首页 > 其他分享 >WEB Service产生随机验证码图片

WEB Service产生随机验证码图片

时间:2022-12-19 17:32:27浏览次数:45  
标签:WEB Service int 验证码 System rd new Next Drawing


 

WEB服务端方法:

[WebMethod]
public byte[] GenerateVerifyImage(int nLen, ref string strKey)
{
int nBmpWidth = 13 * nLen + 5;
int nBmpHeight = 25;
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(nBmpWidth, nBmpHeight);

int nRed, nGreen, nBlue; // 背景的三元色
System.Random rd = new Random((int)System.DateTime.Now.Ticks);
nRed = rd.Next(255) % 128 + 128;
nGreen = rd.Next(255) % 128 + 128;
nBlue = rd.Next(255) % 128 + 128;
System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp);
graph.FillRectangle(new SolidBrush(System.Drawing.Color.FromArgb(nRed, nGreen, nBlue))
, 0
, 0
, nBmpWidth
, nBmpHeight);
int nLines = 3;
System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(nRed - 17, nGreen - 17, nBlue - 17), 2);
for (int a = 0; a < nLines; a++)
{
int x1 = rd.Next() % nBmpWidth;
int y1 = rd.Next() % nBmpHeight;
int x2 = rd.Next() % nBmpWidth;
int y2 = rd.Next() % nBmpHeight;
graph.DrawLine(pen, x1, y1, x2, y2);
}
string strCode = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string strResult = "";
for (int i = 0; i < nLen; i++)
{
int x = (i * 13 + rd.Next(3));
int y = rd.Next(4) + 1;
System.Drawing.Font font = new System.Drawing.Font("Courier New",
12 + rd.Next() % 4,
System.Drawing.FontStyle.Bold);
char c = strCode[rd.Next(strCode.Length)]; // 随机获取字符
strResult += c.ToString();
graph.DrawString(c.ToString(),
font,
new SolidBrush(System.Drawing.Color.FromArgb(nRed - 60 + y * 3, nGreen - 60 + y * 3, nBlue - 40 + y * 3)),
x,
y);
}
System.IO.MemoryStream bstream = new System.IO.MemoryStream();
bmp.Save(bstream, System.Drawing.Imaging.ImageFormat.Jpeg);
bmp.Dispose();
graph.Dispose();

strKey = strResult;
byte[] byteReturn = bstream.ToArray();
bstream.Close();

return byteReturn;
}

客户端应用:

 

string code1 = GenerateCheckCode();//产生随机字符串方法如下:
ValidateCodeService.ValidateCodeWebServiceSoapClient vws = new ValidateCodeService.ValidateCodeWebServiceSoapClient();
byte[] data = vws.enValidateByte(code1);
MemoryStream ms = new MemoryStream(data);
Image img = Image.FromStream(ms);//将字符流转化为图片,用于显示在picturebox控件中;
pictureBox2.Image = img;


private string GenerateCheckCode() 
{
int number;
char code;
string checkCode = String.Empty;
Random random = new Random();
for (int i = 0; i < 4; i++)
{ number = random.Next();
if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26));
checkCode += code.ToString();
}

return checkCode;
}

 

 

 

 

 

标签:WEB,Service,int,验证码,System,rd,new,Next,Drawing
From: https://blog.51cto.com/u_15917617/5953265

相关文章

  • ReactNative(三)——WebStorm的基本配置
    设置.js文件默认以jsx的语法打开在没有进行设置的情况下,每次打开WebStorm的时候打开包含jsx语法的.js文件都会有以下提示:当然我们点击转换后就可以了,但是每次都会提示,所以还......
  • 实验八-web部署
    实验目的在openEuler中基于LAMP部署WordPress相关知识LAMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写:Linux,操作系统,openEuler就是一种Li......
  • web.xml配置
    <?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocatio......
  • web前端经典react面试题
    redux有什么缺点一个组件所需要的数据,必须由父组件传过来,而不能像flux中直接从store取。当一个组件相关数据更新时,即使父组件不需要用到这个组件,父组件还是会重新render......
  • webservice简介&CXF入门
    WebService它是一种跨编程语言和跨操作系统平台的远程调用技术即跨平台远程调用技术. 面向服务架构规范及三要素 1)           SOAP协议:webservice的传输协议,......
  • 图片格式转换webp
    今天突然保存梅西夺冠照片,发现google上下载的图片是webp格式,好新颖的格式。百度了下,webp是google推出的必jpg格式更小的图片格式文件,但是softpage等浏览器还不支持,WIN11设......
  • 验证码是自动化的天敌?看看大神是怎么解决的
     每天进步一点点,关注我们哦,每天分享测试技术文章本文章出自【码同学软件测试】码同学公众号:自动化软件测试,领取资料可加:magetest码同学抖音号:小码哥聊软件测试01验......
  • ASP.NET 实现验证码以及刷新验证码的小例子
    原文链接:https://www.jb51.net/article/42219.htm实现代码///<summary>   ///生成验证码图片,保存session名称VerificationCode   ///</summary>   pu......
  • ASP.NET验证码实现代码(C#)
    原文链接:https://www.jb51.net/article/13634.htm1、创建空白页面CheckCode.aspxpublicpartialclassCheckCode:System.Web.UI.Page{privatevoidPage_Load(......
  • selenium webdriver三种浏览器如何处理不受信任的证书
    Hello,Welcometo ​​Seleniumtutorial​​ inthispostwewillseehowto HandleUntrustedCertificateSelenium. WhatisUntrustedSSLcertificate? Whene......