Asp.net开发之webform图片水印和图片验证码的实现方法
程序员文章站
2022-10-15 08:38:22
两者都需要引入命名空间:using system.drawing;
一、图片水印
前台photoshuiyin.aspx代码:
&l...
两者都需要引入命名空间:using system.drawing;
一、图片水印
前台photoshuiyin.aspx代码:
<div> <asp:fileupload id="fileupload1" runat="server" /> <asp:button id="button1" runat="server" text="上传" /><br /> <asp:image id="image1" runat="server" /> </div>
后台photoshuiyin.aspx.cs代码:
protected void page_load(object sender, eventargs e) { button1.click += button1_click; } void button1_click(object sender, eventargs e) { //1、制作画布 system.drawing.image img = system.drawing.image.fromstream(fileupload1.filecontent); graphics g = graphics.fromimage(img); //水印样式:画什么东西 string a = "http://www.itnba.com"; //字体、大小 font f = new font("黑体", 30); //颜色 brush b = new solidbrush(color.red); //0,0——开始画水印的位置 g.drawstring(a, f, b, 0, 0); //保存路径 string path = "images/" + fileupload1.filename; img.save(server.mappath(path)); //在image控件中展示 image1.imageurl = path; }
效果展示:
二、图片验证码
前台photoyanzhengma.aspx代码:
<form id="form1" runat="server"> <div> 用户名:<asp:textbox id="textbox1" runat="server"></asp:textbox> <br /> 密码:<asp:textbox id="textbox2" runat="server"></asp:textbox> <br /> 验证码:<asp:textbox id="textbox3" runat="server"></asp:textbox> <asp:image id="image1" runat="server" imageurl="yzm.aspx" /> <asp:label id="label1" runat="server" text="label"></asp:label> <br /> <asp:button id="button1" runat="server" text="button" /> </div> </form> </body> </html> <script type="text/javascript"> var aaa = 1; document.getelementbyid("image1").onclick = function () { this.setattribute("src", "yzm.aspx?id=" + aaa); aaa++; }; </script>
链接页面“yzm.aspx”——无需前台代码,后台代码是:
protected void page_load(object sender, eventargs e) { random r = new random(); string aaa = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz1234567890"; //生成画布 bitmap img = new bitmap(80, 30); //画布背景色泛性组合 list<color> clist = new list<color>(); clist.add(color.yellow); clist.add(color.green); clist.add(color.blue); clist.add(color.aqua); clist.add(color.orange); clist.add(color.pink); graphics g = graphics.fromimage(img); g.fillrectangle(new solidbrush(clist[r.next(0, clist.count)]), 0, 0, 80, 30); //随机生成显示的验证码组合 string str = ""; for (int i = 0; i < 4; i++) { str += aaa.substring(r.next(0, aaa.length), 1); } session["yzm"] = str; font f = new font("黑体", 20); brush b = new solidbrush(color.red); //生成 g.drawstring(str, f, b, 10, 0); //添加干扰线 for (int i = 0; i < r.next(6, 20); i++) { brush bb = new solidbrush(clist[r.next(0, clist.count)]); pen p = new pen(bb, 1); g.drawline(p, r.next(0, 80), r.next(0, 30), r.next(0, 80), r.next(0, 30)); } //保存完成 img.save(response.outputstream, system.drawing.imaging.imageformat.jpeg); response.end(); }
效果展示:
以上所述是小编给大家介绍的asp.net开发之webform图片水印和图片验证码的实现方法,希望对大家有所帮助
上一篇: 简单谈谈Python的pycurl模块
下一篇: 苦瓜茶有什么效果