Monthly Archives: January 2011

Generic function to display images on webpage asp.net

Hi fellas,

I am writing this post after a long time. This new year I am back at blogging to help the software developers in writing better code .

Ever need of a generic/common function to display the images on website?
In normal practice, whenever we need to show an image, we used to directly write
Image.Src= “/userfiles/abc.jpg”;

This can be painful sometimes if your folder path gets changed,  in that case you need to replace the folder (userfiles) everywhere.

Smart and better way is to create a common function that will display images. What you need to do is to call this method every time.


Method:

public string ShowImage(string ImageName)
{
string ReturnVal = string.Empty;

if (File.Exists(System.Web.HttpContext.Current.Server.MapPath(“~/userfiles/” + ImageName)) == true)
{
ReturnVal = VirtualPathUtility.ToAbsolute(“~/userfiles/” + ImageName);
}
else
{
ReturnVal = VirtualPathUtility.ToAbsolute(“~/Images/ImageNotAvailable.jpg”);
}

return ReturnVal;
}


Usage:

imgThumbnail.ImageUrl = ShowImage(“abc.jpg”);


Explanation:

This method, takes a string parameter as image name to display. Then it checks that if the particular image exists or not.
If the image exists on the specified path then it returns the image name with full path (userfiles/abc.jpg) else it returns the alternate image path, which is imageNotAvailable.jpg.

This way if the image is not founded on server then user will be shown a imageNotAvailable.jpg

Thats it for now.

Advertisements

2010 in review

The stats helper monkeys at WordPress.com mulled over how this blog did in 2010, and here’s a high level summary of its overall blog health:

Healthy blog!

The Blog-Health-o-Meter™ reads This blog is doing awesome!.

Crunchy numbers

Featured image

The average container ship can carry about 4,500 containers. This blog was viewed about 17,000 times in 2010. If each view were a shipping container, your blog would have filled about 4 fully loaded ships.

 

In 2010, there was 1 new post, growing the total archive of this blog to 34 posts.

The busiest day of the year was August 30th with 97 views. The most popular post that day was Username Regex Validation.

Where did they come from?

The top referring sites in 2010 were forums.asp.net, google.co.in, stackoverflow.com, google.com, and velocityreviews.com.

Some visitors came searching, mostly for mssql rename column, operator ‘+’ cannot be applied to operands of type ‘string’ and ‘method group’, username regex, ms sql rename column, and rename column mssql.

Attractions in 2010

These are the posts and pages that got the most views in 2010.

1

Username Regex Validation October 2008

2

Operator + cannot be applied to operands of type string and method group, C# error September 2008
4 comments

3

Rename a table Column in MS SQL September 2008

4

ASp.Net 2.0 GridView Delete Button Confirmation pop-up August 2008
3 comments

5

Rotate a Label Text August 2008
1 comment