public Bitmap CreateCaptcha()
{
Random randomGenerator = new Random();
// Bitmap bmp = new Bitmap(img_width, img_height, PixelFormat.Format16bppRgb555);
Bitmap bmp = new Bitmap(img_width, img_height, PixelFormat.Format32bppArgb);
Rectangle rect = new Rectangle(0, 0, img_width, img_height);
StringFormat format = new StringFormat();
Graphics g = Graphics.FromImage(bmp);
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
SizeF size;
float fontSize = img_fontsize + 1;
Font font;
try {
font = new Font(img_fontname, img_fontsize);
font.Dispose();
}
catch (Exception ex) {
img_fontname = System.Drawing.FontFamily.GenericSerif.Name;
}
// build a new string with space through the chars
// i.e. keyword 'hello' become 'h e l l o '
string tempKey = "";
for (int ii=0; ii< keyword.Length; ii++)
{
tempKey = String.Concat(tempKey, keyword[ii].ToString());
tempKey = String.Concat(tempKey, " ");
}
// Adjust the font size until the text fits within the image.
do {
fontSize--;
font = new Font(img_fontname, fontSize, img_fontstyle);
size = g.MeasureString(tempKey, font);
} while (size.Width > (0.8*bmp.Width));
img_font = font;
g.Clear(Color.Silver);
g.SmoothingMode = SmoothingMode.AntiAlias;
// fill with a liner gradient
// random colors
g.FillRectangle(new LinearGradientBrush(new Point(0,0), new Point(bmp.Width,bmp.Height),
Color.FromArgb(255, //randomGenerator.Next(255),
randomGenerator.Next(255),
randomGenerator.Next(255),
randomGenerator.Next(255)),
Color.FromArgb(randomGenerator.Next(100),
randomGenerator.Next(255),
randomGenerator.Next(255),
randomGenerator.Next(255))), rect);
if (degree == 0)
BitmapFilter.Swirl(bmp, randomGenerator.NextDouble(), true);
else
BitmapFilter.Swirl(bmp, degree , true);
HatchBrush hatchBrush = new HatchBrush(HatchStyle.LargeConfetti, Color.LightGray, Color.DarkGray);
int m = Math.Max(rect.Width, rect.Height);
for (int i = 0; i < (int) (rect.Width * rect.Height / 30F); i++) {
int x = randomGenerator.Next(rect.Width);
int y = randomGenerator.Next(rect.Height);
int w = randomGenerator.Next(m / 50);
int h = randomGenerator.Next(m / 50);
g.FillEllipse(hatchBrush, x, y, w, h);
}
int posy;
int posx = Convert.ToInt32((img_width - size.Width)/2);
int deltax = Convert.ToInt32(size.Width/tempKey.Length);
Point[] ps = new Point[points];
for (int ii=0; ii < points; ii++) {
int x,y;
x = randomGenerator.Next(bmp.Width);
y = randomGenerator.Next(bmp.Height);
ps[ii] = new Point(x,y);
}
g.DrawCurve(new Pen( img_linecolor, 2), ps, Convert.ToSingle(randomGenerator.NextDouble()));
for (int l=0; l < tempKey.Length; l++ ) {
posy = ((int)(2.5 * (bmp.Height/5))) + (((l%2)==0)?-2:2) * ((int)(size.Height/3));
posy = (int)((bmp.Height/2)+ (size.Height/2));
posy += (int) ((((l%2)==0)?-2:2) * ((size.Height/3)));
posx += deltax;
g.DrawString(tempKey[l].ToString(), img_font, img_fontcolor, posx, posy, format);
}
//BitmapFilter.Water(bmp, wave, true);
//BitmapFilter.TimeWarp(bmp, 2, true);
font.Dispose();
hatchBrush.Dispose();
g.Dispose();
return bmp;
}