view Chronosv2/source/Extensions/ImageExtensions.cs @ 12:6a0449185449

SCC changed from TFS to HG
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:47:35 +0700
parents 443821e55f06
children
line wrap: on
line source

using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Chronos.Extensions
{
    public static class ImageExtensions
    {
        #region · Extensions ·

        /// <summary>
        /// Resizes the specified image.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="size">The size.</param>
        /// <returns></returns>
        public static Image Resize(this Image image, Size size)
        {
            Size newSize = new Size(size.Width, size.Height);

            if (image.Size.Width > image.Size.Height)
            {
                newSize.Height = (image.Size.Height * size.Width) / image.Size.Width;
            }
            else
            {
                newSize.Width = (image.Size.Width * size.Height) / image.Size.Height;
            }

            Rectangle   rectangle   = new Rectangle(0, 0, newSize.Width, newSize.Height);
            Image       resized     = new Bitmap(newSize.Width, newSize.Height, image.PixelFormat);

            using (Graphics graphic = Graphics.FromImage(resized))
            {
                graphic.CompositingQuality  = CompositingQuality.HighQuality;
                graphic.SmoothingMode       = SmoothingMode.HighQuality;
                graphic.InterpolationMode   = InterpolationMode.HighQualityBicubic;

                graphic.DrawImage((System.Drawing.Image)image.Clone(), rectangle);
            }

            return resized;
        }

        #endregion
    }
}