Web前端开发之水印、图片验证码

网友投稿 251 2023-06-28


Web前端开发之水印、图片验证码

一、水印

1.画布

System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent);

2.创建绘制对象,告诉它往哪张图片上绘制

Graphics g = Graphics.FromImage(img);

3.绘制的内容

string s = itnba.com;

4.绘制的字体

Font f = new Font("黑体",130);

5.绘制的画刷

Brush b = new SolidBrush(Color.Red);

6.开始绘制

g.DrawString(s, f, b, 5, 5);

二、图片验证码

Bitmap bit = new Bitmap(90, 40);//画布大小

Graphics g = Graphics.FromImage(bit);//创建绘制对象,告诉它往哪张图片上绘制

Random r = new Random();

string s = "";

Color color1 = Color.FromArgb(r.Next(155, 255), r.Next(155, 255), r.Next(155, 255));

g.FillRectangle(new SolidBrush(color1), 0, 0, 90, 40);//画布颜色随机

for (int i = 0; i < 10; i++)//随机画干扰线

{

Color color3 = Color.FromArgb(r.Next(170, 255), r.Next(190, 255), r.Next(170, 255));

Pen pp = new Pen(new SolidBrush(color3), r.Next(0, 5));

g.DrawLine(pp, r.Next(0, 90), r.Next(0, 40), r.Next(0, 90), r.Next(0, 40));

}

string yan = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";//设置随机的验证码,从里面抽取

int wei = 0;

for (int i = 0; i < 4; i++)//取四位验证码,颜色随机

{

string m = yan.Substring(r.Next(0, yan.Length), 1);

s += m;

Color color2 = Color.FromArgb(r.Next(0, 155), r.Next(0, 155), r.Next(0, 155));

int w = r.Next(20, 25);

Font f = new Font("黑体", w);

SolidBrush b = new SolidBrush(color2);

g.DrawString(m, f, b, wei, r.Next(0, 10));

wei += w;

}

Session["YZM"] = s;//把验证码保存到session中

for (int i = 0; i < 251; i++)

{

Color color3 = Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255));

Pen pp = new Pen(new SolidBrush(color3), r.Next(0, 5));

int a = r.Next(0, 90);

int b = r.Next(0, 40);

g.DrawLine(pp, a, b,kTmrNDFq (a + 1), (b + 1));

}

bit.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

Response.End();

验证码:

//这里图片路径不是一张图片的话会默认打开

以上所述是给大家分享的Web前端开发之水印、图片验证码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,会及时回复大家的,在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java中length,length(),size()详解及区别
下一篇:浅谈Java自定义注解和运行时靠反射获取注解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~