Click here to Skip to main content
15,891,951 members
Articles / Web Development / ASP.NET

Display images from a SQL database - the easy way

Rate me:
Please Sign up or sign in to vote.
2.67/5 (4 votes)
5 Jul 2008Ms-PL1 min read 42.4K   646   21   10
This project contains a Web Custom Control for rendering images directly from the database. Can be used with image fields, text, date, or numerical fields (it generates an image containing the text), and with URL fields (like in a regular image control).

Introduction

This is version 3.0 of the VsCustomRenderer control. You can find the previous versions here: 2.0 and 1.0.

Background

This project was inspired by an article on CodeProject written by dev2dev: Create Dynamic Images in ASP.NET. Parts of the code were taken from that project.

Using the Code

To test this control:

  • Open or create a website.
  • Add the VSCustomRendererLibrary reference to the website.
  • Add the following line in the <httpHandlers> section in the web.config:
XML
<add verb="*" path="*.jpg" 
  type="VSCustomRendererLibrary.VsImageHandler, VSCustomRendererLibrary"/>

If you cannot find this section in your web.config file, just create it under <system.web>:

XML
<system.web>
    ......
     <httpHandlers>
          <add verb="*" path="*.jpg" 
            type="VSCustomRendererLibrary.VsImageHandler, VSCustomRendererLibrary"/>
     </httpHandlers>
</system.web>
  • Open/create a new ASPX page.
  • Add a SqlDataSource connected to a SQL table or view (this project was tested with SQL Server 2005 EE).
  • Add a GridView and connect it to the SqlDataSource.
  • In the GridView, add the image column and transform it into a template field.
  • Select Edit Template from the GridView toolbar menu.
  • Add a VSCustomRenderer into the ItemTemplate. Bind the control to the DataSource field.

Image 1

Points of Interest

This Web Control now works with AJAX. This control no longer saves images to disk in order to display them. The pictures are displayed directly from memory through an IHttpHandler. This HTTP handler does not open a new database connection, it just displays the images from memory, so there are no delays in page rendering and no extra parameters to set.

History

All the versions of this control are also posted on CodePlex.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior)
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Deian18-Mar-10 0:04
Deian18-Mar-10 0:04 
QuestionFailure Pin
vferrava29-Jul-08 11:09
vferrava29-Jul-08 11:09 
While this works fine in Visual Studio, when I turn it over to my IIS server, I get the classic white box with the red X instead of the image. The properties shows the address as: http:\\myIISserver\ 1.jpg?idimg=1 file but the Type is Not Available and the immage does not display. The win.config has been updated. What am I missing?
AnswerRe: Failure Pin
eduguesser31-Jul-08 9:44
eduguesser31-Jul-08 9:44 
GeneralRe: Failure Pin
FF779-Nov-08 1:53
FF779-Nov-08 1:53 
GeneralAlternative Pin
Vitaly Tomilov5-Jul-08 10:51
Vitaly Tomilov5-Jul-08 10:51 
GeneralRe: Alternative Pin
FF775-Jul-08 10:58
FF775-Jul-08 10:58 
GeneralRe: Alternative Pin
HoyaSaxa935-Jul-08 13:13
HoyaSaxa935-Jul-08 13:13 
GeneralRe: Alternative [modified] Pin
Vitaly Tomilov6-Jul-08 1:46
Vitaly Tomilov6-Jul-08 1:46 
GeneralRe: Alternative Pin
FF776-Jul-08 2:12
FF776-Jul-08 2:12 
GeneralRe: Alternative Pin
Vitaly Tomilov6-Jul-08 11:01
Vitaly Tomilov6-Jul-08 11:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.