webform:圖片浮水印、驗證碼製作

來源:互聯網
上載者:User

標籤: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:圖片浮水印、驗證碼製作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.