/* 调用代码
UploadFile upload = new UploadFile();
upload.Path = "upload";
upload.Sizes = 2048;
upload.FileType = "jpg|gif|bmp";
upload.PostedFile = FileUpload1.PostedFile;
Label1.Text = upload.Upload();
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace Subtext.Framework
{
    public class UploadFile
    {
        private string path = null;
        private string fileType = null;
        private int sizes = 0;
        private HttpPostedFile postedFile = null;

        public HttpPostedFile PostedFile
        {
            get
            {
                return postedFile;
            }
            set
            {
                postedFile = value;
            }
        }
        /// <summary>
        /// 设置上传路径,如:upload
        /// </summary>
        public string Path
        {
            get
            {
                return path;
            }
            set
            {
                path = @"\" + value + @"\";
            }
        }

        /// <summary>
        /// 设置上传文件大小,单位为KB
        /// </summary>
        public int Sizes
        {
            get
            {
                return sizes;
            }
            set
            {
                sizes = value * 1024;
            }
        }

        /// <summary>
        /// 上传文件的类型
        /// </summary>
        public string FileType
        {
            get
            {
                return fileType;
            }
            set
            {
                fileType = value;
            }
        }
        public string PathToName(string path)
        {
            int pos = path.LastIndexOf("\\");
            return path.Substring(pos + 1);
        }

        /// <summary>
        /// 上传
        /// </summary>
        public string Upload()
        {
            try
            {
                string filePath = null;
                //以当前时间修改图片的名字或创建文件夹的名字
                string modifyFileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
                //获得站点的物理路径
                string uploadFilePath = null;

                uploadFilePath = System.Web.HttpContext.Current.Server.MapPath("~/") + path;

                //获得文件的上传的路径
                string sourcePath = PathToName(PostedFile.FileName);
                //判断上传文件是否为空
                if (sourcePath == "" || sourcePath == null)
                {
                    HttpContext.Current.Response.Write("<span style='font-size:12px;'><a href='attachment.aspx'>您还没有上传数据</a></span>");
                    HttpContext.Current.Response.End();
                    return null;
                }
                //获得文件扩展名
                string tFileType = sourcePath.Substring(sourcePath.LastIndexOf(".") + 1);
                //获得上传文件的大小
                long strLen = PostedFile.ContentLength;
                //分解允许上传文件的格式
                string[] temp = fileType.Split('|');
                //设置上传的文件是否是允许的格式
                bool flag = false;
                //判断上传文件大小
                if (strLen >= sizes)
                {
                    HttpContext.Current.Response.Write("<span style='font-size:12px;'><a href='attachment.aspx'>上传的图片不能大于" + sizes + "KB</a></span>");
                    HttpContext.Current.Response.End();
                    return null;
                }
                //判断上传的文件是否是允许的格式
                foreach (string data in temp)
                {
                    if (data == tFileType)
                    {
                        flag = true;
                        break;
                    }
                }
                //如果为真允许上传,为假则不允许上传
                if (!flag)
                {
                    HttpContext.Current.Response.Write("<span style='font-size:12px;'><a href='attachment.aspx'>支持的文件格式为:" + fileType + "</a></span>");
                    HttpContext.Current.Response.End();
                    return null;
                }
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(uploadFilePath);
                //判断文件夹否存在,不存在则创建
                if (!dir.Exists)
                {
                    dir.Create();
                }
                filePath = uploadFilePath + modifyFileName + "." + tFileType;
                PostedFile.SaveAs(filePath);
                filePath = path + modifyFileName + "." + tFileType;
                return filePath.Replace(@"\", "/");
            }
            catch
            {
                //异常
                HttpContext.Current.Response.Write("<span style='font-size:12px;'><a href='attachment.aspx'>出现未知错误</a></span>");
                HttpContext.Current.Response.End();
                return null;
            }
        }

        #region 在图片上增加文字水印
        /// <summary>
        /// 在图片上增加文字水印
        /// </summary>
        /// <param name="Path">原服务器图片路径</param>
        /// <param name="Path_sy">生成的带文字水印的图片路径</param>
        public static void AddWater(string Path, string Path_sy)
        {
            string addText = "水印文字";
            System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
            g.DrawImage(image, 0, 0, image.Width, image.Height);
            System.Drawing.Font f = new System.Drawing.Font("宋体", 5);
            System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Gray);

            g.DrawString(addText, f, b, 5, image.Height - 20);
            g.Dispose();

            image.Save(Path_sy);
            image.Dispose();
        }
        #endregion

        #region 在图片上生成图片水印
        /// <summary>
        /// 在图片上生成图片水印
        /// </summary>
        /// <param name="Path">原服务器图片路径</param>
        /// <param name="Path_syp">生成的带图片水印的图片路径</param>
        /// <param name="Path_sypf">水印图片路径</param>
        public static void AddWaterPic(string Path, string Path_syp, string Path_sypf)
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
            System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
            g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
            g.Dispose();

            image.Save(Path_syp);
            image.Dispose();
        }
        #endregion
    }
}
