In this article I will explain with an example, how to display images that are saved (stored) outside the Website root folder using C# and VB.Net in ASP.Net.
The Images will be read and then displayed in Image control with the help of Generic HTTP Handler in ASP.Net using C# and VB.Net.
HTML Markup
The following HTML markup consists of:
Image – For displaying the image file.
The ImageUrl property of the Image control has been set with path of the Generic HTTP Handler with FileName in QueryString parameter.
<asp:Image ID="Image1" runat="server" ImageUrl="~/HandlerCS.ashx?FileName=Penguins.jpg" />
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 = "D:\\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 = "D:\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
Screenshot
Download