12.4. Displaying Thumbnail Images
Problem
You want to be able to display a page of images stored in your database in thumbnail format.
Solution
Implement the first of the two ASP.NET pages described in
Recipe 12.3, changing the
Page_Load method in the code-behind class to scale
the full-sized image retrieved from the database to the appropriate
size for a thumbnail presentation.
In the Page_Load method of the code-behind class
for the page, use the .NET language of your choice to:
Create a
System.Drawing.Imageobject from the byte array retrieved from the database.Use a constant to define the height of the thumbnail and then calculate the width to maintain the aspect ratio of the image.
Use the
GetThumbnailImagemethod of theImageobject to scale the image to the desired size.Load the thumbnail image into a
MemoryStreamand then write it to theResponseobject.
Example 12-16 and Example 12-17 show the VB and C# code-behind class for our
example that illustrates this solution. (See the
CH12ImageFromDatabaseVB
.aspx
file and VB and C# code-behind files in Recipe 12.3 for our starting point.)
To display the thumbnails, create another ASP.NET page, add a
DataList control with image tags in the
ItemTemplate, and then use data binding to set the
src attributes of the image tags.
In the .aspx file for the page:
Use a
DataListcontrol to provide the ability to generate a list using data binding.Use a
HeaderTemplateto label the table of images.Use an
ItemTemplateto define an image that is displayed ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access