string text = project.Variables["captchaText"].Value;
WebRequest a = WebRequest.Create(project.Variables["captchaImgSrc"].Value);
a.Timeout = 60000;
a.Method = "GET";
a.ContentType = "text/plain";
Stream response = a.GetResponse().GetResponseStream();
Image image = Bitmap.FromStream(response);
Bitmap img = image as Bitmap;
Image[] part = new Image[9];
part[0] = img.Clone(new Rectangle(0, 0, 100, 100), img.PixelFormat);
part[1] = img.Clone(new Rectangle(100, 0, 100, 100), img.PixelFormat);
part[2] = img.Clone(new Rectangle(200, 0, 100, 100), img.PixelFormat);
part[3] = img.Clone(new Rectangle(0, 100, 100, 100), img.PixelFormat);
part[4] = img.Clone(new Rectangle(100, 100, 100, 100), img.PixelFormat);
part[5] = img.Clone(new Rectangle(200, 100, 100, 100), img.PixelFormat);
part[6] = img.Clone(new Rectangle(0, 200, 100, 100), img.PixelFormat);
part[7] = img.Clone(new Rectangle(100, 200, 100, 100), img.PixelFormat);
part[8] = img.Clone(new Rectangle(200, 200, 100, 100), img.PixelFormat);
for (int i = 0; i < 9; i++)
{
Graphics.FromImage(part[i]).DrawString((i + 1).ToString(), new Font("Arial", 28, FontStyle.Bold), new SolidBrush(Color.Red), new RectangleF(0, 0, 0, 35));
}
Image res = new Bitmap((part[0].Width * 3) + 40, (part[0].Height * 3) + 140);
Graphics g = Graphics.FromImage(res);
Rectangle rect = new Rectangle(0, 0, (part[0].Width * 3) + 40, (part[0].Height * 3) + 140);
SolidBrush brush = new SolidBrush(Color.White);
Region region = new Region(rect);
g.FillRegion(brush, region);
g.DrawImage(part[0], new Point(10, 110));
g.DrawImage(part[1], new Point(120, 110));
g.DrawImage(part[2], new Point(230, 110));
g.DrawImage(part[3], new Point(10, 220));
g.DrawImage(part[4], new Point(120, 220));
g.DrawImage(part[5], new Point(230, 220));
g.DrawImage(part[6], new Point(10, 330));
g.DrawImage(part[7], new Point(120, 330));
g.DrawImage(part[8], new Point(230, 330));
var textBounds = g.VisibleClipBounds;
textBounds.Inflate(-5, -5);
g.DrawString
(
text + "\r\nПример: 123(!!Это не ответ!!)",
new Font("Arial", 14, FontStyle.Bold),
Brushes.Black,
textBounds
);
ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");
EncoderParameters myEncoderParameters = new EncoderParameters(1);
myEncoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 75L);
res.Save(project.Variables["captchaPath"].Value, myImageCodecInfo, myEncoderParameters);