Код для генерации изображений с кодами, которые препятствуют автоматической регистрации.
1 //AntiRobotImage.cs
2 using System;
3 using System.Drawing;
4 using System.Drawing.Drawing2D;
5 using System.Drawing.Imaging;
6 using System.Drawing.Text;
7
8
9 namespace Gaech.Web.UI
10 {
11 public class AntiRobotImage
12 {
13 private string[] Fonts = { "Verdana", "Tahoma", "Arial", "Small", "Courier", "Fixedsys", "Garamound" };
14
15 #region Text
16 private string text;
17 /// <summary>
18 /// Gets or sets the text.
19 /// </summary>
20 /// <value>The text.</value>
21 public string Text
22 {
23 get
24 {
25 return text;
26 }
27 set
28 {
29 text = value;
30 }
31 }
32 #endregion
33
34 #region Width
35 private int width;
36 /// <summary>
37 /// Gets or sets the image width.
38 /// </summary>
39 /// <value>The width.</value>
40 public int Width
41 {
42 get
43 {
44 return width;
45 }
46 set
47 {
48 width = value;
49 }
50 }
51 #endregion
52
53 #region Height
54 private int height;
55 /// <summary>
56 /// Gets or sets the image height.
57 /// </summary>
58 /// <value>The height.</value>
59 public int Height
60 {
61 get
62 {
63 return height;
64 }
65 set
66 {
67 height = value;
68 }
69 }
70 #endregion
71
72 #region Image
73 private Bitmap image;
74 /// <summary>
75 /// Gets or sets the image.
76 /// </summary>
77 /// <value>The image.</value>
78 public Bitmap Image
79 {
80 get
81 {
82 return image;
83 }
84 set
85 {
86 image = value;
87 }
88 }
89 #endregion
90
91 #region FontFamily
92 private string fontFamily;
93 public string FontFamily
94 {
95 get
96 {
97 return fontFamily;
98 }
99 set
100 {
101 fontFamily = value;
102 }
103 }
104 #endregion
105
106 #region Random
107 // For generating random numbers.
108 private Random random = new Random();
109 #endregion
110
111 #region GenerateImage()
112 private void GenerateImage()
113 {
114 // Create a new 32-bit bitmap image.
115 Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format32bppArgb);
116
117 // Create a graphics object for drawing.
118 Graphics g = Graphics.FromImage(bitmap);
119 g.SmoothingMode = SmoothingMode.AntiAlias;
120 Rectangle rect = new Rectangle(0, 0, Width, Height);
121
122 // Fill in the background.
123 HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White);
124 g.FillRectangle(hatchBrush, rect);
125
126 // Set up the text font.
127 SizeF size;
128 float fontSize = height;
129 Font font;
130 // Adjust the font size until the text fits within the image.
131 do
132 {
133 fontSize--;
134 font = new Font(FontFamily, fontSize, FontStyle.Bold);
135 size = g.MeasureString(this.text, font);
136 } while (size.Width > rect.Width);
137
138 // Set up the text format.
139 StringFormat format = new StringFormat();
140 format.Alignment = StringAlignment.Center;
141 format.LineAlignment = StringAlignment.Center;
142
143 // Create a path using the text and warp it randomly.
144 GraphicsPath path = new GraphicsPath();
145 path.AddString(this.text, font.FontFamily, (int)font.Style, font.Size, rect, format);
146 float v = 4F;
147 PointF[] points =
148 {
149 new PointF(this.random.Next(rect.Width) / v, this.random.Next(rect.Height) / v),
150 new PointF(rect.Width - this.random.Next(rect.Width) / v, this.random.Next(rect.Height) / v),
151 new PointF(this.random.Next(rect.Width) / v, rect.Height - this.random.Next(rect.Height) / v),
152 new PointF(rect.Width - this.random.Next(rect.Width) / v, rect.Height - this.random.Next(rect.Height) / v)
153 };
154 Matrix matrix = new Matrix();
155 matrix.Translate(0F, 0F);
156 path.Warp(points, rect, matrix, WarpMode.Perspective, 0F);
157
158 // Draw the text.
159 hatchBrush = new HatchBrush(HatchStyle.LargeConfetti, Color.LightGray, Color.DarkGray);
160 g.FillPath(hatchBrush, path);
161
162 // Add some random noise.
163 int m = Math.Max(rect.Width, rect.Height);
164 for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++)
165 {
166 int x = this.random.Next(rect.Width);
167 int y = this.random.Next(rect.Height);
168 int w = this.random.Next(m / 50);
169 int h = this.random.Next(m / 50);
170 g.FillEllipse(hatchBrush, x, y, w, h);
171 }
172
173 // Clean up.
174 font.Dispose();
175 hatchBrush.Dispose();
176 g.Dispose();
177
178 // Set the image.
179 this.image = bitmap;
180 }
181 #endregion
182
183 #region Constructors()
184
185 /// <summary>
186 /// Initializes a new instance of the <see cref="T:AntiRobotImage"/> class.
187 /// </summary>
188 /// <param name="Text">The text.</param>
189 /// <param name="Width">The width.</param>
190 /// <param name="Height">The height.</param>
191 public AntiRobotImage(string Text, int Width, int Height)
192 {
193 this.Text = Text;
194 this.Width = Width;
195 this.Height = Height;
196 this.FontFamily = Fonts[random.Next(0, Fonts.Length - 1)];
197 GenerateImage();
198 }
199 #endregion
200 }
201 }
202 // SampleAntiRobotImage.aspx.cs
203 using System;
204 using System.Data;
205 using System.Configuration;
206 using System.Collections;
207 using System.Web;
208 using System.Web.Security;
209 using System.Web.UI;
210 using System.Web.UI.WebControls;
211 using System.Web.UI.WebControls.WebParts;
212 using System.Web.UI.HtmlControls;
213
214 using Mediachase.Web.UI;
215
216 public partial class Controls_Comments_AntiRobot : System.Web.UI.Page
217 {
218 private Random random = new Random();
219
220 protected void Page_Load(object sender, EventArgs e)
221 {
222 if (Session["AntiRobotImageKeyword"] == null)
223 {
224 Session["AntiRobotImageKeyword"] = random.Next(100000, 999999).ToString();
225 }
226 AntiRobotImage ari = new AntiRobotImage(Session["AntiRobotImageKeyword"].ToString(), 150, 50);
227
228 Response.Clear();
229
230 Response.ContentType = "image/jpeg";
231
232
233 ari.Image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
234 }
235 }
236 //ИСПОЛЬЗОВАНИЕ
237 <img src="SampleAntiRobotImage.aspx">