Beginners commonly ask how to create thumbnails of images stored in a database and show them in an aspx page. So I decided to build a sample application that uses a repeater control, a generic handler and one aspx page.
My database table name is ImgTable.
- USE [TestDatabase]
- GO
- /****** Object: Table [dbo].[ImgTable] Script Date: 02/01/2013 13:45:38 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- SET ANSI_PADDING ON
- GO
- CREATE TABLE [dbo].[ImgTable](
- [Sno] [bigint] IDENTITY(1,1) NOT NULL,
- [Name] [varchar](500) NULL,
- [ImageName] [varbinary](max) NULL
- ) ON [PRIMARY]
- GO
- SET ANSI_PADDING OFF
- GO
In the ImgTable I have three columns:
-
Sno that is the identity column
-
Name
-
ImageName that is of type VarBinary(MAX) that will store the image
Let's start coding the front end.
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Repeater ID="Repeater1" runat="server">
- <ItemTemplate>
- <asp:Image Width="100px" Height="100px" ID="Image1" runat="server" ImageUrl="<%# Bind('name') %>"/>
- </ItemTemplate>
- </asp:Repeater>
- </div>
- </form>
- </body>
In the cs file on the page load event I use the following:
- string constring = @"Data Source=******;Initial
- Catalog=TestDatabase;Integrated Security=True;User ID=****** ";
- SqlConnection cn = new SqlConnection(constring);
- cn.Open();
- SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.ImgTable", cn);
- DataTable dt = new DataTable();
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- sda.Fill(dt);
- List<ThumbNail> li = new List<ThumbNail>();
- foreach(DataRow row in dt.Rows)
- {
- li.Add(new ThumbNail { thumb = (byte[])row["ImageName"], sno = row["sno"].ToString() });
- row["name"] = "Handler.ashx?SrNo=" + row["sno"].ToString();
- }
- dt.AcceptChanges();
- Session["file"] = li;
- Repeater1.DataSource = dt;
- Repeater1.DataBind();
Don't be confused with List<Thumbnail>li =new List<Thumbnail>(), Thumbnail is my class in which there is two fields of the thumb (type byte array) and sno of type string. As you can see in the preceding code, I created a list of the class type and added sno and image fetched from the database into it and put the list into a session variable that I will later use on my handler.

Guest UserPosted Nov 1, 2017, 2:06 PM
Thanks for the article. I created it as per ur article but these are not clickable thumbnail. I want to retrieve images stored with details from another database on click dynamically
Anil KumarPosted Feb 3, 2013, 12:52 AM
1. You are fetching all images from your table. 2. You store all this into Session and also make ready a DataTable 3. You are directly binding your Repeater with DataTable 4. You state "Beginners commonly ask how to create thumbnails of images stored in a database and show them in an aspx page. So I decided to build a sample application that uses a repeater control, a generic handler and one aspx page." I don't find any bit of code that create thumbnail (it is html that fix the image in fixed height/width).