Development ASP.Net Tutorial

File: App_Code\ImageHandler.cs
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
namespace MyNamespace
{
    public class ImageHandler : IHttpHandler
    {
        const string connectionStringName = "Images";
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Buffer = false;
            string fileName = VirtualPathUtility.GetFileName(context.Request.Path);
            string conString = WebConfigurationManager.ConnectionStrings [connectionStringName].ConnectionString;
            SqlConnection con = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand("SELECT Image FROM Images WHERE FileName=@FileName", con);
            cmd.Parameters.AddWithValue("@fileName", fileName);
            using (con)
            {
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader(CommandBehavior. SequentialAccess);
                if (reader.Read())
                {
                    int bufferSize = 8040;
                    byte[] chunk = new byte[bufferSize];
                    long retCount;
                    long startIndex = 0;
                    retCount = reader.GetBytes(0, startIndex, chunk, 0, bufferSize);
                    while (retCount == bufferSize)
                    {
                        context.Response.BinaryWrite(chunk);
                        startIndex += bufferSize;
                        retCount = reader.GetBytes(0, startIndex, chunk, 0, bufferSize);
                    }
                    byte[] actualChunk = new Byte[retCount - 1];
                    Buffer.BlockCopy(chunk, 0, actualChunk, 0, (int)retCount - 1);
                    context.Response.BinaryWrite(actualChunk);
                }
            }
        }
        public bool IsReusable
        {
            get { return true; }
        }
    }
}
            
Register the class in the web configuration file. 
File: Web.Config

  
          connectionString="Data Source=.\SQLExpress;Integrated
           Security=True;AttachDBFileName=|DataDirectory|ImagesDB.mdf;
           User Instance=True"/>
  

    
      
                   type="MyNamespace.ImageHandler" validate="false" />
                   type="MyNamespace.ImageHandler" validate="false" />
                   type="MyNamespace.ImageHandler" validate="false" />
      

    


Displaying images with the ImageHandler.
<%@ Page Language="C#" %>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (upFile.HasFile)
        {
                srcImages.Insert();
        }
    }



    Image Upload


    
    

            id="lblFile"
        Text="Image File:"
        AssociatedControlID="upFile"
        Runat="server" />
            id="upFile"
        Runat="server" />
            id="btnAdd"
        Text="Add Image"
        OnClick="btnAdd_Click"
        Runat="server" />
    
            id="grdImages"        
        DataSourceID="srcImages"
        AutoGenerateColumns="false"
        ShowHeader="false"
        GridLines="None"
        Runat="server">
        
                    DataImageUrlField="FileName"
            DataAlternateTextField="FileName" />
        

    
            id="srcImages"
        ConnectionString="<%$ ConnectionStrings:Images %>"
        SelectCommand="SELECT FileName FROM Images"
        InsertCommand="INSERT Images (FileName,Image) VALUES (@FileName,@FileBytes)"
        Runat="server">