
Code
<%@ Page Language="C#" AutoEventWireup="true" Inherits="Kirin.Private.Page" %>
<%@ Import Namespace="System.Drawing" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)

{
string target = Request.QueryString["action"];
string code = string.Empty;

Ui.CancelCache();

code = Ui.GetVerifyNum(4);


if (target == "AdminLoginVerify" || target == "VoneVerify" || target == "ShenzhouVerify" || target == "YeepayVerify" || target == "NumberVerify")

{
CreateVerifyImg(target, code);
}
}

/**//// <summary>
/// 生成验证码图片
/// </summary>
/// <param name="sTarget">应用目标</param>
private void CreateVerifyImg(string sTarget, string checkCode)

{
if (string.IsNullOrEmpty(checkCode))
return;
Session[sTarget] = checkCode.Replace(" ", ""); //将字符串保存到Session中,以便需要时进行验证\'
System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(Request.MapPath("bg.jpg"));
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;
System.Drawing.Bitmap image = new System.Drawing.Bitmap(phWidth, phHeight, System.Drawing.Imaging.PixelFormat.Format64bppArgb);
image.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);


Graphics g = Graphics.FromImage(image);
try

{
//随机种子
Random random = new Random();

//图片背景
g.Clear(Color.WhiteSmoke);

//画图片的背景噪音线
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawImage(
imgPhoto, // Photo Image object
new Rectangle(0, 0, phWidth, phHeight), // Rectangle structure
0, // x-coordinate of the portion of the source image to draw.
0, // y-coordinate of the portion of the source image to draw.
phWidth, // Width of the portion of the source image to draw.
phHeight, // Height of the portion of the source image to draw.
GraphicsUnit.Pixel); // Units of measure


System.Drawing.FontStyle newFontStyle = FontStyle.Bold | FontStyle.Italic;
Font font = new System.Drawing.Font("黑体", random.Next(20, 33), newFontStyle);
Font font2 = new System.Drawing.Font("黑体", random.Next(20, 33), newFontStyle);
Font font3 = new System.Drawing.Font("黑体", random.Next(20, 33), newFontStyle);
Font font4 = new System.Drawing.Font("黑体", random.Next(20, 33), newFontStyle);
int i, j, k;
Color color = Color.FromArgb(i = random.Next(0, 255), j = random.Next(0, 255), k = random.Next(0, 255));
string[] arr = checkCode.Split(\' \');

System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), color, Color.Orchid, 90, true);
g.DrawString(arr[0], font, brush, 2, 2);
g.DrawString(arr[1], font2, brush, 26, 6);
g.DrawString(arr[2], font3, brush, 48, 2);
g.DrawString(arr[3], font4, brush, 72, 6);


//画图片的前景噪音点
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/jpeg";
Response.BinaryWrite(ms.ToArray());
}
catch

{
g.Dispose();
image.Dispose();
}
}
</script>
相关文章: