上传图片加水印

上传图片并显示:

aspx中:

<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>

cs中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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)
    {
        string p = "pic/" + DateTime.Now.ToString("yyyyMMddhhmmssma") + FileUpload1.FileName;
        string path=Server.MapPath(p);
        FileUpload1.SaveAs(path);//绝对路径
        Image1.ImageUrl = p;//相对路径
    }
}

上传图片加水印:
aspx与上相同;

cs代码:添加using drawing类

using System;
using System.Collections.Generic;
using System.Drawing;
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)
    {
        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 ss = "pic/" + DateTime.Now.ToString("yyyyMMddhhmmssms") + FileUpload1.FileName;

        img.Save(Server.MapPath(ss));
        Image1.ImageUrl = ss;
    }
}

 

posted @ 2017-01-07 14:59  游称  阅读(169)  评论(0编辑  收藏  举报