C#事件的工作过程(工作原理)、标识符、验证码图片的生成代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
/// <summary>
}
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
{
number = random.Next();
code = (char)('0' + (char)(number % 10));
checkCode += code.ToString();
}
return checkCode;
}
//创建由随机数字生成的图片
public void CreateCheckCodeImage(string checkCode)
///创建验证码图片,该图片是由4位随机数字构成
/// </summary>
public class CheckCodeImage
{
private HttpResponse Response;
public CheckCodeImage(HttpResponse response)
{
this.Response = response;
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "imagBiblioteka Baidu/Gif";
Response.BinaryWrite(ms.ToArray());
}
C#事件的工作过程(工作原理):
1、定义一个继承于EventArgs类来在鼠标的点击处记录更多的你需要的信息(图1)。
2、在将要用的控件或组件类中声明一个委托,参数一定要定义好,它将会用到事件中去,输入、输出参数和想要定义的事件委托必须吻合(图2)。
3、在将要用的控件或组件类中利用声明的委托定义事件(图2)。
}
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed,1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
//画图片的前景噪音点
for (int i = 0; i < 88; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
下面是四位验证码图片的生成代码,至于其他的验证码图片的生成可以参照进行相应的修改来得到所需情况的代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
Graphics g = Graphics.FromImage(image);
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for (int i = 0; i < 44; i++)
{
int x1 = random.Next(image.Width - i);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
}
//生成随机数字组成的字符串
public string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
for (int i = 0; i < 4; i++)
4、在将要用的控件或组件类中利用方法调用事件(图3),这保证了委托的触发,这样就可以在使用控件、组件时添加此事件的具体实践代码,然后通过添加事件以得到调用(像平常中添加的其他事件一样)。
注:事件可以看作特殊的委托。
图1
图2
图3
标识符的概念:
标识符:是用户编程时使用的名字。我们指定某个东西、人,都要用到它,他或她的名字;在数学中解方程时,我们也常常用到这样或那样的变量名或函数名。同样的道理,在电脑语言中,对于变量,常量,函数,语句块也有名字,我们统统称之为标识符。我们在给人起名字时有一定的规矩,比如,头一个字为父亲或母亲的姓氏,后面一般为一个或两个字。所以,您可以想当然地认为电脑语言里的标识符也有一定的命名规则,如果您这样想,那您就想对了!
相关文档
最新文档