using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

using Security;
namespace Security
{
	public partial class uploadImg : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e){ }

		protected void btnUploadImg_Click(object sender, EventArgs e)
		{
			if (this.fileUpload.HasFile)
			{
				///创建数据连接对象：
				MysqlHelp mh = new MysqlHelp();
				///查询数据库
				DataTable dt=new DataTable();
				dt = mh.GetDatabysql("Select * From Preference4DeepSoon");
				string uploadFileDir = "/UploadFile/";
				string WatermarkLogo = "/System_Img/WatermarkLogo.png";
				if(dt.Rows.Count > 0 ){
					uploadFileDir = dt.Rows[0]["uploadFileDir"].ToString();
					WatermarkLogo = dt.Rows[0]["WatermarkLogo"].ToString();
				}
				
				string fileContentType = this.fileUpload.PostedFile.ContentType;
				if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/jpeg"|| fileContentType == "image/pjpeg")
				{
					string name = this.fileUpload.PostedFile.FileName;  /// 客户端上传文件路径
					FileInfo file = new FileInfo(name);
					string FileType = file.Name.Substring(file.Name.LastIndexOf("."));                   ///文件后缀名   
					string fileName = "old_"+DateTime.Now.ToString("yyyyMMddhhmmssms")+FileType;                /// 上传的文件名称
					string fileName_syp = DateTime.Now.ToString("yyyyMMddhhmmssms")+FileType;   /// 加水印图片（图片）名称
					string webFilePath = Server.MapPath(uploadFileDir + fileName);        /// 服务器端文件路径
					string webFilePath_syp = Server.MapPath(uploadFileDir +  fileName_syp);  /// 服务器端带水印（图片）路径
					string webFilePath_sypf = Server.MapPath(WatermarkLogo);  /// 当做水印的图片
					
					if (!File.Exists(webFilePath))
					{
						try
						{
							this.fileUpload.SaveAs(webFilePath);       /// 暂时保存原始文件
							AddWatermarkPic(webFilePath, webFilePath_syp, webFilePath_sypf); ///给原始图片加图片水印并保存至服务器
							Response.Write("<script type='text/javascript'>");
							Response.Write("window.parent.document.getElementById('imgHref').value = '" + uploadFileDir +fileName_syp+"'");
							Response.Write("</script>");
							File.Delete(MapPath(uploadFileDir + fileName));  ///立即删除原始图片
			
						}catch (Exception ex){}
					}
				}
			}

		}

		/// 加图片水印
		protected void AddWatermarkPic(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();
		}
		
	}
}