| | |
Random Display of Images
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
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.
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.
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
ASP.NET Syntax (Toggle Plain Text)
Dim RandomClass As New Random() Dim rndNumber As Integer rndNumber = (RandomClass.Next(01, 99))
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?
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
Here is something that you should probably use to grab the image:
Keep in mind I use T_*.jpg for all my "thumbnails".
This code is untested, so let me know if you have problems.
Keep in mind I use T_*.jpg for all my "thumbnails".
ASP.NET Syntax (Toggle Plain Text)
'Random Image Public Function RndImage() As String Dim rnd As New Random() 'The below is for image names that start with "T_" and are jpeg. Dim ID As String = "T_*.jpg" Dim IDArr() As String = ID.split("*") Dim images() As String = System.IO.Directory.GetFiles((Server.MapPath("\images\"), ID) Dim imageToDisplay As String if CountPhotos(ID) < 1 then imageToDisplay = "nopicture" Do While Len(imageToDisplay) < 1 imageToDisplay = images(rnd.Next(images.Length)) loop Return ("\images\" & IDArr(0) & imageToDisplay & IDArr(1)) End Function Public Function CountPhotos(ByVal ID As Long) As Integer Dim numero As Integer = 0 Dim strPath As String = (Server.MapPath("\images\") & ID) Dim strFiles As String = Dir(strPath) Do Until Len(strFiles) = 0 numero += 1 if numero > 0 then exit do Loop CountPhotos = numero End Function
![]() |
Similar Threads
- Starting Python (Python)
- Random Images on Refresh (JavaScript / DHTML / AJAX)
- display random images from folder (PHP)
- Random text and images (JavaScript / DHTML / AJAX)
- Re: Starting Python (Python)
- Python Error-- cannot find file (Python)
- Random IMG (PHP)
- looking for a free or low cost program to randomly display text and/or images (PHP)
Other Threads in the ASP.NET Forum
- Previous Thread: trigger example???
- Next Thread: how to delete selected value of dropdownlist
| Thread Tools | Search this Thread |
Tag cloud for ASP.NET
.net 2.0 3.5 ajax alltypeofvideos anathor appliances application asp asp.net beginner box browser businesslogiclayer button c# cac checkbox class commonfunctions control countryselector dataaccesslayer database datagrid datagridview datalist deployment development dgv dialog dropdownlist dropdownmenu dynamic dynamically edit embeddingactivexcontrol feedback fileuploader fill findcontrol flash folder form gridview gudi iis image javascript list listbox login maps microsoft mobile mouse mssql nameisnotdeclared news novell numerical opera panelmasterpagebuttoncontrols parent problem project radio redirect registration relationaldatabases reportemail richtextbox schoolproject search security select sessionvariables silverlight smoobjects software sql sql-server sqlserver2005 ssl tracking treeview validatedate validation vb.net videos vista visualstudio vs2008 web webapplications webdevelopment webprogramming webservice wizard xsl






