#region COPYRIGHT
//
//     THIS IS GENERATED BY TEMPLATE
//     
//     AUTHOR  :     ROYE
//     DATE       :     2010
//
//     COPYRIGHT (C) 2010, TIANXIAHOTEL TECHNOLOGIES CO., LTD. ALL RIGHTS RESERVED.
//
#endregion

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

namespace System.Drawing
{
    public static class ImageHelper
    {
        public static Image CreateThumbnail(Stream fileStream, int width, int height, Color penColor)
        {
            Bitmap bitmap;
            try
            {
                bitmap = new Bitmap(fileStream);
            }
            catch
            {
                bitmap = new Bitmap(width, height);
            }
            return CreateThumbnail(bitmap, width, height, penColor);
        }

        public static Image CreateThumbnail(string fileName, int width, int height, Color penColor)
        {
            Bitmap bitmap;
            try
            {
                bitmap = new Bitmap(fileName);
            }
            catch
            {
                bitmap = new Bitmap(width, height);
            }
            return CreateThumbnail(bitmap, width, height, penColor);
        }

        public static Image CreateThumbnail(Image image, int width, int height, Color penColor)
        {
            Bitmap bitmap;
            try
            {
                bitmap = new Bitmap(image);
            }
            catch
            {
                bitmap = new Bitmap(width, height);
            }
            return CreateThumbnail(bitmap, width, height, penColor);
        }

        private static Image CreateThumbnail(Bitmap bitmap, int width, int height, Color penColor)
        {
            width = bitmap.Width > width ? width : bitmap.Width;
            height = bitmap.Height > height ? height : bitmap.Height;

            Bitmap thumbnail = new Bitmap(width, height, PixelFormat.Format64bppPArgb);
            using (Graphics g = Graphics.FromImage(thumbnail))
            {
                int tnWidth = width;
                int tnHeight = height;

                if (bitmap.Width > bitmap.Height)
                {
                    tnHeight = (int)(((float)bitmap.Height / (float)bitmap.Width) * tnWidth);
                }
                else if (bitmap.Width < bitmap.Height)
                {
                    tnWidth = (int)(((float)bitmap.Width / (float)bitmap.Height) * tnHeight);
                }

                int iLeft = (width / 2) - (tnWidth / 2);
                int iTop = (height / 2) - (tnHeight / 2);

                g.PixelOffsetMode = PixelOffsetMode.None;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                g.DrawImage(bitmap, iLeft, iTop, tnWidth, tnHeight);

                using (Pen pen = new Pen(penColor, 1)) //Color.Wheat
                {
                    g.DrawRectangle(pen, 0, 0, thumbnail.Width - 1, thumbnail.Height - 1);
                }
                return thumbnail;
            }
        }
    }
}
