当前位置 > 首页 > Asp.net

Thumbnail image from database in ASP.NET

2009-10-28 16:02:00来源: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.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
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.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
    }
}