webform文件上传加水印
1.文件上传加水印
页面代码:
后台代码:
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) { if (FileUpload1.FileName == "") { Label1.Text = "上传的文件不能为空"; return; } //选择上传的图片 if (FileUpload1.PostedFile.ContentLength > (4 * 1024 * 1024)) { Label1.Text = "文件过大,不能上传"; } else { System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent); //绘制水印 Graphics g = Graphics.FromImage(img); //添加的水印 string s = "你好"; //绘制的文字样式 Font f = new Font("微团雅黑", 30); //绘制的颜色 Brush b = new SolidBrush(Color.Red); //绘制开始的坐标 PointF p = new PointF(50, 50); g.DrawString(s, f, b, p); //上传 //路径 string path = "Upload/" + DateTime.Now.ToString("yyyyMMddmmssms") + FileUpload1.FileName; Label1.Text = "上传成功"; //上传的方法 img.Save(Server.MapPath(path)); //上传的文件在页面中显示 Image1.ImageUrl = path; } } }