一、图片加水印
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default1.aspx.cs" Inherits="Default1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:FileUpload ID="FileUpload1" runat="server" /><asp:Button ID="Button1" runat="server" Text="上传" /><br /> <asp:Image ID="Image1" runat="server" /> </div> </form> </body> </html>
后台代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; public partial class Default1 : System.Web.UI.Page { 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); //2、往上面画上水印 Graphics g = Graphics.FromImage(img);//这个是绘制方法,等号右边是往那张图片上绘制 string s = "http://www.itnba.com"; //绘制的字符串 Font f = new Font("微软雅黑",18);//绘制使用的字体 SolidBrush b = new SolidBrush(Color.Red);//绘制使用的刷子,设置颜色 g.DrawString(s, f, b, 0, 0); //绘制完毕 //3、保存到服务器指定路径,并且在Image控件中显示 string path = Server.MapPath("Uploads/" + FileUpload1.FileName); img.Save(path);//将绘制完的图片保存,注意:千万不要把选择的图片保存 Image1.ImageUrl = "Uploads/" + FileUpload1.FileName; } }
二、图片验证码
1、首先引用命名空间:system.Drawing;
2、将aspx页面当做图片路径,在load事件中进行绘制并保存在内存流中
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> 请输入验证码:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Image ID="Image1" runat="server" ImageUrl="~/YZM.aspx" /><br /> <asp:Button ID="Button1" runat="server" Text="验证" /><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html> <script type="text/javascript"> var aaa = 0; document.getElementById('Image1').onclick = function () { this.setAttribute("src", "yzm.aspx?id="+aaa); aaa++; }; </script>
后台代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; } void Button1_Click(object sender, EventArgs e) { if (TextBox1.Text == Session["YZM"].ToString()) { Label1.Text = "正确!"; } else { Label1.Text = "错误!"; } } }
生成图片验证码的后台代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; public partial class YZM : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //1、准备画布 Bitmap img = new Bitmap(60, 30); //2、往画布上绘制验证码 string all = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; string aaa = ""; Random r = new Random(); for (int i = 0; i < 4; i++) { aaa += all.Substring(r.Next(all.Length), 1); } Session["YZM"] = aaa; Graphics g = Graphics.FromImage(img); Font f = new Font("微软雅黑",16); SolidBrush b = new SolidBrush(Color.Green); g.DrawString(aaa, f, b, 0, 0); img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png); } }