July 25, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Rendering Images in ASP.NET Directly from Your Database

  • August 1, 2007
  • By Paul Kimmel
  • Send Email »
  • More Articles »

Introduction

Time and again I have read articles about loading images on ASP.NET pages. All of the articles I have read suggest a variation on storing file system paths in the database and loading the URN (or path) to the image. But why should images be treated differently than text? Images are just data and one wouldn't store paths to text or numeric data.

In this article, you will learn how to store images in your database, directly load them to your ASP.NET pages, and some of the benefits that can be derived from this approach. If you are sure you know how to do this, read next week's article, but consider skipping to the end of this article. You are sure to learn a couple of techniques you may not be familiar with and there is a great book recommendation.

Disclaimer: Windows Presentation Foundation (WPF, aka Silverlight) may support loading images directly from the database, when you adopt Silverlight.

Understanding What Many Programmers Do Now

Everybody uses images in ASP.NET. The de facto standard for bitmaps, JPGs, and GIFs (collectively images) in ASP.NET is to store the URL/URN to the image in the database and bind the path to the image's source property. The technique described herein is relatively easy, but almost every other kind of data is stored in the database itself and images can be too.

Loading Images Directly from Your Database

First, you know that images can be stored in the database as an Image type (at least in SQL Server, and similar types exist for Oracle or whatever). You also know that you can store Image fields and retrieve them. The trick is how to get them in the <img> HTML control or the Image Web control. Because web controls are just little HTML generators, you can use either the HTML control or the ASP.NET Image control with database image binding.

For this example, I will be using .NET 2.0, Visual Studio 2005, and SQL Server 2000. I assure you this technique works for the next version of these products too. I will also be using the AdventureWorks2000 sample database because it has images in the ProductPhoto table and should be available to many of you.

Note: The AdventureWorks2000 ProductPhoto table uses .gif images. If you try to load a .gif image directly, you will receive a System.Exception with the following error: "A Graphics object cannot be created from an image that has an indexed pixel format." This article demonstrates how to resolve this error in a couple of ways. Generally, I use JPGs because they seem to be a little more manageable here.

Storing and Retrieving Images

An insert statement will load an image to a database field. Listing 1 shows code that will load an image from a database to a custom object (in Listing 2) containing an Image field, not the byte array you get back from the database.

Listing 1: The Data Access layer that constructs the ProductPhoto generic list.

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections.Generic

Public Class ReadProductPhoto

   Private Shared connectionString As String = _
      "Data Source=BUTLER;Initial Catalog= _
       AdventureWorks2000;Integrated Security=True"
   Private Shared sql As String = _
      "SELECT ProductPhotoID, LargePhoto FROM ProductPhoto"

   Public Shared Function  GetProductPhotos() _
      As List(Of ProductPhoto)

      Using connection As SqlConnection = _
         New SqlConnection(connectionString)
         connection.Open()
         Dim command As SqlCommand = _
            New SqlCommand(Sql, connection)
         Dim reader As SqlDataReader = command.ExecuteReader

         Dim list As List(Of ProductPhoto) = _
            New List(Of ProductPhoto)
         While (reader.Read())

            Dim id As Integer
            Dim image As Byte() = Nothing

            If (reader("ProductPhotoID") Is System.DBNull.Value) _
               Then Continue While
            id = reader.GetInt32(0)

            If (reader("LargePhoto") Is System.DBNull.Value = False) _
               Then image = CType(reader.GetValue(1), Byte())
            End If

            list.Add(New ProductPhoto(id, image))


         End While

         Return list
      End Using
   End Function

End Class




Page 1 of 5



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel