In this article I will explain with an example, how to show images that are saved (stored) outside the Website root folder (directory) in ASP.Net using C# and VB.Net.
The Images will be fetched outside the Website root folder (directory) and then displayed in Image control with the help of Generic HTTP Handler in ASP.Net using C# and VB.Net.
HTML Markup
Following HTML markup consists of an ASP.Net DropDownList control and an Image control to display the selected image.
The OnSelectedIndexChanged event handler has been set for the DropDownList.
<span>Select Image:</span>
<asp:DropDownList ID="ddlImages" runat="server" AppendDataBoundItems="true" AutoPostBack="true"
OnSelectedIndexChanged="FetchImage">
<asp:ListItem Text="Select Image" Value="0"/>
</asp:DropDownList>
<hr/>
<asp:Image ID="Image1" runat="server" Visible="false"/>
Namespaces
You will need to import the following namespace.
C#
VB.Net
Binding images stored outside the Website root folder (directory) in DropDownList
In the Page Load event, the Image files are fetched from the Images folder stored outside the Website Root Folder and using a ForEach Loop, the file name of the Image files are added to the DropDownList one by one.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string[] images = Directory.GetFiles(@"C:\Images");
foreach (string imageFile in images)
{
ddlImages.Items.Add(new ListItem(Path.GetFileName(imageFile), Path.GetFileName(imageFile)));
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim images() As String = Directory.GetFiles("C:\Images")
For Each imageFile As String In images
ddlImages.Items.Add(New ListItem(Path.GetFileName(imageFile), Path.GetFileName(imageFile)))
Next
End If
End Sub
Adding Generic Handler
You will need to add a new Generic Handler (ASHX) file using Add New Item Dialog of Visual Studio as shown below.
The Generic Handler
The following Generic Handler accepts the FileName of the Image file through the QueryString parameter and the file is read from the Folder outside the Root directory and then the Image is converted into Byte Array.
Finally, the Byte Array is returned through the response.
C#
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.IO;
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (!string.IsNullOrEmpty(context.Request.QueryString["FileName"]))
{
string filePath = "C:\\Images\\";
string fileName = context.Request.QueryString["FileName"];
string contentType = "image/" + Path.GetExtension(fileName).Replace(".", "");
using (FileStream fs = new FileStream(filePath + fileName, FileMode.Open, FileAccess.Read))
{
using (BinaryReader br = new BinaryReader(fs))
{
// Read the file and convert it to Byte Array.
byte[] bytes = br.ReadBytes((Int32)fs.Length);
br.Close();
fs.Close();
// Write the file to response Stream.
context.Response.ContentType = contentType;
context.Response.BinaryWrite(bytes);
context.Response.End();
}
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
VB.Net
<%@ WebHandler Language="VB" Class="Handler" %>
Imports System
Imports System.Web
Imports System.IO
Public Class Handler : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
If Not String.IsNullOrEmpty(context.Request.QueryString("FileName")) Then
Dim filePath As String = "C:\Images\"
Dim fileName As String = context.Request.QueryString("FileName")
Dim contentType As String = "image/" & Path.GetExtension(fileName).Replace(".", "")
Using fs As FileStream = New FileStream(filePath & fileName, FileMode.Open, FileAccess.Read)
Using br As BinaryReader = New BinaryReader(fs)
' Read the file and convert it to Byte Array.
Dim bytes As Byte() = br.ReadBytes(Convert.ToInt32(fs.Length))
br.Close()
fs.Close()
' Write the file to response Stream.
context.Response.ContentType = contentType
context.Response.BinaryWrite(bytes)
context.Response.End()
End Using
End Using
End If
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
Displaying the images stored outside the Website root folder (directory) in Image Control using Generic Handler
The following event handler is executed on the SelectedIndexChanged event of the DropDownList control.
The FileName of the Image is appended in the URL of the Generic HTTP Handler and the URL is assigned to the Image control.
As soon as the Page is rendered, the Generic HTTP Handler is called and it returns the Image data i.e. Binary data to the Image control which ultimately displays the Image.
C#
protected void FetchImage(object sender, EventArgs e)
{
string fileName = ddlImages.SelectedItem.Value;
Image1.Visible = fileName != "0";
if (fileName != "0")
{
Image1.ImageUrl = "Handler.ashx?FileName=" + fileName;
}
}
VB.Net
Protected Sub FetchImage(ByVal sender As Object, ByVal e As EventArgs)
Dim fileName As String = ddlImages.SelectedItem.Value
Image1.Visible = fileName <> "0"
If fileName <> "0" Then
Image1.ImageUrl = "Handler.ashx?FileName=" & fileName
End If
End Sub
Screenshot
Download