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
        Next
 
        xCount = xCount - 1
 
        Dim rnd As Integer, randomNum As New Random
        rnd = randomNum.Next(1, xCount)
        
 
        Image1.ImageUrl = ???

Open in new window

smagnus1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gnoonCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
0
smagnus1Author Commented:
I have found a link to send this in the right direction.

http://juranger.spaces.live.com/Blog/cns!8B711EA4E33A8804!187.entry

Thanks again, all
0
gnoonCommented:
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
0
gnoonCommented:
>I have found a link to send this in the right direction.

Glad to hear that :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.