// Personal Webpage Engine
// Copyright(c) 2008 Mihail Kochanov
// You can contact me at StilgarAD@yahoo.com
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//------------------------------------------------------------------------------
//
// Personal Web Page Engine
//
//------------------------------------------------------------------------------

using System.Drawing;

/// <summary>
/// Used to handle images
/// </summary>
public static class ImageManipulator
{
    public static Bitmap Resize(Image image, int width, int height, bool keepAspectRatio, bool enlargeSmallerImages)
    {
        if (!enlargeSmallerImages && image.Width <= width && image.Height <= height)
        {
            return new Bitmap(image);
        }
        if (!keepAspectRatio)
        {
            return new Bitmap(image, width, height);
        }
        else
        {
            double aspectRatio = image.Width / (double)image.Height;
            double newAspectRatio = width / height;

            if (aspectRatio >= newAspectRatio) //fit horizontally
            {
                double scale = image.Width / (double)width;
                int newHeight = (int)(image.Height / scale);
                return new Bitmap(image, width, newHeight);
            }
            else //fit vertically
            {
                double scale = image.Height / (double)height;
                int newWidth = (int)(image.Width / scale);
                return new Bitmap(image, newWidth, height);
            }
        }
    }
}
