Friday, November 22, 2013

How to give Local drive image Url path as a source to img tag in ASP.NET

How to give Local drive image Url path as a source to img tag in ASP.NET:

 

While doing programming I come a cross issue that how to display image from local drive C ,D..it won't take when you give src="D://test.jpg" I found the solution For that I am sharing with you..

 

Write an Handler image.ashx:
using System;
using System.Web;
public class images : IHttpHandler {
 public void ProcessRequest (HttpContext context) {
       string imgName = context.Request.QueryString["n"];
       context.Response.ContentType = "image/png";
       string path = @"" + imgName;
       System.Drawing.Image image = System.Drawing.Image.FromFile(path);
       image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
    }
    public bool IsReusable {
       get {
           return false;
       }
    }
 
}


Let Us say I have image tag in source with id img1

And From your page aspx.cs just call:

if (!IsPostBack)
      { img1.ImageUrl = "images.ashx?n=C:/Application/esdata/Photos/110657463302002.jpg";}

No comments: