標籤:attribute attr box path lin mss png ssm protect
一、圖片浮水印
1:引命名空間System.Drawing;
前端代碼
<div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button1" runat="server" Text="上傳" /><br /> <asp:Image ID="Image1" runat="server" /></div>
後台代碼
//準備畫布 System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent); //建立畫筆 Graphics g = Graphics.FromImage(img); string s = "漢企奇點網路0928"; //畫筆的字型 Font f = new Font("微軟雅黑",20); //畫筆的樣式和顏色 Brush b = new SolidBrush(Color.Red); //在什麼位置畫 PointF p = new PointF(20,20); //畫字串 g.DrawString(s, f, b, p); string sss = "images/"+DateTime.Now.ToString("yyyyMMddhhmmssms")+FileUpload1.FileName; img.Save(Server.MapPath(sss)); Image1.ImageUrl = sss;
二、圖片驗證碼
1:引命名空間System.Drawing;
單獨建立一個表單來繪製驗證碼
後台代碼
protected void Page_Load(object sender, EventArgs e) { Bitmap img = new Bitmap(80, 40); Graphics g = Graphics.FromImage(img); Random r = new Random(); //建立一個顏色集合 或者也可以通過RGBA顏色直接隨機產生 List<Color> clist = new List<Color>(); clist.Add(Color.Yellow); clist.Add(Color.Green); clist.Add(Color.Blue); clist.Add(Color.Pink); clist.Add(Color.Orange); clist.Add(Color.Black); //繪製背景色 g.FillRectangle(new SolidBrush(clist[r.Next(0, clist.Count)]), 0, 0, 80, 40); //迴圈隨機繪製幹擾線 for (int i = 0; i < 10; i++) { Color ccc = clist[r.Next(0, clist.Count)]; Pen ppp = new Pen(new SolidBrush(ccc), r.Next(1, 5)); g.DrawLine(ppp, new PointF(r.Next(0, 80), r.Next(0, 40)), new PointF(r.Next(0, 80), r.Next(0, 40))); } //準備一些字母數字 string all = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; string s = ""; //從上面隨機出四個字元拼接 for (int i = 0; i < 4; i++) { int a = r.Next(all.Length); s += all.Substring(a, 1); } //存入Session Session["YZM"] = s; //繪製出驗證碼 g.DrawString(s, new Font("微軟雅黑", 16), new SolidBrush(Color.Red), new PointF(3, 3)); //通過流把繪製好的驗證碼輸出出去 img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png); Response.End(); }
介面接收
void Button1_Click(object sender, EventArgs e) { string t1 = TextBox1.Text; string t2 = Session["YZM"].ToString(); if (t1.ToUpper() == t2.ToUpper()) { Label1.Text = "驗證成功!"; } else { Label1.Text = "驗證失敗!"; } }
重建驗證碼(JS)
<script type="text/javascript"> var a = 0; document.getElementById("Image1").onclick = function () { this.setAttribute("src", "yzm.aspx?id=" + a); a++; }</script>
webform:圖片浮水印、驗證碼製作