using System;

namespace Hitsuji.Framework.Utils
{
    public class ReSizeImageUtil
    {
        /// <summary>
        /// 幅、高さ を指定してリサイズ
        /// </summary>
        /// <param name="image"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static System.Drawing.Image reSizeImage(System.Drawing.Image image, 
                                                       int width,
                                                       int height)
        {
            var bitmap = new System.Drawing.Bitmap(width, height);
            //var graph = System.Drawing.Graphics.FromImage(bitmap);
            using (var graph = System.Drawing.Graphics.FromImage(bitmap))
            {
                graph.FillRectangle(System.Drawing.Brushes.White, 0, 0, width, height);
                graph.DrawImage(image, 0, 0, width, height);

                return bitmap;
            }
        }

        /// <summary>
        /// 拡大率を指定してリサイズ
        /// </summary>
        /// <param name="image"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static System.Drawing.Image reSizeImage(System.Drawing.Image image,
                                                        double multiply)
        {
            try
            {
                int width = (int)Math.Ceiling(image.Width * multiply);
                int height = (int)Math.Ceiling(image.Height * multiply);

                return reSizeImage(image, width, height);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {

            }
        }

        /// <summary>
        /// 幅、高さ の最小サイズを指定してリサイズ
        /// </summary>
        /// <param name="image"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static System.Drawing.Image reSizeImageMinLengthLimited(System.Drawing.Image image,
                                                                       int width,
                                                                       int height)
        {
            try
            {
                //縦・横の各サイズのうち、指定した長さに設定したときにより割合の小さくなるほうを取得
                double widPer = (double)width / (double)image.Width;
                double heiPer = (double)height / (double)image.Height;
                double shortPer = widPer < heiPer ? widPer : heiPer;
                int newWidth = (int)Math.Ceiling(image.Width * shortPer);
                int newHeight = (int)Math.Ceiling(image.Height * shortPer);

                return reSizeImage(image, newWidth, newHeight);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {

            }
        }

        /// <summary>
        /// 幅、高さ の最大サイズを指定してリサイズ
        /// </summary>
        /// <param name="image"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static System.Drawing.Image reSizeImageMaxLengthLimited(System.Drawing.Image image,
                                                                       int width,
                                                                       int height)
        {
            //縦・横の各サイズのうち、指定した長さに設定したときにより割合の小さくなるほうを取得
            double widPer = (double)width / (double)image.Width;
            double heiPer = (double)height / (double)image.Height;
            double largePer = widPer > heiPer ? widPer : heiPer;
            int newWidth = (int)Math.Floor(image.Width * largePer);
            int newHeight = (int)Math.Floor(image.Height * largePer);

            var img = reSizeImage(image,
                newWidth <= 0 ? 1 : newWidth,
                newHeight <= 0 ? 1 : newHeight);
            return img;
        }

    }
}
