Image Resize : Image « 2D Graphics « C# / C Sharp






Image Resize

   
//http://blazingcms.codeplex.com/
//GNU General Public License version 2 (GPLv2)
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;

namespace Blazing.Web.Util
{
    #region ImageResize CLASS
    /// <summary>
    /// Found: http://www.codeproject.com/KB/aspnet/Thumbnail_Images.aspx
    /// 
    /// ImageResize is a class that is based on an article that was obtained from
    /// the URL http://www.devx.com/dotnet/Article/22079/0/page/3. I had to make
    /// some minor changes to a couple of the properties, but otherwise it is very
    /// much like the original article.
    /// </summary>
    public class ImageResize
    {
        #region Instance Fields
        //instance fields
        private double m_width, m_height;
        private bool m_use_aspect = true;
        private bool m_use_percentage = false;
        private System.Drawing.Image m_src_image, m_dst_image;
        private System.Drawing.Image m_image;
        private ImageResize m_cache;
        private Graphics m_graphics;
        #endregion
        #region Public properties
        /// <summary>
        /// gets of sets the File
        /// </summary>
        public System.Drawing.Image File
        {
            get { return m_image; }
            set { m_image = value; }
        }
        /// <summary>
        /// gets of sets the Image
        /// </summary>
        public System.Drawing.Image Image
        {
            get { return m_src_image; }
            set { m_src_image = value; }
        }
        /// <summary>
        /// gets of sets the PreserveAspectRatio
        /// </summary>
        public bool PreserveAspectRatio
        {
            get { return m_use_aspect; }
            set { m_use_aspect = value; }
        }
        /// <summary>
        /// gets of sets the UsePercentages
        /// </summary>
        public bool UsePercentages
        {
            get { return m_use_percentage; }
            set { m_use_percentage = value; }
        }
        /// <summary>
        /// gets of sets the Width
        /// </summary>
        public double Width
        {
            get { return m_width; }
            set { m_width = value; }
        }
        /// <summary>
        /// gets of sets the Height
        /// </summary>
        public double Height
        {
            get { return m_height; }
            set { m_height = value; }
        }
        #endregion
        #region Public Methods
        /// <summary>
        /// Returns a Image which represents a rezised Image
        /// </summary>
        /// <returns>A Image which represents a rezised Image, using the 
        /// proprerty settings provided</returns>
        public virtual System.Drawing.Image GetThumbnail()
        {
            // Flag whether a new image is required
            bool recalculate = false;
            double new_width = Width;
            double new_height = Height;
            // Load via stream rather than Image.FromFile to release the file
            // handle immediately
            if (m_src_image != null)
                m_src_image.Dispose();
            m_src_image = m_image;
            recalculate = true;
            // If you opted to specify width and height as percentages of the original
            // image's width and height, compute these now
            if (UsePercentages)
            {
                if (Width != 0)
                {
                    new_width = (double)m_src_image.Width * Width / 100;

                    if (PreserveAspectRatio)
                    {
                        new_height = new_width * m_src_image.Height / (double)m_src_image.Width;
                    }
                }
                if (Height != 0)
                {
                    new_height = (double)m_src_image.Height * Height / 100;

                    if (PreserveAspectRatio)
                    {
                        new_width = new_height * m_src_image.Width / (double)m_src_image.Height;
                    }
                }
            }
            else
            {
                // If you specified an aspect ratio and absolute width or height, then calculate this 
                // now; if you accidentally specified both a width and height, ignore the 
                // PreserveAspectRatio flag
                if (PreserveAspectRatio)
                {
                    if (Width != 0 && Height == 0)
                    {
                        new_height = (Width / (double)m_src_image.Width) * m_src_image.Height;
                    }
                    else if (Height != 0 && Width == 0)
                    {
                        new_width = (Height / (double)m_src_image.Height) * m_src_image.Width;
                    }
                }
            }
            recalculate = true;
            if (recalculate)
            {
                // Calculate the new image
                if (m_dst_image != null)
                {
                    m_dst_image.Dispose();
                    m_graphics.Dispose();
                }
                System.Drawing.Image bitmap = new Bitmap((int)new_width, (int)new_height);
                m_graphics = Graphics.FromImage(bitmap);
                m_graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                m_graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                m_graphics.DrawImage(m_src_image, 0, 0, bitmap.Width, bitmap.Height);
                m_dst_image = bitmap;
                // Cache the image and its associated settings
                m_cache = this.MemberwiseClone() as ImageResize;
            }

            return m_dst_image;
        }
        #endregion
        #region Deconstructor
        /// <summary>
        /// Frees all held resources, such as Graphics and Image handles
        /// </summary>
        ~ImageResize()
        {
            // Free resources
            if (m_dst_image != null)
            {
                m_dst_image.Dispose();
                m_graphics.Dispose();
            }

            if (m_src_image != null)
                m_src_image.Dispose();
        }
        #endregion
    }
    #endregion
}

   
    
    
  








Related examples in the same category

1.Load Image from an image file with Exception handler
2.Image.FromStream: load image from stream
3.Shrink ImageShrink Image
4.Shear ImageShear Image
5.Clone ImageClone Image
6.Get Image Resolution and Image size and Display sizeGet Image Resolution and Image size and Display size
7.Draw image based on its sizeDraw image based on its size
8.Set image resolution and paint itSet image resolution and paint it
9.Load image and displayLoad image and display
10.Fill Ellipse with image based Texture BrushFill Ellipse with image based Texture Brush
11.Image Save
12.Image Open
13.Thumbnail Image
14.Draw on Pixel-Size Image
15.Draw text on an Image
16.Partial Image Rotate
17.Partial Image Stretch
18.Draw Partial Image
19.Image At Points (Draw part of the image)
20.Image Reflection
21.Image Scale Isotropic
22.Image Scale To Rectangle
23.Center Pixel-Size Image by using Image.Width and Image.Height
24.Center an Image (VerticalResolution, HorizontalResolution)
25.Load image from a URL(Web) and draw it
26.Scribble with Bitmap
27.Image Flipping and Rotating
28.Image.GetThumbnailImage
29.Image Zoom
30.Create Thumbnail
31.Get Bitmap Source
32.Get Bytes From BitmapSource
33.Load Image
34.Create Icon from Image
35.Resizes an image
36.Get Gravatar Image
37.Changing image opacity, Resize image, crop image and gray scale image
38.Resize Image
39.Resize image to fit
40.Image Filter
41.Save Window/Canvas to Image
42.Image Cache
43.Saves the image to a bytes array. Used to store an image into DB.
44.Get the "energy" of a pixel