using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace ATL.Picture
{
    public class Images
    {
        /// <summary>
        /// 图片水印处理方法
        /// </summary>
        /// <param name="path">需要加载水印的图片路径（绝对路径）</param>
        /// <param name="waterpath">水印图片（绝对路径）</param>
        /// <param name="location">水印位置（传送正确的代码）</param>
        /// <returns></returns>
        public string ImageWatermark(string path,string waterpath,string location)
        {
            //获取文件扩展名
            string kz_name = Path.GetExtension(path);
            //暂时只支持给.JPG,.BMP,.JPEG格式加水印
            if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg")
            {
                //设置新的文件名
                DateTime time = DateTime.Now;
                string filename = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString();
                //加载需要加载水印的图片
                Image img = Bitmap.FromFile(path);
                //加载水印图片
                Image waterimg = Image.FromFile(waterpath);

                //添加水印
                Graphics g = Graphics.FromImage(img);
                //获取水印位置设置
                ArrayList loca = GetLocation(location, img, waterimg);
                g.DrawImage(waterimg,new Rectangle(int.Parse(loca[0].ToString()),int.Parse(loca[1].ToString()),waterimg.Width,waterimg.Height));
                //释放资源
                waterimg.Dispose();
                g.Dispose();
                //保存水印图片
                string newpath = Path.GetDirectoryName(path) + filename + kz_name;
                img.Save(newpath);
                img.Dispose();
                //将水印复制到原有图片
                //将水印图片替换原有图片
                File.Copy(newpath, path, true);
                //删除水印
                if (File.Exists(newpath))
                {
                    File.Delete(newpath);
                }
            }
            return path;
        }
       /// <summary>
       /// 图片水印位置处理方法
       /// </summary>
       /// <param name="location">水印位置</param>
       /// <param name="img">需要添加水印的图片</param>
       /// <param name="waterimg">水印图片</param>
       /// <returns></returns>
        private ArrayList GetLocation(string location, Image img, Image waterimg)
        {
            //定义数组存储位置
            ArrayList loca = new ArrayList();
            int x = 0;
            int y = 0;

            if (location == "LT")
            {
                x = 10;
                y = 10;
            }
            else if (location == "T")
            {
                x = img.Width / 2 - waterimg.Width / 2;
                y = img.Height - waterimg.Height;
            }
            else if (location == "RT")
            {
                x = img.Width - waterimg.Width;
                y = 10;

            }
            else if (location == "LC")
            {

                x = 10;
                y = img.Height / 2 - waterimg.Height / 2;

            }
            else if (location == "C")
            {
                x = img.Width / 2 - waterimg.Width / 2;
                y = img.Height / 2 - waterimg.Height / 2;
            }
            else if (location == "RC")
            {
                x = img.Width - waterimg.Width;
                y = img.Height / 2-waterimg.Height/2;
            }
            else if (location == "LB")
            {
                x = 10;
                y = img.Height - waterimg.Height;
            }
            else if (location == "B")
            {
                x = img.Width / 2 - waterimg.Width / 2;
                y = img.Height - waterimg.Height;
            }
            else
            {
                x = img.Width - waterimg.Width;
                y = img.Height - waterimg.Height;
            }
            loca.Add(x);
            loca.Add(y);
            return loca;
        }
        /// <summary>
        /// 文字水印处理方法
        /// </summary>
        /// <param name="path">图片路径（绝对路径）</param>
        /// <param name="size">字体大小</param>
        /// <param name="letter">水印文字</param>
        /// <param name="color">颜色</param>
        /// <param name="location">水印位置</param>
        /// <returns></returns>
        public string LetterWatermark(string path, int size,string letter,Color color,string location)
        {

            //获取文件扩展名
            string kz_name = Path.GetExtension(path);
            if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg")
            {
                //设置新的文件名
                DateTime time = DateTime.Now;
                string filename = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString();
                //加文字水印

                Image img = Bitmap.FromFile(path);


                Graphics gs = Graphics.FromImage(img);
                //获取X,Y坐标
                ArrayList loca = GetLocation(location, img,size,letter.Length);
                //gs.DrawImage(img, new Rectangle(loca[0], loca[1], 150, 150));
                Font font = new Font("宋体", size);
                Brush br = new SolidBrush(color);
                gs.DrawString(letter, font, br,float.Parse(loca[0].ToString()),float.Parse(loca[1].ToString()));
                gs.Dispose();
                //保存添加水印后的图片

                string newpath = Path.GetDirectoryName(path) + filename + kz_name;
                img.Save(newpath);
                img.Dispose();
                //将水印图片替换原有图片
                File.Copy(newpath, path, true);
                //删除水印
                if (File.Exists(newpath))
                {
                    File.Delete(newpath);
                }
            }
            return path;
        }
        /// <summary>
        /// 处理文字水印位置的方法
        /// </summary>
        /// <param name="location">位置代码</param>
        /// <param name="img">图片对象</param>
        /// <param name="width">宽(当水印类型为文字时,传过来的就是字体的大小)</param>
        /// <param name="height">高(当水印类型为文字时,传过来的就是字符的长度)</param>
        /// <returns></returns>
        private ArrayList GetLocation(string location,Image img,int width,int height)
        {
            //定义数组存储位置
            ArrayList loca = new ArrayList();
            float x = 10;
            float y = 10;

            if (location == "LT")
            {
                loca.Add(x);
                loca.Add(y);
            }
            else if (location == "T")
            {
                x = img.Width / 2 - (width * height) / 2;
                loca.Add(x);
                loca.Add(y);
            }
            else if (location == "RT")
            {
                x = img.Width - width * height;
            }
            else if (location == "LC")
            {
                y = img.Height / 2;
            }
            else if (location == "C")
            {
                x = img.Width / 2 - (width * height) / 2;
                y = img.Height / 2;               
            }
            else if (location == "RC")
            {
                x = img.Width - height;
                y = img.Height / 2;
            }
            else if (location == "LB")
            {
                y = img.Height - width-5;
            }
            else if (location == "B")
            {
                x = img.Width / 2 - (width * height) / 2;
                y = img.Height - width-5;
            }
            else 
            {
                x = img.Width - width * height;
                y = img.Height - width-5;
            }         
            loca.Add(x);
            loca.Add(y);
            return loca;
        }
        /// <summary>
        /// 缩略图处理方法
        /// </summary>
        /// <param name="path"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public string Thumbnail(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 thum_path = ph + "thumbnail_" + file + kz_name;
            //保存缩略图
            small.Save(thum_path);
            //释放资源
            flagimg.Dispose();
            small.Dispose();
            return thum_path;
        }

    }
}
