• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 774
  • Last Modified:

Random Image Display

Hi all,

I know this is a "dumb" question, but here goes.  Given a folder (Call it C:\Test\ for short) filled with images, I want, on page load, to load a random photo from that folder.  My code so far is attached.  Thanks in advance for any help.
Dim di As New IO.DirectoryInfo("c:\Test\")
        Dim diar1 As IO.FileInfo() = di.GetFiles()
        Dim dra As IO.FileInfo
        Dim xCount As Integer
        For Each dra In diar1
            xCount = xCount + 1
        xCount = xCount - 1
        Dim rnd As Integer, randomNum As New Random
        rnd = randomNum.Next(1, xCount)
        Image1.ImageUrl = ???

Open in new window

  • 3
  • 2
1 Solution
You need to make files in the Test folder accessible by client. Two options
1. move the test folder to be under your web root
2. the folder is outside web path, but you create a server script to read file's content from physical path and write to response's outputstream for any request

1. you will know what URL to access the folder, so you just append the random filename to the URL

Image1.ImageUrl = "filerelativepath/" & diar1(rnd).Name

2. your files will be more secure because your can determine security for each request. You can design the URL for a file request. This depends on script's code you written. For example, the script will accept a filename via a parameter named fn, thus URL should be proxyscript.aspx?fn=<<filename>>.

Image1.ImageUrl = "proxyscript.aspx?fn=" & diar1(rnd).Name

Moreover, you may need to provide a default image outside the folder (under web path) in case of no file exists in the folder, so you can switch to the default image to be displayed.

Furthermore, you code above will thow an outofrange error if no file exists. You can get the random number by this

rnd = randomNum.Next(diar1.Length) - 1

If rnd is -1, then switch to the default image's URL.

Hope this can help.
smagnus1Author Commented:
Thanks.  I understand the importance of bulletproofing the code (aka, what if no file exists, etc.) and do appreciate it, but I am simply trying to get the darn thing to work right now.  I feel relatively comfortable performing the default image task and whatnot, but I am hung @:

Image1.ImageUrl = "c:\1\" & CStr(dra(rnd))

How do I set the image to a random?  I know, for instance, that the above code simply will not do this.  I appologize for being vague.  Thanks again.
smagnus1Author Commented:
I have found a link to send this in the right direction.


Thanks again, all
Image1.ImageUrl is web relative path on IIS, not physical path on server. So, you should not assign the URL like that.

>How do I set the image to a random?

Well, I see your concept in code above. You read info of files into an array (diar1), 10 files for example and lets say position 0-9. Then, you random a number between 0 and 9 to pick up a file which is now random selected. You can get the random file like this


Dim sDraURL As String = "image/defaultimage.jpg"

If rnd >= 0 And rnd < diar1.Length Then
    sDraURL = "filerelativepath/" & diar1(rnd).Name
End If

Image1.ImageUrl = sDraURL
>I have found a link to send this in the right direction.

Glad to hear that :)

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now