Random Display of Images

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 73
Reputation: ebabes is an unknown quantity at this point 
Solved Threads: 0
ebabes's Avatar
ebabes ebabes is offline Offline
Junior Poster in Training

Random Display of Images

 
0
  #1
Feb 12th, 2008
How can I display an image randomly in ASP.Net?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Random Display of Images

 
0
  #2
Feb 13th, 2008
Your easiest and quickest way to do this would be to name all your images a number. Then use the "Random()" command and generate a number between 0 and the amount of files you have.

Otherwise you can loop through each file within the folder and put it into an array. Then do the Random() and selected the array index for your random image.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 73
Reputation: ebabes is an unknown quantity at this point 
Solved Threads: 0
ebabes's Avatar
ebabes ebabes is offline Offline
Junior Poster in Training

Re: Random Display of Images

 
0
  #3
Feb 14th, 2008
Thanks. I forgot how to make use of Random function already. can I have a sample code for it? Thanks again...
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Random Display of Images

 
0
  #4
Feb 14th, 2008
  1. Dim RandomClass As New Random()
  2. Dim rndNumber As Integer
  3.  
  4. rndNumber = (RandomClass.Next(01, 99))
This will choose a random number between 1 and 99. Keep in mind, when doing images, you should stick to a proper format and if you have less than 100 images, and plan on keeping it that way, include one zero in place of the first digit:

01 - 99
001 - 999
0001 - 9999

Now you don't honestly HAVE to follow this, but it prevents errors dramatically. Plus, if you get an error, do you really want to go through and rename a thousand images?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Random Display of Images

 
0
  #5
Feb 14th, 2008
Here is something that you should probably use to grab the image:
Keep in mind I use T_*.jpg for all my "thumbnails".
  1. 'Random Image
  2. Public Function RndImage() As String
  3. Dim rnd As New Random()
  4. 'The below is for image names that start with "T_" and are jpeg.
  5. Dim ID As String = "T_*.jpg"
  6. Dim IDArr() As String = ID.split("*")
  7. Dim images() As String = System.IO.Directory.GetFiles((Server.MapPath("\images\"), ID)
  8. Dim imageToDisplay As String
  9. if CountPhotos(ID) < 1 then imageToDisplay = "nopicture"
  10. Do While Len(imageToDisplay) < 1
  11. imageToDisplay = images(rnd.Next(images.Length))
  12. loop
  13. Return ("\images\" & IDArr(0) & imageToDisplay & IDArr(1))
  14. End Function
  15.  
  16. Public Function CountPhotos(ByVal ID As Long) As Integer
  17. Dim numero As Integer = 0
  18. Dim strPath As String = (Server.MapPath("\images\") & ID)
  19. Dim strFiles As String = Dir(strPath)
  20. Do Until Len(strFiles) = 0
  21. numero += 1
  22. if numero > 0 then exit do
  23. Loop
  24. CountPhotos = numero
  25. End Function
  26.  
This code is untested, so let me know if you have problems.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for ASP.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC