April 19, 2024

SamTech 365

PowerPlatform, Power Apps, Power Automate, PVA, SharePoint, C#, .Net, SQL, Azure News, Tips ….etc

ASP.net handler for images

If you need to have a dynamic url which is supposed to read files from specific location and return the feed to the browser, especially in the case where the end users are not supposed to have access to the files’ location, what can we do ?

ASP.net handler is the answer for this question

1- In Visual Studio, create a new ASP.net handler, imagesHandler.aspx

 

 

public void ProcessRequest(HttpContext context)
 {
 string imgName = context.Request.QueryString["n"];
 context.Response.ContentType = "image/png";
 string path = @"C:\Users\Public\Pictures\Sample Pictures\" + imgName;
 Image image = Image.FromFile(path);
 image.Save(context.Response.OutputStream, ImageFormat.Png);
}

Than, we can just use the url of the image control and point it to the handler

image.src = "images.ashx?n=penguin.jpg";

 

 

If you need to have a dynamic url which is supposed to read files from specific location and return the feed to the browser, especially in the case where the end users are not supposed to have access to the files’ location, what can we do ?

ASP.net handler is the answer for this question

1- In Visual Studio, create a new ASP.net handler, imagesHandler.aspx

 

 

public void ProcessRequest(HttpContext context)
 {
 string imgName = context.Request.QueryString["n"];
 context.Response.ContentType = "image/png";
 string path = @"C:\Users\Public\Pictures\Sample Pictures\" + imgName;
 Image image = Image.FromFile(path);
 image.Save(context.Response.OutputStream, ImageFormat.Png);
}

Than, we can just use the url of the image control and point it to the handler

image.src = "images.ashx?n=penguin.jpg";