码迷,mamicode.com
首页 > Web开发 > 详细

上传图片加水印

时间:2017-01-07 16:29:27      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:protected   name   save   text   date   using   图片   水印   上传   

上传图片并显示:

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

 

上传图片加水印

标签:protected   name   save   text   date   using   图片   水印   上传   

原文地址:http://www.cnblogs.com/wy1992/p/6259427.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!