(Advertisment)
German Wear Discount Shop - Click Here
[Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


Get a Random Image from Collection of Images using ASP .NET

Written by kamal on Apr 19, 2007
How to get a Random Image from a set of images?

Explanation:


1. Add an Image Tag to your.

<asp:Image ID="Image1" runat="server"/>



2. Create a Collection of your Images using string Array.

string[] images = new string[] { "/images/Backcolor.gif", "/images/Bold.gif", "/images/cut.gif" }; 


3. Use System.Random object to get a Rendom Image path.

System.Random r = new Random();
string returningImage=images[r.Next(0,images.Length)]; 


4. Create a Url from the given path:

string path = Request.ApplicationPath + GetRendomImage();

5. Assign the return path to your Image Tag.

Image1.ImageUrl = path;
Image1.AlternateText = path;

Now, the Complete code behind will look like this:

protected void Page_Load(object sender, EventArgs e)
{
string path = Request.ApplicationPath + GetRendomImage();
Image1.ImageUrl = path;
Image1.AlternateText = path;
}
protected string GetRendomImage()
{
System.Random r = new Random();
string[] images = new string[] { "/images/Backcolor.gif", "/images/Bold.gif", "/images/cut.gif" };
return images[r.Next(0,images.Length)];
}

Visitors/Readers Comments
(for questions please use The Forum)



santha
good

25/10/2008 01:28:00 UTC

R.subramaniam
this is very useful.
 

24/03/2009 05:01:03 UTC

latha

this code is very useful and simple,  Working very very fine


Thanku

06/04/2009 04:36:23 UTC




Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)