要解决的问题:
@H_404_4@ 1. 如何随机生成图片
@H_404_4@ 生成System.Drawing.Bitmap对象,使用system.drawing.graphics向位图对象中绘图。
@H_404_4@ 2. 如何在WebService的方法中通过参数传递图片数据
@H_404_4@ 将Bitmap对象输出成字节流,WebMothod使用字节数组返回该字节流。
@H_404_4@ 实例:
@H_404_4@ 1. 用VS.NET 2003创建一个ASP.NET Webservice工程,默认的Service名为MyService,为MyService添加一个名为GenerateVerifyImage的WebMethod。该方法的代码如下:
@H_404_4@ /// <summary>
/// 生成图片验证码
/// </summary>
/// <param name="nLen">验证码的长度</param>
/// <param name="strKey">输出参数,验证码的内容</param>
/// <returns>图片字节流</returns>
[WebMethod]
public byte[] GenerateVerifyImage(int nLen,ref string strKey)
{
int nBmpWidth = 13*nLen+5;
int nBmpHeight = 25;
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(nBmpWidth,nBmpHeight); @H_404_4@ // 1. 生成随机背景颜色
int nRed,nGreen,nBlue; // 背景的三元色
System.Random rd = new Random((int)System.DateTime.Now.Ticks);
nRed = rd.Next(255)%128+128;
nGreen = rd.Next(255)%128+128;
nBlue = rd.Next(255)%128+128; @H_404_4@ // 2. 填充位图背景
system.drawing.graphics graph = system.drawing.graphics.FromImage(bmp);
graph.FillRectangle(new SolidBrush(System.Drawing.Color.FromArgb(nRed,nBlue))
,0
,nBmpWidth
,nBmpHeight); @H_404_4@
// 3. 绘制干扰线条,采用比背景略深一些的颜色
int nLines = 3;
System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(nRed-17,nGreen-17,nBlue-17),2);
for(int a =0;a< nLines;a++)
{
int x1 = rd.Next() % nBmpWidth;
int y1 = rd.Next() % nBmpHeight;
int x2 = rd.Next() % nBmpWidth;
int y2 = rd.Next() % nBmpHeight;
graph.DrawLine(pen,x1,y1,x2,y2);
} @H_404_4@ // 采用的字符集,可以随即拓展,并可以控制字符出现的几率
string strCode = "ABCDEFGHIJKLMnopQRSTUVWXYZ"; @H_404_4@ // 4. 循环取得字符,并绘制
string strResult = "";
for(int i=0;i<nLen;i++)
{
int x = (i*13 + rd.Next(3));
int y = rd.Next(4) + 1; @H_404_4@ // 确定字体
System.Drawing.Font font = new System.Drawing.Font("Courier New",
12 + rd.Next()%4,
System.Drawing.FontStyle.Bold);
char c = strCode[rd.Next(strCode.Length)]; // 随机获取字符
strResult += c.ToString(); @H_404_4@ // 绘制字符
graph.DrawString(c.ToString(),
font,
new SolidBrush(System.Drawing.Color.FromArgb(nRed-60+y*3,nGreen-60+y*3,nBlue-40+y*3)),
x,
y);
} @H_404_4@ // 5. 输出字节流
System.IO.MemoryStream bstream = new System.IO.MemoryStream();
bmp.Save(bstream,System.Drawing.Imaging.ImageFormat.Jpeg);
bmp.dispose();
graph.dispose(); @H_404_4@ strKey = strResult;
byte[] byteReturn = bstream.ToArray();
bstream.Close(); @H_404_4@ return byteReturn;
} @H_404_4@ 2. 测试WebMethod,添加一个WebForm,引用上述WebService,引用名为imagesvr。在Page_Load中添加代码: @H_404_4@ ...
imagesvr.MyService imgsvr = new imagesvr.MyService();
string strKey = "";
byte[] data = imgsvr.GenerateVerifyImage(5,ref strKey);
Response.OutputStream.Write(data,data.Length);
... @H_404_4@ 3. 运行。每次refresh这个WebForm时,就会显示一个新生成的图片验证码,而函数的输出参数strKey保存的就是这个验证码的实际内容,可以保存在Session中,作为验证使用。 @H_404_4@ 上次开发出图片验证码之后,根据一些朋友的建议,本着验证码易识别(针对人),不易破解,美观的原则,改进了验证码生成的算法,采用图像滤镜的方法,对图片验证码进行反破解干扰,结果图片示例如下:
private const double PI2 = 6.283185307179586476925286766559; @H_404_4@ /// <summary>
/// 正弦曲线Wave扭曲图片
/// </summary>
/// <param name="srcBmp"></param>
/// <param name="bXDir"></param>
/// <param name="nMultValue">波形的幅度倍数</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); @H_404_4@ // 将位图背景填充为白色
system.drawing.graphics graph = system.drawing.graphics.FromImage(destBmp);
graph.FillRectangle(new SolidBrush(System.Drawing.Color.White),destBmp.Width,destBmp.Height);
graph.dispose();
double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width; @H_404_4@ 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); @H_404_4@ // 取得当前点的颜色
int nOldX = 0,nOldY = 0;
nOldX = bXDir ? i + (int)(dy*dMultValue) : i;
nOldY = bXDir ? j : j + (int)(dy*dMultValue); @H_404_4@ System.Drawing.Color color = srcBmp.GetPixel(i,j);
if(nOldX >= 0 && nOldX < destBmp.Width
&& nOldY >=0 && nOldY < destBmp.Height)
{
destBmp.SetPixel(nOldX,nOldY,color);
}
}
} @H_404_4@ return destBmp;
} @H_404_4@ 开头的示例图片,是两次波形效果的叠加,两次效果分别针对X轴方向和Y轴方向,如果取消对边缘背景色的填充,可以看到算法对图形的影响,如下图:
/// 生成图片验证码
/// </summary>
/// <param name="nLen">验证码的长度</param>
/// <param name="strKey">输出参数,验证码的内容</param>
/// <returns>图片字节流</returns>
[WebMethod]
public byte[] GenerateVerifyImage(int nLen,ref string strKey)
{
int nBmpWidth = 13*nLen+5;
int nBmpHeight = 25;
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(nBmpWidth,nBmpHeight); @H_404_4@ // 1. 生成随机背景颜色
int nRed,nGreen,nBlue; // 背景的三元色
System.Random rd = new Random((int)System.DateTime.Now.Ticks);
nRed = rd.Next(255)%128+128;
nGreen = rd.Next(255)%128+128;
nBlue = rd.Next(255)%128+128; @H_404_4@ // 2. 填充位图背景
system.drawing.graphics graph = system.drawing.graphics.FromImage(bmp);
graph.FillRectangle(new SolidBrush(System.Drawing.Color.FromArgb(nRed,nBlue))
,0
,nBmpWidth
,nBmpHeight); @H_404_4@
// 3. 绘制干扰线条,采用比背景略深一些的颜色
int nLines = 3;
System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(nRed-17,nGreen-17,nBlue-17),2);
for(int a =0;a< nLines;a++)
{
int x1 = rd.Next() % nBmpWidth;
int y1 = rd.Next() % nBmpHeight;
int x2 = rd.Next() % nBmpWidth;
int y2 = rd.Next() % nBmpHeight;
graph.DrawLine(pen,x1,y1,x2,y2);
} @H_404_4@ // 采用的字符集,可以随即拓展,并可以控制字符出现的几率
string strCode = "ABCDEFGHIJKLMnopQRSTUVWXYZ"; @H_404_4@ // 4. 循环取得字符,并绘制
string strResult = "";
for(int i=0;i<nLen;i++)
{
int x = (i*13 + rd.Next(3));
int y = rd.Next(4) + 1; @H_404_4@ // 确定字体
System.Drawing.Font font = new System.Drawing.Font("Courier New",
12 + rd.Next()%4,
System.Drawing.FontStyle.Bold);
char c = strCode[rd.Next(strCode.Length)]; // 随机获取字符
strResult += c.ToString(); @H_404_4@ // 绘制字符
graph.DrawString(c.ToString(),
font,
new SolidBrush(System.Drawing.Color.FromArgb(nRed-60+y*3,nGreen-60+y*3,nBlue-40+y*3)),
x,
y);
} @H_404_4@ // 5. 输出字节流
System.IO.MemoryStream bstream = new System.IO.MemoryStream();
bmp.Save(bstream,System.Drawing.Imaging.ImageFormat.Jpeg);
bmp.dispose();
graph.dispose(); @H_404_4@ strKey = strResult;
byte[] byteReturn = bstream.ToArray();
bstream.Close(); @H_404_4@ return byteReturn;
} @H_404_4@ 2. 测试WebMethod,添加一个WebForm,引用上述WebService,引用名为imagesvr。在Page_Load中添加代码: @H_404_4@ ...
imagesvr.MyService imgsvr = new imagesvr.MyService();
string strKey = "";
byte[] data = imgsvr.GenerateVerifyImage(5,ref strKey);
Response.OutputStream.Write(data,data.Length);
... @H_404_4@ 3. 运行。每次refresh这个WebForm时,就会显示一个新生成的图片验证码,而函数的输出参数strKey保存的就是这个验证码的实际内容,可以保存在Session中,作为验证使用。 @H_404_4@ 上次开发出图片验证码之后,根据一些朋友的建议,本着验证码易识别(针对人),不易破解,美观的原则,改进了验证码生成的算法,采用图像滤镜的方法,对图片验证码进行反破解干扰,结果图片示例如下:
private const double PI2 = 6.283185307179586476925286766559; @H_404_4@ /// <summary>
/// 正弦曲线Wave扭曲图片
/// </summary>
/// <param name="srcBmp"></param>
/// <param name="bXDir"></param>
/// <param name="nMultValue">波形的幅度倍数</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); @H_404_4@ // 将位图背景填充为白色
system.drawing.graphics graph = system.drawing.graphics.FromImage(destBmp);
graph.FillRectangle(new SolidBrush(System.Drawing.Color.White),destBmp.Width,destBmp.Height);
graph.dispose();
double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width; @H_404_4@ 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); @H_404_4@ // 取得当前点的颜色
int nOldX = 0,nOldY = 0;
nOldX = bXDir ? i + (int)(dy*dMultValue) : i;
nOldY = bXDir ? j : j + (int)(dy*dMultValue); @H_404_4@ System.Drawing.Color color = srcBmp.GetPixel(i,j);
if(nOldX >= 0 && nOldX < destBmp.Width
&& nOldY >=0 && nOldY < destBmp.Height)
{
destBmp.SetPixel(nOldX,nOldY,color);
}
}
} @H_404_4@ return destBmp;
} @H_404_4@ 开头的示例图片,是两次波形效果的叠加,两次效果分别针对X轴方向和Y轴方向,如果取消对边缘背景色的填充,可以看到算法对图形的影响,如下图:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。