using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
namespace Kingo.Plugin.EngineEditor.Common
{
    public class BitmapScaleHelper
    {
        /// 
        /// 缩放图片
        /// 
        /// 原图片
        /// 新图片宽度
        /// 新图片高度
        /// 新图片
        public Bitmap ScaleToSize(Bitmap bitmap, int width, int height)
        {
            if (bitmap.Width == width && bitmap.Height == height)
            {
                return bitmap;
            }
            var scaledBitmap = new Bitmap(width, height);
            using (var g = Graphics.FromImage(scaledBitmap))
            {
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(bitmap, 0, 0, width, height);
            }
            return scaledBitmap;
        }
        /// 
        /// 缩放图片
        /// 
        /// 原图片
        /// 新图片大小
        /// 新图片
        public Bitmap ScaleToSize(Bitmap bitmap, Size size)
        {
            //return bitmap.ScaleToSize(size.Width, size.Height);
            return ScaleToSize(bitmap, size.Width, size.Height);
        }
        /// 
        /// 按比例来缩放
        /// 
        /// 原图
        /// ratio大于1,则放大;否则缩小
        /// 新图片
        public Bitmap ScaleToSize(Bitmap bitmap, float ratio)
        {
            return ScaleToSize(bitmap, (int)(bitmap.Width * ratio), (int)(bitmap.Height * ratio));
        }
        /// 
        /// 按给定长度/宽度等比例缩放
        /// 
        /// 原图
        /// 新图片宽度
        /// 新图片高度
        /// 新图片
        public Bitmap ScaleProportional(Bitmap bitmap, int width, int height)
        {
            float proportionalWidth, proportionalHeight;
            if (width.Equals(0))
            {
                proportionalWidth = ((float)height) / bitmap.Size.Height * bitmap.Width;
                proportionalHeight = height;
            }
            else if (height.Equals(0))
            {
                proportionalWidth = width;
                proportionalHeight = ((float)width) / bitmap.Size.Width * bitmap.Height;
            }
            else if (((float)width) / bitmap.Size.Width * bitmap.Size.Height <= height)
            {
                proportionalWidth = width;
                proportionalHeight = ((float)width) / bitmap.Size.Width * bitmap.Height;
            }
            else
            {
                proportionalWidth = ((float)height) / bitmap.Size.Height * bitmap.Width;
                proportionalHeight = height;
            }
            return ScaleToSize(bitmap, (int)proportionalWidth, (int)proportionalHeight);
        }
        /// 
        /// 按给定长度/宽度缩放,同时可以设置背景色
        /// 
        /// 原图
        /// 背景色
        /// 新图片宽度
        /// 新图片高度
        /// 
        public Bitmap ScaleToSize(Bitmap bitmap, Color backgroundColor, int width, int height)
        {
            var scaledBitmap = new Bitmap(width, height);
            using (var g = Graphics.FromImage(scaledBitmap))
            {
                g.Clear(backgroundColor);
                var proportionalBitmap = ScaleProportional(bitmap, width, height);
                var imagePosition = new Point((int)((width - proportionalBitmap.Width) / 2m), (int)((height - proportionalBitmap.Height) / 2m));
                g.DrawImage(proportionalBitmap, imagePosition);
            }
            return scaledBitmap;
        }
        /// 
        /// 符号转化为图片
        /// 
        /// 符号
        /// 图片的宽
        /// 图片的高
        /// 
        public Bitmap PreViewFillSymbol(ISymbol pSymbol, int width, int height)
        {
            IStyleGalleryClass pStyleGalleryClass = new FillSymbolStyleGalleryClass();
            Bitmap img = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            Graphics gc = Graphics.FromImage(img);
            IntPtr hdc = gc.GetHdc();
            tagRECT rect = new tagRECT();
            rect.left = 0;
            rect.top = 0;
            rect.right = width;
            rect.bottom = height;
            pStyleGalleryClass.Preview(pSymbol, hdc.ToInt32(), ref rect);
            //gc.Save();
            gc.ReleaseHdc(hdc);
            gc.Dispose();
            img.MakeTransparent(Color.FromArgb(SystemColors.Window.ToArgb()));
            return img.Clone(new Rectangle(0, 0, width, height), System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        }
        /// 
        /// 截取bitmap图像,将图像分成多分,如果selected为空就全部返回
        /// 
        /// 
        /// 截取行数
        /// 截取列数
        /// 选着截取后的第几个,从0开始算
        public Bitmap[] PhotoScreenShot(Bitmap bmpRes, int nXClipNum, int nYClipNum, int[] selected)
        {
            Bitmap[] bmpaClipBmpArr = new Bitmap[nYClipNum * nXClipNum];
            try
            {
                for (int nYClipNumIndex = 0; nYClipNumIndex < nYClipNum; nYClipNumIndex++)
                {
                    for (int nXClipNumIndex = 0; nXClipNumIndex < nXClipNum; nXClipNumIndex++)
                    {
                        int nClipWidth = bmpRes.Width / nXClipNum;
                        int nClipHight = bmpRes.Height / nYClipNum;
                        int nBmpIndex = nXClipNumIndex + nYClipNumIndex * nYClipNum;
                        Rectangle rClipRect = new Rectangle(
                            nClipWidth * nXClipNumIndex,
                            nClipHight * nYClipNumIndex, 
                            nClipWidth,
                            nClipHight);
                        bmpaClipBmpArr[nBmpIndex] = bmpRes.Clone(rClipRect, bmpRes.PixelFormat);
                    }
                }
                if (selected == null || selected.Length == 0)
                {
                    return bmpaClipBmpArr;
                }
                else
                {
                    Bitmap[] temp = new Bitmap[selected.Length];
                    int j = 0;
                    for (int i = 0; i < selected.Length; i++)
                    {
                        temp[j++] = bmpaClipBmpArr[selected[i]];
                    }
                    return temp;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
            }
        }
    }
}