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

namespace LJLib.Tools.Utils
{
    public static class ImgHelper
    {
        public static Bitmap NewBMPFrom(Image img, int width, int height)
        {
            var oImg = new Bitmap(width, height);
            using (var g = Graphics.FromImage(oImg))
            {
                var srcRect = new Rectangle(0, 0, img.Width, img.Height);
                var dstRect = new Rectangle();
                if (srcRect.Width != oImg.Width || srcRect.Height != oImg.Height)
                {
                    if (srcRect.Width * oImg.Height > srcRect.Height * oImg.Width) // 按宽度拉伸
                    {
                        dstRect.Width = oImg.Width;
                        dstRect.Height = dstRect.Width * srcRect.Height / srcRect.Width;
                        dstRect.X = 0;
                        dstRect.Y = (oImg.Height - dstRect.Height) / 2;
                    }
                    else // 按高度拉伸
                    {
                        dstRect.Height = oImg.Height;
                        dstRect.Width = dstRect.Height * srcRect.Width / srcRect.Height;
                        dstRect.X = (oImg.Width - dstRect.Width) / 2;
                        dstRect.Y = 0;
                    }
                }
                else // 不拉伸
                {
                    dstRect.Width = oImg.Width;
                    dstRect.Height = oImg.Height;
                    dstRect.X = 0; //(oImg.Width - dstRect.Width) / 2;
                    dstRect.Y = 0; //(oImg.Height - dstRect.Height) / 2;
                }

                var transparent = Color.FromArgb(0, 255, 255, 255);

                g.Clear(transparent);
                g.DrawImage(img, dstRect, srcRect, GraphicsUnit.Pixel);
                g.Save();
                oImg.MakeTransparent(transparent);
            }
            return oImg;
        }

        /// <summary>
        /// Byte数组 转 Image
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static Image BytesToImage(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer);
            Image image = System.Drawing.Image.FromStream(ms);
            return image;
        }

        /// <summary>
        /// Image 转 Byte数组
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static byte[] ImageToBytes(Image image)
        {
            ImageFormat format = image.RawFormat;
            using (MemoryStream ms = new MemoryStream())
            {
                if (format.Equals(ImageFormat.Jpeg))
                {
                    image.Save(ms, ImageFormat.Jpeg);
                }
                else if (format.Equals(ImageFormat.Png))
                {
                    image.Save(ms, ImageFormat.Png);
                }
                else if (format.Equals(ImageFormat.Bmp))
                {
                    image.Save(ms, ImageFormat.Bmp);
                }
                else if (format.Equals(ImageFormat.Gif))
                {
                    image.Save(ms, ImageFormat.Gif);
                }
                else if (format.Equals(ImageFormat.Icon))
                {
                    image.Save(ms, ImageFormat.Icon);
                }
                byte[] buffer = new byte[ms.Length];
                //Image.Save()会改变MemoryStream的Position，需要重新Seek到Begin
                ms.Seek(0, SeekOrigin.Begin);
                ms.Read(buffer, 0, buffer.Length);
                return buffer;
            }
        }

        /// <summary>
        /// 添加水印    
        /// </summary>
        /// <param name="image">处理图片</param>
        /// <param name="addText">添加文字</param>
        /// <param name="color">文字颜色</param>
        /// <param name="font">文字字体</param>
        /// <param name="pos_x">文字位置--x</param>
        /// <param name="pos_y">文字位置--y</param>
        /// <returns></returns>
        public static Image AddWaterMark(Image image, string addText, Color color, Font font, string pos_x, string pos_y) //添加文本水印函数
        {
            try
            {
                Graphics g = Graphics.FromImage(image);
                g.DrawImage(image, 0, 0, image.Width, image.Height);  //填充原图
                Brush b = new SolidBrush(color);  //获取颜色
                g.DrawString(addText, font, b, Convert.ToInt16(pos_x), Convert.ToInt16(pos_y));  //在原图中加上水印文字
                g.Dispose();
                return image;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        /// <summary>
        /// Byte数组 图片添加水印 返回 Byte数组
        /// </summary>
        /// <param name="byteArray"></param>
        /// <param name="addText"></param>
        /// <param name="color"></param>
        /// <param name="font"></param>
        /// <param name="pos_x"></param>
        /// <param name="pos_y"></param>
        /// <returns></returns>
        public static Byte[] AddWaterMarkWithByte(Byte[] byteArray, string addText, Color color, Font font, string pos_x, string pos_y) //添加文本水印函数
        {
            try
            {
                var image = ImgHelper.BytesToImage(byteArray);
                Image newimage = ImgHelper.AddWaterMark(image, addText, color, font, pos_x, pos_y);
                return ImageToBytes(newimage);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
       
    }
}
