Zoe

1、上传图片加水印:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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="上传" />
        <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 _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;
    }

    void Button1_Click(object sender, EventArgs e)
    {
        //获得要上传的图片
        System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent);
        //加上水印
        Graphics g = Graphics.FromImage(img);
        string s="hello word";
        Font f=new Font("微软雅黑",30);
        Brush b=new SolidBrush(Color.Red);
        PointF pf=new PointF(50,50);
        g.DrawString(s,f,b,pf);
        //保存下来
        string path = "upload/" +DateTime.Now.ToString("yyyyMMddhhmmssms")+ FileUpload1.FileName;
        img.Save(Server.MapPath(path));
        Image1.ImageUrl = path;


    }
}

2、验证码制作

 

<%@ 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">
    
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <img src="yzm.aspx" id="yzm"/>
        <asp:Button ID="Button1" runat="server" Text="验证" />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
    
    </form>
</body>
</html>
<script type="text/javascript">
    var a = 0;
    document.getElementById("yzm").onclick = function () {
        this.src = "Default2.aspx?a="+a;
        a++;
    }



</script>

 

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 Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

       
        Button1.Click += Button1_Click;
    }

    void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = Session["Default2"].ToString();
        if (TextBox1.Text == Session["Default2"].ToString())
        {
            Label2.Text = "正确!!!";
        }
        else { Label2.Text = "错误!!"; }
    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="yzm.aspx.cs" Inherits="yzm" %>

<!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>
    
    </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 yzm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<Color> clist = new List<Color>();
        clist.Add(Color.Red);
        clist.Add(Color.Purple);
        clist.Add(Color.Pink);
        clist.Add(Color.Blue);
        clist.Add(Color.Orange);
        clist.Add(Color.Black);
        clist.Add(Color.White);
        clist.Add(Color.Violet);

        Random r = new Random();
        string s = "";
        string all = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        for (int i = 0; i < 4; i++)
        {
            s += all.Substring(r.Next(0, all.Length), 1);
        }
        Session["Default2"] = s;
        Bitmap img = new Bitmap(100, 50);
        Graphics g = Graphics.FromImage(img);
        Brush b = new SolidBrush(clist[r.Next(0, clist.Count)]);
        g.FillRectangle(b, 0, 0, 100, 50);


        Graphics g2 = Graphics.FromImage(img);
        Font f = new Font("微软雅黑", 30);
        Brush b2 = new SolidBrush(Color.Red);
        PointF pf = new PointF(5, 5);
        g2.DrawString(s, f, b2, pf);


        for (int i = 0; i < 7; i++)
        {
            Graphics g3 = Graphics.FromImage(img);
            Pen p3 = new Pen(new SolidBrush(clist[r.Next(0, clist.Count)]), r.Next(2, 5));
            g3.DrawLine(p3, new Point(r.Next(0, 100), r.Next(0, 50)), new Point(r.Next(0, 100), r.Next(0, 50)));
        }
        img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
    }
}

 

posted on 2017-06-11 20:40  口袋里的SKY  阅读(290)  评论(3编辑  收藏  举报