Webform 上传图片加水印

时间:2024-12-04 18:33:38

界面:

 <%@ 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>
<style type="text/css">
#Image1 {
width: 500px;
}
</style>
</head>
<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>
</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 = "汉企奇点网络0928";
Font f = new Font("微软雅黑",);//字体
Brush b = new SolidBrush(Color.Red);//颜色
//所在画布位置
PointF p = new PointF(,);
PointF p1 = new PointF(, );
PointF p2 = new PointF(, );
PointF p3 = new PointF(, ); g.DrawString(s, f, b, p);
g.DrawString(s, f, b, p1);
g.DrawString(s, f, b, p2);
g.DrawString(s, f, b, p3); string sss = "images/"+DateTime.Now.ToString("yyyyMMddhhmmssms")+FileUpload1.FileName;
//保存位置
img.Save(Server.MapPath(sss));
//显示图片
Image1.ImageUrl = sss;
}
}

后台:

Webform 上传图片加水印