Thumbnail image from database in ASP.NET

We often need to display images from database like profile photo of the user or something. Displaying images from database to an ASP.NET Image is not a big deal but maintaining the exact aspect ratio and displaying a web friendly image is very important. The following program will help us to do that.

<asp:Image ID="ImgPhoto" runat="server"></asp:Image>

The below code is written in the code behind page. Either the Width or Height of the image is specified. The second dimension is calculated by the program, maintaining the aspect ratio.

ImgPhoto.ImageUrl = "ImageDisplay.aspx?Width=300&PhotoID=2";

ImageDisplay.aspx is a webpage which contains the following code. This page is used to retrieve the image from the database and generate a thumbnail of the dimensions given by the user.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

using System.Drawing;

using System.Drawing.Imaging;

 

public partial class ImageDisplay : System.Web.UI.Page

{

    DAL_Users objUser = new DAL_Users();

 

    protected void Page_Load(object sender, EventArgs e)

    {

        #region Page_Load

 

        if (!IsPostBack)

        {

            int orgwidth = 0;

            int orgheight = 0;

            int imgwidth = 0;

            int imgheight = 0;

 

            byte[] bytes = null;

            if (Request.QueryString["PhotoID"] != null)

                bytes = (byte[])objUser.GetPhotoFromDatabase(Convert.ToInt32(Request.QueryString["PhotoID"].Trim()));

            System.IO.MemoryStream streamBitmap = new System.IO.MemoryStream(bytes);

            Bitmap objBm = new Bitmap((Bitmap)System.Drawing.Image.FromStream(streamBitmap));

 

            orgwidth = objBm.Width;

            orgheight = objBm.Height;

 

            imgwidth = objBm.Width;

            imgheight = objBm.Height;

 

            if (Request.QueryString["Width"] != null)

            {

                imgwidth = Convert.ToInt32(Request.QueryString["Width"].Trim());

                imgheight = Convert.ToInt32((imgwidth * orgheight) / orgwidth);

            }

            else if (Request.QueryString["Height"] != null)

            {

                imgheight = Convert.ToInt32(Request.QueryString["Height"].Trim());

                imgwidth = Convert.ToInt32((imgheight * orgwidth) / orgheight);

            }

 

            //Generate the Thumbnails

            Bitmap objb2 = new Bitmap(objBm.GetThumbnailImage(imgwidth, imgheight, null, IntPtr.Zero));

 

            //Converting Bitmap to Byte array

            System.IO.MemoryStream stream = new System.IO.MemoryStream();

            objb2.Save(stream, ImageFormat.Jpeg);

            stream.Position = 0;

            byte[] data = new byte[stream.Length];

            stream.Read(data, 0, Convert.ToInt32(stream.Length));

 

            objb2.Dispose();

            objBm.Dispose();

            bytes = null;

            GC.Collect();

 

            //Write the byte array

            Response.BinaryWrite(data);

            Response.End();

        }

 

        #endregion

    }

}

Published Wednesday, October 28, 2009 12:58 PM by k.srinivas81

Comments

# re: Thumbnail image from database in ASP.NET

Wednesday, October 28, 2009 11:25 AM by Jeff

It makes more sense to use an HttpHandler (via .ashx or registered in web.config) instead of a page. The page comes with all of the baggage of creating a page, whereas the handler just puts out data.

You also may want to consider using the WPF classes instead of System.Drawing. They might be faster (just guessing, I've never compared). You shouldn't have to call GC either.

# re: Thumbnail image from database in ASP.NET

Wednesday, October 28, 2009 1:15 PM by AndrewSeven

A handler would definitely be better, and the code should NOT call CG.Collect()

Also: Generating the appropriate http headers for cache control and expiration is just as important as serving the image bytes

Leave a Comment

(required) 
(required) 
(optional)
(required)