Image Handler
I have images stored in a network drive in another server. How can I show the image in a web page?
If for some reason, it is not possible to create a virtual directory within IIS, then creating a handler for streaming images is the best solution. A handler is a class derived from IHttpHandler. It has a method called ProcessRequest which contains the Request and Response objects. The code for showing streaming images in a webpage using handler is as follows:
public class ImageViewer : IHttpHandler { public void ProcessRequest(HttpContext context) { string id = context.Request.QueryString["Id"]; if (!String.IsNullOrEmpty(id)) { // Retrieve the image filename based on id // For our example, the image filename is C:\red.jpg string filePath = "C:\\red.jpg"; // The above path could be any network drive FileStream fs = new FileStream(filePath, FileMode.Open); byte [] bytes = new byte[fs.Length]; fs.Read(bytes, 0, (int)fs.Length); context.Response.BinaryWrite(bytes); } } public bool IsReusable { get { return false; } } }In the above code, a filestream object is used to access the file in a network drive. The image is read into a binary buffer (byte []) and is written into the Response object.
In the ASPX page, you will write code like below to access the image using Handler (.ashx):
<img src="ImageViewer.ashx?id=1" />
Posted by Vijay on 22-Oct-2009 02:49 AM
Category : ASP.NET
Category : ASP.NET



I am facing error in line Dim fs As New FileStream(sFilePath, FileMode.Open) The error is : Logon failure: unknown user name or bad password
Are you using out-of-process app pooling. Check this KB article: http://support.microsoft.com/kb/255770