Click here to Skip to main content
Licence 
First Posted 16 Sep 2004
Views 59,959
Bookmarked 37 times

Rendering images directly from database BLOBS to Web Pages

By | 16 Sep 2004 | Article
How to render multiple images from the DB to the web pages directly.
 
Part of The SQL Zone sponsored by
See Also

Introduction

In a recent project, I had a situation with a lot of ad-hoc images, and these images were associated with certain profiles. It was decided that the images should reside in the database.

So, the task at hand was to be able to get these images out of the database and render them on the web pages dynamically.

This code snippet/control does just that. It takes the image identifier (key to the image in the database), reads the appropriate image from the DB, and then generates a dynamic, virtual image which can then be referenced on the ASP.NET page.

//Page component
private void Page_Load(object sender, System.EventArgs e)
{
    PicGen genPic = new PicGen();    
    int id = Convert.ToInt32(Request.QueryString["id"]);

    genPic.OutputPicture( this, id);

}
//------------ PicGen component.

//Get the connection String from the application's Web.Config file.
string ConnectionString = 
        ConfigurationSettings.AppSettings["connectionString"];

//Byte Array to hold the image from the DataBase.
private byte[] image ;

//Function to get the Img from the database.
private void getImg(int imgId){
   SqlConnection conn = new SqlConnection(ConnectionString);
   SqlDataReader rdr=null ;
   SqlCommand cmd=null ;

   try{
        //initiallise so that later we can test if length > 1
        image = new byte[' '];

        conn.Open();
        cmd = new SqlCommand();
        cmd.Connection = conn;

        cmd.CommandText =
            "select imageId, imageBolb from myImages where imageID = " 
            + imgId.ToString();
        rdr = cmd.ExecuteReader();
        if(rdr.HasRows){
            rdr.Read();
            //put the image in the byteArray
            image = (byte[])rdr["GraphicBlob"];
        }
    }
    finally{
        try{
            rdr.Close();
            conn.Close();
        }
        //ignore any error while closing.
        catch{}
    }
}

Using the code

This can be called from a regular aspx or HTML page in the project, by simply adding an image tag.

<img src=DynamicImageFromDB.aspx?id=203>

There are no files to clean up from the file system after this process, as the images are generated virtually for each call.

Points of Interest

If I get enough queries on this one, then I can start writing another article about how to stuff the images in the database in the first place.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Mind Experts

Web Developer

United States United States

Member

Over 7 years of code development experience in various MS Platforms.
 
One of the best compliments I ever received was "All my code is nothing but Hacks"!!
My Web Site -->Mind Experts!


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionPicGen?? PinmemberD. Harmor17:14 29 Oct '05  
AnswerRe: PicGen?? PinmemberMind Experts1:52 30 Oct '05  
QuestionRe: PicGen?? Pinmembermlowry9:02 7 Dec '06  
AnswerRe: PicGen?? PinmemberMind Experts1:52 30 Oct '05  
GeneralYeah, do write antother article about... Pinsussrobprogger20:35 11 Sep '05  
GeneralInterested in getting the images into database! PinmemberWeeeBob23:28 27 Jan '05  
GeneralERROR on genPic.OutputPicture( this, id); PinsussAnonymous4:57 29 Oct '04  
GeneralRe: ERROR on genPic.OutputPicture( this, id); PinmemberMind Experts5:43 29 Oct '04  
Generalperformance issues... Pinmemberl a u r e n12:34 17 Sep '04  
GeneralRe: performance issues... PinmemberMind Experts4:52 18 Sep '04  
GeneralRe: performance issues... Pinmemberl a u r e n12:55 19 Sep '04  
GeneralRe: performance issues... PinmemberMind Experts15:58 19 Sep '04  
GeneralRe: performance issues... PinsussAnonymous15:14 21 Feb '05  
GeneralRe: performance issues... PinmemberMind Experts15:30 21 Feb '05  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 16 Sep 2004
Article Copyright 2004 by Mind Experts
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid