using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Data;


using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace Fm.Bussiness
{
    public class Photos : System.Web.UI.Page
    {
        public Photos(){ }
        /// <summary>
        /// 图片水印功能操作方法 
        /// </summary>
        /// <param name="path">需要添加水印图片的地址</param>
        /// <param name="dt">水印设置参数集</param>
        /// <returns></returns>
        public string Create_tupian(string path,DataTable dt)
        {
            return "";
 
        }
        /// <summary>
        /// 设置文字水印
        /// </summary>
        /// <param name="path">图片路径</param>
        /// <param name="str">水印文字</param>
        public void Create_wenzi(string path,DataTable dt)
        {
          
           //获取文件扩展名
            string kz_name = Path.GetExtension(path).ToUpper();
            //设置新的文件名
            string filename = DateTime.Now.ToShortDateString()+DateTime.Now.ToShortTimeString() + new Random().Next(1000,9999).ToString();
            filename = filename.Replace("-","");
            filename = filename.Replace(":","");
            //加文字水印
            Image img = Image.FromFile(path);
            Graphics gs = Graphics.FromImage(img);
            gs.DrawImage(img, 10, 10, img.Width, img.Height);
            Font font = new Font("宋体", 12);
            Brush br = new SolidBrush(Color.White);
            gs.DrawString(dt.Rows[0]["sy_wenzi"].ToString(), font, br, 12, 12);
            gs.Dispose();
            //保存添加水印后的图片
            //string newpath = HttpContext.Current.Server.MapPath("~/") + dt.Rows[0]["up_path"].ToString()+"/"+filename+kz_name;
            img.Save(path);
            img.Dispose();
           
            //替换原有图片
            //File.Copy(small_path, path, true);
            //删除水印
            //if (File.Exists(path))
            //{
            //    File.Delete(path);
            //}
          

        }
        /// <summary>
        /// 创建缩略图
        /// </summary>
        /// <param name="path">要设置缩略图的路径</param>
        public string WaterLetter(string path,int width,int height)
        {
            //获取文件扩展名
            string kz_name = Path.GetExtension(path).ToLower();
            //设置新的文件名 在原来的文件名
            string filename = Path.GetFileName(path);
            //开始生成缩略图
            Image.GetThumbnailImageAbort call = null;
            Image flagimg = Image.FromFile(path);

            Image small = flagimg.GetThumbnailImage(width, height, call, new System.IntPtr());

            string ph = path.Replace(filename,"");
            //获取新的存放路径
            string file = DateTime.Now.ToShortDateString().Replace("-", "") + DateTime.Now.ToShortTimeString().Replace(":", "");
            string small_path = ph +"thumbnail_"+ file + kz_name;
            //保存缩略图
            small.Save(small_path);
            //释放资源
            flagimg.Dispose();
            small.Dispose();
            return "/"+small_path.Replace(Server.MapPath("~/"),"");
        }
    }
}
