标签:saveas tle tip add bool .net doc 加水印 proc
1 原有图片添加水印
1.1 封装一个类,用于获取文件路径
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; namespace _06_图片处理 { public static class FileHelper { public static string GetFilePath() { //02 创建文件保存路径 string savePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Upload\\"); //02-01 根据日期创建文件夹 DateTime dt = DateTime.Now; savePath += dt.Year + "\\" + dt.Month + "\\" + dt.Day; if (!Directory.Exists(savePath)) { //创建文件夹 Directory.CreateDirectory(savePath); } //02-02文 件名为当前时间 //savePath += "\\" + dt.ToString().Replace(‘:‘, ‘-‘) + ".gif"; savePath += "\\" + dt.ToString().Replace(‘:‘, ‘-‘) ; return savePath; } } }
1.2 html页面和ashx页面
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form action="AddLogo.ashx" method="post" enctype="multipart/form-data"> <input type="file" name="OrImg" /> <input type="submit" value="添加水印" /> </form> </body> </html>
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Web; namespace _06_图片处理 { /// <summary> /// AddLogo 的摘要说明 /// </summary> public class AddLogo : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; //01 获取上传图片 HttpPostedFile pf = context.Request.Files["OrImg"]; #region 02 添加水印 //02-01 创建画布 Bitmap bm = new Bitmap(pf.InputStream); //02-02 创建绘图工具 Graphics gs = Graphics.FromImage(bm); //02-03 拿到logo图片 Bitmap bmLogo = new Bitmap(AppDomain.CurrentDomain.BaseDirectory + "/images/LogoYK.GIF"); //02-04 开始绘制 gs.DrawImage(bmLogo,bm.Width-bmLogo.Width,bm.Height-bmLogo.Height,bmLogo.Width,bmLogo.Height); #endregion #region 03 保存 //03-01 获取文件扩展名 string extName = pf.FileName.Substring(pf.FileName.LastIndexOf(‘.‘)); //03-02 获取文件路径 string ph = FileHelper.GetFilePath(); string savePath = ph + extName; //03-03 saveAs bm.Save(savePath); #endregion //04 展示 context.Response.Write("<img src=‘" + savePath.Substring(savePath.IndexOf("Upload")) + "‘/> "); } public bool IsReusable { get { return false; } } } }
1.3 运行效果
标签:saveas tle tip add bool .net doc 加水印 proc
原文地址:http://www.cnblogs.com/YK2012/p/7011291.html