随机图片生成器制作方法分享
/// <summary>
/// pichandler1 的摘要说明
/// </summary>
public class pichandler1 : ihttphandler, irequiressessionstate
{
private string mcheckno = string.empty;
protected imgbuilder _imgbuilder = new imgbuilder();
protected vryimggen _imgbuildernew = new vryimggen();
private string _text = string.empty;
private string _font = "宋体";
private int _fontsize = 8;
private int _padding = 2;
public void processrequest(httpcontext context)
{
mcheckno = discheckno();
context.session["checkcode"] = mcheckno;
this._imgbuilder.fontsize = this._fontsize;
this._imgbuilder.padding = this._padding;
if (!string.isnullorempty(this._font))
{
this._imgbuilder.fonts = new string[] { this._font };
}
this._imgbuildernew.chaoswight = 40;
this._imgbuildernew.fontsize = 25;
this._imgbuildernew.padding = 3;
system.drawing.bitmap image =
this._imgbuildernew.createimage(mcheckno);
system.io.memorystream ms = new system.io.memorystream();
image.save(ms, system.drawing.imaging.imageformat.jpeg);
context.response.clearcontent();
context.response.contenttype = "image/jpeg";
context.response.binarywrite(ms.toarray());
context.session["checkcode"] = mcheckno.tostring(); //如果没有实现irequiressessionstate,则这里会出错,也无法生成图片
context.response.end();
}
//验证码生成
protected string discheckno()
{
string hash = hashcode.getnext();
string checkno = string.empty;
random rd = new random(datetime.now.millisecond);
for (int i = 0; i < 6; i++)
{
checkno += hash.substring(rd.next(1, hash.length - 1), 1);
}
checkno = checkno.replace("0", rd.next(1, 9).tostring());
checkno = checkno.replace("o", rd.next(1, 9).tostring());
checkno = checkno.replace("o", rd.next(1, 9).tostring());
return checkno;
}
public bool isreusable
{
get
{
return false;
}
}
}
调用的类
using system;
using system.drawing;
using system.text;
/// <summary>
/// vryimggen 的摘要说明
/// </summary>
public class vryimggen
{
public static string chinesechars = string.empty;
/// <summary>
/// 英文与数字串
/// </summary>
protected static readonly string englishornumchars = "0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
public vryimggen()
{
rnd = new random(unchecked((int)datetime.now.ticks));
}
/// <summary>
/// 全局随机数生成器
/// </summary>
private random rnd;
int length = 5;
/// <summary>
/// 验证码长度(默认6个验证码的长度)
/// </summary>
public int length
{
get { return length; }
set { length = value; }
}
int fontsize = 20;
/// <summary>
/// 验证码字体大小(为了显示扭曲效果,默认30像素,可以自行修改)
/// </summary>
public int fontsize
{
get { return fontsize; }
set { fontsize = value; }
}
int padding = 4;
/// <summary>
/// 边框补(默认4像素)
/// </summary>
public int padding
{
get { return padding; }
set { padding = value; }
}
bool chaos = true;
/// <summary>
/// 是否输出燥点(默认输出)
/// </summary>
public bool chaos
{
get { return chaos; }
set { chaos = value; }
}
color chaoscolor = color.lightgray;
/// <summary>
/// 输出燥点的颜色(默认灰色)
/// </summary>
public color chaoscolor
{
get { return chaoscolor; }
set { chaoscolor = value; }
}
int chaoswight = 1;
/// <summary>
/// 输出燥点的浓度
/// </summary>
public int chaoswight
{
get { return chaoswight; }
set { chaoswight = value; }
}
color backgroundcolor = color.white;
/// <summary>
/// 自定义背景色(默认白色)
/// </summary>
public color backgroundcolor
{
get { return backgroundcolor; }
set { backgroundcolor = value; }
}
color[] colors = { color.black, color.red, color.darkblue, color.green, color.orange, color.brown, color.darkcyan, color.purple };
/// <summary>
/// 自定义随机颜色数组
/// </summary>
public color[] colors
{
get { return colors; }
set { colors = value; }
}
string[] fonts = { "arial", "georgia" };
/// <summary>
/// 自定义字体数组
/// </summary>
public string[] fonts
{
get { return fonts; }
set { fonts = value; }
}
#region 产生波形滤镜效果
private const double pi = 3.1415926535897932384626433832795;
private const double pi2 = 6.283185307179586476925286766559;
/// <summary>
/// 正弦曲线wave扭曲图片(edit by 51aspx.com)
/// </summary>
/// <param name="srcbmp">图片路径</param>
/// <param name="bxdir">如果扭曲则选择为true</param>
/// <param name="nmultvalue">波形的幅度倍数,越大扭曲的程度越高,一般为3</param>
/// <param name="dphase">波形的起始相位,取值区间[0-2*pi)</param>
/// <returns></returns>
public system.drawing.bitmap twistimage(bitmap srcbmp, bool bxdir, double dmultvalue, double dphase)
{
system.drawing.bitmap destbmp = new bitmap(srcbmp.width, srcbmp.height);
// 将位图背景填充为白色
system.drawing.graphics graph = system.drawing.graphics.fromimage(destbmp);
graph.fillrectangle(new solidbrush(system.drawing.color.white), 0, 0, destbmp.width, destbmp.height);
graph.dispose();
double dbaseaxislen = bxdir ? (double)destbmp.height : (double)destbmp.width;
for (int i = 0; i < destbmp.width; i++)
{
for (int j = 0; j < destbmp.height; j++)
{
double dx = 0;
dx = bxdir ? (pi2 * (double)j) / dbaseaxislen : (pi2 * (double)i) / dbaseaxislen;
dx += dphase;
double dy = math.sin(dx);
// 取得当前点的颜色
int noldx = 0, noldy = 0;
noldx = bxdir ? i + (int)(dy * dmultvalue) : i;
noldy = bxdir ? j : j + (int)(dy * dmultvalue);
system.drawing.color color = srcbmp.getpixel(i, j);
if (noldx >= 0 && noldx < destbmp.width
&& noldy >= 0 && noldy < destbmp.height)
{
destbmp.setpixel(noldx, noldy, color);
}
}
}
return destbmp;
}
#endregion
/// <summary>
/// 生成校验码图片
/// </summary>
/// <param name="code">验证码</param>
/// <returns></returns>
public bitmap createimage(string code)
{
int fsize = fontsize;
int fwidth = fsize + padding;
int imagewidth = (int)(code.length * fwidth) + 4 + padding * 2;
int imageheight = fsize * 2 + padding * 2;
system.drawing.bitmap image = new system.drawing.bitmap(imagewidth - 10, imageheight - 10);
graphics g = graphics.fromimage(image);
g.clear(backgroundcolor);
//给背景添加随机生成的燥点
if (this.chaos)
{
pen pen = new pen(chaoscolor, 0);
int c = chaoswight * 10;
for (int i = 0; i < c; i++)
{
int x = rnd.next(image.width);
int y = rnd.next(image.height);
g.drawrectangle(pen, x, y, 1, 1);
}
}
int left = 0, top = 0, top1 = 1, top2 = 1;
int n1 = (imageheight - fontsize - padding * 2);
int n2 = n1 / 4;
top1 = n2;
top2 = n2 * 2;
font f;
brush b;
int cindex, findex;
//随机字体和颜色的验证码字符
for (int i = 0; i < code.length; i++)
{
cindex = rnd.next(colors.length - 1);
findex = rnd.next(fonts.length - 1);
f = new system.drawing.font(fonts[findex], fsize, system.drawing.fontstyle.bold);
b = new system.drawing.solidbrush(colors[cindex]);
if (i % 2 == 1)
{
top = top2;
}
else
{
top = top1;
}
left = i * fwidth;
g.drawstring(code.substring(i, 1), f, b, left, top);
}
//画一个边框 边框颜色为color.gainsboro
g.drawrectangle(new pen(color.gainsboro, 0), 0, 0, image.width - 1, image.height - 1);
g.dispose();
//产生波形(add by 51aspx.com)
image = twistimage(image, true, 8, 4);
return image;
}
/// <summary>
/// 生成随机字符码
/// </summary>
/// <param name="codelen">字符串长度</param>
/// <param name="zhcharscount">中文字符数</param>
/// <returns></returns>
public string createverifycode(int codelen, int zhcharscount)
{
char[] chs = new char[codelen];
int index;
for (int i = 0; i < zhcharscount; i++)
{
index = rnd.next(0, codelen);
if (chs[index] == '\0')
chs[index] = createzhchar();
else
--i;
}
for (int i = 0; i < codelen; i++)
{
if (chs[i] == '\0')
chs[i] = createenornumchar();
}
return new string(chs, 0, chs.length);
}
/// <summary>
/// 生成默认长度5的随机字符码
/// </summary>
/// <returns></returns>
public string createverifycode()
{
return createverifycode(length, 0);
}
/// <summary>
/// 生成英文或数字字符
/// </summary>
/// <returns></returns>
protected char createenornumchar()
{
return englishornumchars[rnd.next(0, englishornumchars.length)];
}
/// <summary>
/// 生成汉字字符
/// </summary>
/// <returns></returns>
protected char createzhchar()
{
//若提供了汉字集,查询汉字集选取汉字
if (chinesechars.length > 0)
{
return chinesechars[rnd.next(0, chinesechars.length)];
}
//若没有提供汉字集,则根据《gb2312简体中文编码表》编码规则构造汉字
else
{
byte[] bytes = new byte[2];
//第一个字节值在0xb0, 0xf7之间
bytes[0] = (byte)rnd.next(0xb0, 0xf8);
//第二个字节值在0xa1, 0xfe之间
bytes[1] = (byte)rnd.next(0xa1, 0xff);
//根据汉字编码的字节数组解码出中文汉字
string str1 = encoding.getencoding("gb2312").getstring(bytes);
return str1[0];
}
}
}