当前位置 > 首页 > 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
    }
}



  • How To: Dynamically Load A Page For Processing

    2007-7-10 10:06:00

    I have often desired the ability to dynamically load a page for processing, much like we do for .as

  • .NET 4.5 now supported with Windows Azure Web Sites

    2012-10-26 11:06:00

    This week we finished rolling out .NET 4.5 to all of our Windows Azure Web Site clusters.  Thi

  • March 2007 CTP Linq 的一些改变

    2007-5-18 1:30:00

    摘要: 开始学习Linq,下载Visual Studio Orcas beta1 pro版本,安装过程中自带装上了.Net framework 3.5 在网上学习了N多资料以后开始自己尝试写一

  • F#中的管道操作符

    2009-10-21 14:43:00

    原文出处: http://lorgonblog.spaces.live.com/blog/cns!701679AD17B6D310!165.entry Translated by Sean

  • 象棋编程游戏——布局篇

    2009-12-24 17:27:00

    阅读: 361 评论: 8 作者: 夏小冰 发表于 2009-12-24 15:36 原文链接 BingWay原创作品,转载请注明作者和出处。 这段时间,发现自己的象棋水平有点小进步

  • Latest ASP.NET AJAX Articles

    2007-8-23 5:25:00

    Over the past few months Ive been writing articles for the .NET Insight insight newsletter coveri

  • MS BI Conference 14: Evening Reception

    2007-5-16 16:23:00

    The evening reception was at the Experience Music Project/SciFi Museum Hall of Fame. Gary and I wal

  • OpenXML: excel 插入BarChart图表

    2009-6-17 20:47:00

    Openxml format SDK 2.0 v2 的 how to 文章里提供了一个怎样插入 chart 图表的示例, 但这个 chart的数据是独立不是根据excel sheet 上data 动

  • VS2008 使用IE8 调试出错

    2009-8-12 22:44:00

    这几天新装VS2008 建立一个WEB应用程序 直接F5调试 VS2008自己报错挂了 要重启VS. 到网上找了很久都没找到答案..于是怀疑是自己浏览器问题 我用的是IE8 B2版 装了个IE7

  • C# 异步读取数据库里面的数据与绑定UI的解决办法

    2009-7-7 14:44:00

    异步读取数据库,在数据绑定的时候会出现点问题,就是窗体界面会无法关闭,要结束任务才能结束进程。例如下面代码 首先按习惯的方法,设定线程更新UI a2.CheckForIllegalCro