Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

display random images

Posted on 2004-04-28
3
Medium Priority
?
437 Views
Last Modified: 2010-04-07
hey there;

i'd like to display random images each time my asp.net page is reloaded.

these images are all stored in a specified folder on the server in either jpg, gif and png formats.

i simply wish to reach in the folder, grab a random file, then display it.  

must be vb.net code as i'm not a c# programmer.  also, i'm *not* looking for any xml solutions (like the ad rotator uses).

thanks.
0
Comment
Question by:vbnewbie01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 4

Expert Comment

by:pml_siva
ID: 10937752

string[]  sDir = Directory.GetFiles(@"E:\images");
                  
Random rnd = new Random();
int iRndIndex = rnd.Next(sDir.Length);
string strDisp = "<img src= '" + sDir[iRndIndex] + "'";
Response.Write(strDisp);
0
 
LVL 4

Accepted Solution

by:
pml_siva earned 400 total points
ID: 10937814
Dim sDir As String() = Directory.GetFiles("E:\images")
 Dim rnd As Random = New Random
 Dim iRndIndex As Integer = rnd.Next(sDir.Length)
 Dim strDisp As String = "<img src = '" & sDir(iRndIndex) & "'"
 Response.Write(strDisp)
0
 
LVL 1

Author Comment

by:vbnewbie01
ID: 10941690
strong work, pml siva;.  thanks.

however, i made a couple of modifications in case anyone else is looking to do a similar thing by having your image displayed using an asp.net control.

first, i put the code in my Page_Load event, then i had to append the opening line to read System.IO (in front of the DIrectory.GetFiles)

next, i put an image control (asp:image id="imgRandomPhoto" runat="server") in my html code.

i called it in the Page_Load event using:

imgRandomPhoto.imageURL=sDir(iRndIndex)

entire code written below
---------------------------------------------------

Dim sDir As String() = System.IO.Directory.GetFiles("C:\Inetpub\wwwroot\Website\uploaded_images")

Dim rnd As Random = New Random
Dim iRndIndex As Integer = rnd.Next(sDir.Length)
Dim strDisp As String = "<img src = '" & sDir(iRndIndex) & "'"
            
imgRandomPhoto.imageURL=sDir(iRndIndex)

----------------------------------------------------

thanks again.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question