Asp.net 文件上传基类(取得文件后缀名,保存文件,加入文字水印)

  1.   
  2. using System;
  3. using System.Data;
  4. using System.Configuration;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Drawing;
  12. using System.IO;
  13. using System.Drawing.Imaging;
  14. namespace EC
  15. {
  16.     /// <summary>
  17.     /// 上传类
  18.     /// </summary>
  19.     public class UploadObj
  20.     {
  21.         public UploadObj()
  22.         {
  23.             //
  24.             // TODO: 在此处添加构造函数逻辑
  25.             //
  26.         }
  27.         /// <summary>
  28.         /// 允许文件上传的类型枚举
  29.         /// </summary>
  30.         public enum FileType
  31.         {
  32.            jpg,gif,bmp,png            
  33.         }
  34.         #region 取得文件后缀
  35.         /// <summary>
  36.         /// 取得文件后缀
  37.         /// </summary>
  38.         /// <param name="filename">文件名称</param>
  39.         /// <returns></returns>
  40.         public static string GetFileExtends(string filename)
  41.         {
  42.             string ext = null;
  43.             if (filename.IndexOf('.') > 0)
  44.             {
  45.                 string[] fs = filename.Split('.');
  46.                 ext = fs[fs.Length - 1];
  47.             }
  48.             return ext;
  49.         }
  50.         #endregion
  51.         #region 检测文件是否合法
  52.         /// <summary>
  53.         /// 检测上传文件是否合法
  54.         /// </summary>
  55.         /// <param name="fileExtends">文件后缀名</param>
  56.         /// <returns></returns>
  57.         public static bool CheckFileExtends(string fileExtends)
  58.         {
  59.             bool status = false;
  60.             fileExtends = fileExtends.ToLower();
  61.             string[] fe = Enum.GetNames(typeof(FileType));
  62.             for (int i = 0; i < fe.Length; i++)
  63.             {
  64.                 if (fe[i].ToLower() == fileExtends)
  65.                 {
  66.                     status = true;
  67.                     break;
  68.                 }
  69.             }
  70.             return status;
  71.         }
  72.         #endregion
  73.         #region 保存文件
  74.        /// <summary>
  75.        /// 保存文件
  76.        /// </summary>
  77.        /// <param name="fpath">全路径,Server.MapPath()</param>
  78.        /// <param name="myFileUpload">上传控件</param>
  79.        /// <returns></returns>
  80.         public static string PhotoSave(string fpath,FileUpload myFileUpload)
  81.         {   
  82.             string s = "";
  83.             string fileExtends = "";
  84.             string fileName = myFileUpload.FileName;
  85.             if (fileName != "")
  86.             {
  87.                 //取得文件后缀
  88.                 fileExtends = EC.UploadObj.GetFileExtends(fileName);
  89.                 if (!EC.UploadObj.CheckFileExtends(fileExtends))
  90.                 {
  91.                     EC.MessageObject.ShowPre("上传文件类型不合法");                  
  92.                 }
  93.                 Random rd = new Random();
  94.                 s = EC.RandomObject.DateRndName(rd) + "." + fileExtends;
  95.                 string file = fpath + "//" + s;
  96.                 try
  97.                 {
  98.                     myFileUpload.SaveAs(file);
  99.                 }
  100.                 catch (Exception ee)
  101.                 {
  102.                     throw new Exception(ee.ToString());
  103.                 }
  104.             }
  105.             return s;
  106.         }
  107.         #endregion
  108.         #region 加入文字水印
  109.         /// <summary>
  110.         /// 加入文字水印
  111.         /// </summary>
  112.         /// <param name="fileName">文件名称路径(全路径)</param>
  113.         /// <param name="text">文件</param>
  114.         public void AddTextToImg(string fileName, string text)
  115.         {
  116.             if (!File.Exists(fileName))
  117.             {
  118.                 throw new FileNotFoundException("文件不存在");
  119.             }
  120.             if (text == string.Empty)
  121.             {
  122.                 return;
  123.             }
  124.             //判断文件类型是否为图像类型
  125.             System.Drawing.Image image = System.Drawing.Image.FromFile(fileName);
  126.             Bitmap bitmap = new Bitmap(image, image.Width, image.Height);
  127.             Graphics g = Graphics.FromImage(bitmap);
  128.             float fontSize = 12.0f;//字体大小
  129.             float textWidth = text.Length * fontSize;//文本的长度
  130.             //下面定义一个矩形区域,以后在这个矩形里面画上白底黑字
  131.             float rectX = 0;
  132.             float rectY = 0;
  133.             float rectWidth = text.Length * (fontSize + 8);
  134.             float rectHeight = fontSize + 8;
  135.             //声明矩形域
  136.             RectangleF textArea = new RectangleF(rectX, rectY, rectWidth, rectHeight);
  137.             Font font = new Font("宋体", fontSize);//定义字体
  138.             Brush whiteBrush = new SolidBrush(Color.White);//白笔刷,画文字用
  139.             Brush blackBrush = new SolidBrush(Color.Black);//黑笔刷,画背景用
  140.             g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight);
  141.             g.DrawString(text, font, whiteBrush, textArea);
  142.             MemoryStream ms = new MemoryStream();
  143.             bitmap.Save(ms, ImageFormat.Jpeg);
  144.             //输出处理后的图像,这里为了演示方便,我将图片显示在页面中了
  145.             //Response.Clear();
  146.             //Response.ContentType = "image/jpeg";
  147.             //Response.BinaryWrite(ms.ToArray());
  148.             g.Dispose();
  149.             bitmap.Dispose();
  150.             image.Dispose();        
  151.         }
  152.         #endregion
  153.     }
  154. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值