WebForm 上传图片加水印!

 界面:

1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 8     <title></title>
 9     <style type="text/css">
10         #Image1 {
11             width: 500px;
12         }
13     </style>
14 </head>
15 <body>
16     <form id="form1" runat="server">
17         <div>
18             <asp:FileUpload ID="FileUpload1" runat="server" />
19             <asp:Button ID="Button1" runat="server" Text="上传" />
20             <br />
21             <asp:Image ID="Image1" runat="server" />
22         </div>
23     </form>
24 </body>
25 </html>
————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
后台:
1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 using System.Drawing;
 8 
 9 public partial class _Default : System.Web.UI.Page
10 {
11     protected void Page_Load(object sender, EventArgs e)
12     {
13         Button1.Click += Button1_Click;
14     }
15 
16     void Button1_Click(object sender, EventArgs e)
17     {
18         //画布
19         System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent);
20 
21         Graphics g = Graphics.FromImage(img);
22         //水印内容
23         string s = "汉企奇点网络0928";
24         Font f = new Font("微软雅黑",20);//字体
25         Brush b = new SolidBrush(Color.Red);//颜色
26         //所在画布位置
27         PointF p = new PointF(20,20);
28         PointF p1 = new PointF(100, 100);
29         PointF p2 = new PointF(400, 100);
30         PointF p3 = new PointF(250, 200);
31 
32         g.DrawString(s, f, b, p);
33         g.DrawString(s, f, b, p1);
34         g.DrawString(s, f, b, p2);
35         g.DrawString(s, f, b, p3);
36 
37         string sss = "images/"+DateTime.Now.ToString("yyyyMMddhhmmssms")+FileUpload1.FileName;
38         //保存位置
39         img.Save(Server.MapPath(sss));
40         //显示图片
41         Image1.ImageUrl = sss;
42     }
43 }

 

 

图片验证码:

  单独为验证码图片创建一个窗体,界面上什么都不用写,在后台中绘制

  

复制代码
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>
复制代码
posted @ 2017-01-12 10:38  吴皓杰  阅读(140)  评论(0编辑  收藏  举报