-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmycaptcha.aspx.cs
More file actions
44 lines (43 loc) · 2.36 KB
/
mycaptcha.aspx.cs
File metadata and controls
44 lines (43 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
public partial class mycaptcha : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Random random = new Random();
Bitmap bitmap = new Bitmap(150, 90);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.White);
graphics.DrawLine(Pens.Black, random.Next(0, 50), random.Next(10, 30), random.Next(0, 200), random.Next(0, 50));
graphics.DrawRectangle(Pens.Blue, random.Next(0, 20), random.Next(0, 20), random.Next(50, 80), random.Next(0, 20));
graphics.DrawLine(Pens.Blue, random.Next(0, 20), random.Next(10, 50), random.Next(100, 200), random.Next(0, 80));
Brush disignBrush = default(Brush);
//captcha background style
HatchStyle[] bkgStyle = new HatchStyle[]
{
HatchStyle.BackwardDiagonal, HatchStyle.Cross, HatchStyle.DashedDownwardDiagonal, HatchStyle.DashedHorizontal, HatchStyle.DashedUpwardDiagonal, HatchStyle.DashedVertical,
HatchStyle.DiagonalBrick, HatchStyle.DiagonalCross, HatchStyle.Divot, HatchStyle.DottedDiamond, HatchStyle.DottedGrid, HatchStyle.ForwardDiagonal, HatchStyle.Horizontal,
HatchStyle.HorizontalBrick, HatchStyle.LargeCheckerBoard, HatchStyle.LargeConfetti, HatchStyle.LargeGrid, HatchStyle.LightDownwardDiagonal, HatchStyle.LightHorizontal
};
//create captcha rectangular area
RectangleF rectagleArea = new RectangleF(0, 0, 250, 250);
disignBrush = new HatchBrush(bkgStyle[random.Next(bkgStyle.Length - 3)], Color.FromArgb((random.Next(100, 255)), (random.Next(100, 255)), (random.Next(100, 255))), Color.White);
graphics.FillRectangle(disignBrush, rectagleArea);
//generate captcha image
string captchaCode = string.Format("{0:X}", random.Next(1000000, 9999999));
//add catcha code into session
Session["sessionCaptcha"] = captchaCode.ToLower();
Font objFont = new Font("Arial", 25, FontStyle.Bold);
//create image for captcha
graphics.DrawString(captchaCode, objFont, Brushes.Black, 20, 20);
//Save the image
bitmap.Save(Response.OutputStream, ImageFormat.Gif);
}
}