Access image through url and render as jpeg in asp.net

No.of Views933
Bookmarked0 times
Downloads 
Votes0
By  malav.rajendra   On  28 Mar 2010 05:03:19
Tag : ASP.NET , Image Handling
In this code snippet, access the image through the image in asp.net and render the image in page.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this code snippet, i'm going to explain read image through the url and render the image in asp.net page.

CodeSnippet

class ImageProcessingModule : IHttpModule
{
public void Init(System.Web.HttpApplication context)
{
context.BeginRequest += new EventHandler(OnBeginRequest);
}
private void OnBeginRequest(object sender, EventArgs e)
{
HttpApplication app;
string requestUrl;

app = sender as HttpApplication;
requestUrl = app.Request.Url.ToString();
//requestUrl=requestUrl.Replace("https://", "http://");


//if (!requestUrl.ToLower().Contains("products")) { return; }
string[] strUrl = requestUrl.Split('/');
string ProductCode = string.Empty;
string Size = string.Empty;

for (int i = 0; i < strUrl.Length; i++)
{
if (strUrl[i].ToLower() == "images" && strUrl[i + 1].ToLower() == "products")
{
if ((i + 2) < strUrl.Length)
ProductCode = strUrl[i + 2];
if ((i + 3) < strUrl.Length)
Size = strUrl[i + 3];

string serverDirectory = app.Server.MapPath("~/UploadImages/" + Size);
string FilePath = System.IO.Path.Combine(serverDirectory, ProductCode) + ".jpg";
//app.Server.Transfer("~/ImageSystem/RetreiveImages.aspx?productcode=" + ProductCode + "&size=" + Size + "",false);
// app.Response.Redirect ("~/UploadImages/" + Size + "/" + ProductCode + ".jpg");

byte[] photo = null;
if (System.IO.File.Exists(FilePath))
{
photo = ReadImage(app.Server.MapPath( "~/UploadImages/" + Size + "/" + ProductCode + ".jpg"));//FilePath;
}
else
{
photo = ReadImage(app.Server.MapPath("~/UploadImages/" + Size + "/Missing.jpg"));//FilePath;

}
if (photo != null)
{
app.Response.ContentType = "image/jpeg";
app.Response.Cache.SetCacheability(HttpCacheability.Public);
app.Response.BufferOutput = false;
app.Response.OutputStream.Write(photo, 0, photo.Length);
}

}
}
// app.Response.Write(ProductCode+" hi "+ Size);

}

public void Dispose()
{
}
private static byte[] ReadImage(string p_postedImageFileName)
{
// bool isValidFileType = false;
try
{
FileInfo file = new FileInfo(p_postedImageFileName);


FileStream fs = new FileStream(p_postedImageFileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] image = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
return image;

// return null;
}
catch (Exception ex)
{
return null;
//throw ex;
}
}
}

 

Finally you have to register this httpmodule in web configuration file.

<httpModules>	
<add name="ImageProcessingModule" type="ImageProcessingModule"/>		
</httpModules>

 

 
Sign Up to vote for this article
 
About Author
 
malav.rajendra
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-Not Provided
Joined date-01 Feb 2010
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
    Auto Growing TextBox or TextArea in ASP.NET
    Published Date : 08/May/2010
    In this code snippet, you will learn how to bind DropdownList within the ListView in ASP.NET. The ListView is powerful control and fully customizable using templates.
    Published Date : 10/Oct/2010
    In this codesnippet, i will show How to Delete Row in GridView using JQuery in ASP.NET.
    Published Date : 20/Jul/2011
    In this snippet, I will show how to format a cell and apply style in gridview using JQuery. Sometimes we may need to apply the format for a particular cell based on the cell value; it can be done in within DataRowBound event in asp.net.
    Published Date : 05/Jan/2011
    In this snippet I will explain how to add controls dynamically in asp.net and register events for the controls and make it work events perfectly. Last week I have read the forums many readers asking about add controls dynamically in asp.net giving problems and also it not working properly with events
    Published Date : 03/Jan/2011
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top