📄 test3.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
public partial class backup_test3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string codes = CreateCode(6);
CreateImage(codes);
Session["CheckCodes"] = codes;
}
private string CreateCode(int numbers)
{
string strs = "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[] chars = strs.Split(',');
Random ran = new Random();
string strCode = "";
for (int i = 0; i < numbers; i++)
{
int j = ran.Next(0, chars.Length);
strCode += chars[j];
}
return strCode;
}
private void CreateImage(string strCode)
{
Bitmap bp = new Bitmap(strCode.Length * 16, 28);
Graphics g = Graphics.FromImage(bp);
g.Clear(Color.White);
Pen silverPen = new Pen(Color.Silver);
//随机画25条背景线背景
Random ran = new Random();
for (int i = 0; i < 25; i++)
{
int x1 = ran.Next(bp.Width);
int y1 = ran.Next(bp.Height);
int x2 = ran.Next(bp.Width);
int y2 = ran.Next(bp.Height);
g.DrawLine(silverPen, x1, y1, x2, y2);
}
//画随机验证码
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, bp.Width, bp.Height), Color.Green, Color.Blue, 2);
Font font = new Font("Arial", 18, FontStyle.Bold | FontStyle.Italic);
g.DrawString(strCode, font, brush, 0, 0);
//设置100个点的颜色为银色
for (int i = 0; i < 100; i++)
{
int x = ran.Next(bp.Width);
int y = ran.Next(bp.Height);
bp.SetPixel(x, y, Color.Silver);
}
//为验证码添加一个银色的边框
g.DrawRectangle(silverPen, 0, 0, bp.Width - 1, bp.Height - 1);
//定义一个内存流
System.IO.MemoryStream ms = new System.IO.MemoryStream();
//将我们“画”的验证码图片存放在刚刚创建的内存流中 并指定图片保存的格式(后缀)
bp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
//清除缓冲区的内容
Response.ClearContent();
//设置输出流的类型
Response.ContentType = "image/gif";
//ms.ToArray()将内存流转换为二进制数组
Response.BinaryWrite(ms.ToArray());//这时候 图片是以流的形式存放(写入)在http输出流中
g.Dispose();
bp.Dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -