Click here to Skip to main content
15,886,036 members
Articles / Web Development / ASP.NET

SQL Database Image Storage & Easy Thumbnails

Rate me:
Please Sign up or sign in to vote.
4.82/5 (62 votes)
17 May 20077 min read 464.6K   8.9K   278  
Shows how to store images in SQL Database Image Storage & create Thumnails easiliy from
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;



#region ImageResize CLASS
/// <summary>
/// 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();
            }
            Bitmap bitmap = new Bitmap((int)new_width, (int)new_height, m_src_image.PixelFormat);
            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

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions