[验证码实现] Captcha 验证码类,一个很个性的验证码类 (转载)

时间:2023-03-08 16:54:24

点击下载 Captcha.zip

/// <summary>
/// 类说明:条码生成类
/// 编 码 人:苏飞
/// 联系方式:361983679
/// 更新网站:[url=http://www.sufeinet.com/thread-655-1-1.html]http://www.sufeinet.com/thread-655-1-1.html[/url]
/// </summary>
using System;
using System.Drawing; public static class Captcha
{
private static double[] addVector(double[] a, double[] b)
{
return new double[] { a[] + b[], a[] + b[], a[] + b[] };
} private static double[] scalarProduct(double[] vector, double scalar)
{
return new double[] { vector[] * scalar, vector[] * scalar, vector[] * scalar };
} private static double dotProduct(double[] a, double[] b)
{
return a[] * b[] + a[] * b[] + a[] * b[];
} private static double norm(double[] vector)
{
return Math.Sqrt(dotProduct(vector, vector));
} private static double[] normalize(double[] vector)
{
return scalarProduct(vector, 1.0 / norm(vector));
} private static double[] crossProduct(double[] a, double[] b)
{
return new double[]
{
(a[] * b[] - a[] * b[]),
(a[] * b[] - a[] * b[]),
(a[] * b[] - a[] * b[])
};
} private static double[] vectorProductIndexed(double[] v, double[] m, int i)
{
return new double[]
{
v[i + ] * m[] + v[i + ] * m[] + v[i + ] * m[] + v[i + ] * m[],
v[i + ] * m[] + v[i + ] * m[] + v[i + ] * m[] + v[i + ] * m[],
v[i + ] * m[] + v[i + ] * m[] + v[i + ] * m[]+ v[i + ] * m[],
v[i + ] * m[] + v[i + ] * m[] + v[i + ] * m[]+ v[i + ] * m[]
};
} private static double[] vectorProduct(double[] v, double[] m)
{
return vectorProductIndexed(v, m, );
} private static double[] matrixProduct(double[] a, double[] b)
{
double[] o1 = vectorProductIndexed(a, b, );
double[] o2 = vectorProductIndexed(a, b, );
double[] o3 = vectorProductIndexed(a, b, );
double[] o4 = vectorProductIndexed(a, b, ); return new double[]
{
o1[], o1[], o1[], o1[],
o2[], o2[], o2[], o2[],
o3[], o3[], o3[], o3[],
o4[], o4[], o4[], o4[]
};
} private static double[] cameraTransform(double[] C, double[] A)
{
double[] w = normalize(addVector(C, scalarProduct(A, -)));
double[] y = new double[] { , , };
double[] u = normalize(crossProduct(y, w));
double[] v = crossProduct(w, u);
double[] t = scalarProduct(C, -); return new double[]
{
u[], v[], w[], ,
u[], v[], w[], ,
u[], v[], w[], ,
dotProduct(u, t), dotProduct(v, t), dotProduct(w, t),
};
} private static double[] viewingTransform(double fov, double n, double f)
{
fov *= (Math.PI / );
double cot = 1.0 / Math.Tan(fov / );
return new double[] { cot, , , , , cot, , , , , (f + n) / (f - n), -, , , * f * n / (f - n), };
} public static Image Generate(string captchaText)
{
int fontsize = ;
Font font = new Font("Arial", fontsize); SizeF sizeF;
using (Graphics g = Graphics.FromImage(new Bitmap(, )))
{
sizeF = g.MeasureString(captchaText, font, , StringFormat.GenericDefault);
} int image2d_x = (int)sizeF.Width;
int image2d_y = (int)(fontsize * 1.3); Bitmap image2d = new Bitmap(image2d_x, image2d_y);
Color black = Color.Black;
Color white = Color.White; using (Graphics g = Graphics.FromImage(image2d))
{
g.Clear(black);
g.DrawString(captchaText, font, Brushes.White, , );
} Random rnd = new Random();
double[] T = cameraTransform(new double[] { rnd.Next(-, ), -, rnd.Next(, ) }, new double[] { , , });
T = matrixProduct(T, viewingTransform(, , )); double[][] coord = new double[image2d_x * image2d_y][]; int count = ;
for (int y = ; y < image2d_y; y += )
{
for (int x = ; x < image2d_x; x++)
{
int xc = x - image2d_x / ;
int zc = y - image2d_y / ;
double yc = -(double)(image2d.GetPixel(x, y).ToArgb() & 0xff) / * ;
double[] xyz = new double[] { xc, yc, zc, };
xyz = vectorProduct(xyz, T);
coord[count] = xyz;
count++;
}
} int image3d_x = ;
int image3d_y = image3d_x * / ;
Bitmap image3d = new Bitmap(image3d_x, image3d_y);
Color fgcolor = Color.White;
Color bgcolor = Color.Black;
using (Graphics g = Graphics.FromImage(image3d))
{
g.Clear(bgcolor);
count = ;
double scale = 1.75 - (double)image2d_x / ;
for (int y = ; y < image2d_y; y += )
{
for (int x = ; x < image2d_x; x++)
{
if (x > )
{
double x0 = coord[count - ][] * scale + image3d_x / ;
double y0 = coord[count - ][] * scale + image3d_y / ;
double x1 = coord[count][] * scale + image3d_x / ;
double y1 = coord[count][] * scale + image3d_y / ;
g.DrawLine(new Pen(fgcolor), (float)x0, (float)y0, (float)x1, (float)y1);
}
count++;
}
}
}
return image3d;
}
}