水印.jpg
WebFormUpload.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebFormUpload.aspx.cs" Inherits="Study.WebFormUpload" %>
<!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 method="post" action="WebFormUpload.aspx" enctype="multipart/form-data" >
<input type="hidden" name="isPostBack" value="POST" />
<input type="file" name="fileUp" />
<input type="text" name="waterName" />
<input type="submit" value="上传" />
</form>
</body>
</html>
WebFormUpload.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
namespace Study
{
public partial class WebFormUpload : System.Web.UI.Page
{
//目录
public string Dir { get; set; }
//上传文件的完整路径
public string FullDir { get; set; }
//获取要添加的水印文字
public string WaterName { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
//HttpFileCollection httpFileCollection = Request.Files;
if (!string.IsNullOrEmpty(Request.Form["isPostBack"]))
{
this.WaterName = Request.Form["waterName"];
SaveFile();
MakeWaterImage(Dir,FullDir,WaterName);
MakeThumbnailImage(Dir,FullDir,300, 200);
}
}
protected void SaveFile()
{
HttpPostedFile file = Request.Files[0];
if (file.ContentLength>0)
{
//对上传的文件类型进行校验
string fileName = Path.GetFileName(file.FileName);//获取上传文件的名称,包含扩展名
string fileExt = Path.GetExtension(fileName);//获取上传文件扩展名
List<string> Extension = new List<string>()
{
".jpg",
".png",
".bmp",
".gif",
".jpeg"
};
if (Extension.Contains(fileExt))
{
//1.对上传文件进行重命名
string newFileName = Guid.NewGuid().ToString();
//2.将上传的文件放在不同目录下
this.Dir = "upload/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
//创建文件夹
if (!Directory.Exists(Request.MapPath(this.Dir)))
{
Directory.CreateDirectory(Request.MapPath(this.Dir));
}
this.FullDir = this.Dir + newFileName + fileExt;//文件存放的完整路径
file.SaveAs(Request.MapPath(FullDir));//保存原始文件
}
else
{
Response.Write("只能上传图片文件");
}
}
else
{
Response.Write("请选择上传文件");
}
}
protected void MakeWaterImage(string dir,string fullDir,string waterName)
{
//创建图片水印
using (System.Drawing.Image img = System.Drawing.Image.FromFile(Request.MapPath(fullDir)))
{
using (Bitmap map = new Bitmap(img.Width, img.Height)) //根据图片的高度和宽度创建画布
{
using (Graphics g = Graphics.FromImage(map))
{
//先用画笔在画布上画图,从画布的左上角开始画,画图大小是上传图片的大小
g.DrawImage(img, 0, 0, img.Width, img.Height);
//再用画笔在画面上画文字水印
g.DrawString(WaterName, new Font("微软雅黑", 15.0f, FontStyle.Regular), Brushes.Red, new PointF(img.Width / 2, img.Height / 3));
string waterImageName = "WaterMark-" + Guid.NewGuid().ToString();
//保存水印图片
map.Save(Request.MapPath(dir + waterImageName + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
//展示水印图片
Response.Write("<html><body>![]("+dir + waterImageName + ".jpg)<body></html>");
}
}
}
}
protected void MakeThumbnailImage(string dir,string fullDir,int width,int height)
{
//创建缩略图
using (System.Drawing.Image img = System.Drawing.Image.FromFile(Request.MapPath(fullDir)))
{
using (Bitmap map = new Bitmap(width, height))
{
using (Graphics g = Graphics.FromImage(map))
{
g.DrawImage(img, 0, 0, map.Width, map.Height);
string thumbnailFileName = "Thumbnail-" + Guid.NewGuid().ToString();
map.Save(Request.MapPath(dir + thumbnailFileName + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
Response.Write("<html><body>![](" + dir + thumbnailFileName + ".jpg)<body></html>");
}
}
}
}
}
}