473,320 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Show image randomly

2
It run w/o error but no image appears. Please help me. Here si the code I do
"
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>

<body>
<%
Function RandomImage(ImageFolderPath, ImageFileTypes, ImageDescription)
'Declare variables
Dim CompleteImagesFolderPath
Dim FileSystemObject
Dim ImageFolder
Dim Files
Dim i
Dim ImageFiles
Dim File
Dim FileName
Dim FileExtension
Dim RandomNumber

'Find the complete path to image folder by using Server.MapPath
CompleteImagesFolderPath = Server.MapPath(ImageFolderPath)

'Create an instance of the FileSystemObject which allows ASP to access the file system
Set FileSystemObject = Server.CreateObject("Scripting.FileSystemObject")

'Check that the folder containing the images exists
If Not FileSystemObject.FolderExists(CompleteImagesFolder Path) Then
RandomImage = "Error 0: Cannot find requested folder"
Set FileSystemObject = nothing
Exit Function
End If

'Get the folder containing the images
Set ImageFolder = FileSystemObject.GetFolder(CompleteImagesFolderPat h)

'Get a list of all the files within the images folder
Set Files = ImageFolder.Files

'Use a dictionary object to temporarily store the image file names
i = 1
Set ImageFiles = Server.CreateObject("Scripting.Dictionary")

'Loop through the list of files within the images folder.
'If the file has a file extension that is in the list of
'file types specified in the ImageFileTypes function parameter,
'then add the file name to the ImageFiles dictionary object
For Each File in Files
FileName = File.Name
FileExtension = Right(FileName, Len(FileName) - (InStrRev(FileName, ".")))

If InStr(1,ImageFileTypes,FileExtension,vbTextCompare ) > 0 then
ImageFiles.Add i, FileName
i = i + 1

End If

Next

'Destroy objects that are no longer required
Set ImageFolder = nothing
Set Files = nothing
Set FileSystemObject = nothing

'Initialise the random number generator
Randomize

' Check that image file(s) have been found
If ImageFiles.Count = 0 Then
RandomImage = "Error 1: Requested folder does not contain any image files"
Exit Function
End If

'Generate a random number between 1 and the number of image files
RandomNumber = Int((ImageFiles.Count) * Rnd + 1)

'Return a hyperlink to a random image file
RandomImage = "<img src=" & Chr(34) & ImagesFolderPath & ImageFiles.Item(RandomNumber) & Chr(34) & " border=" & Chr(34) & "3" & Chr(34) & " width=" & Chr(34) & "450" & Chr(34) & " height=" & Chr(34) & "600" & Chr(34) & " alt=" & Chr(34) & ImageDescription & Chr(34) & ">"

Set ImageFiles = nothing

End Function
%>

<%Response.Write RandomImage("Chi/", "jpg", "My Image")%>

<p>&nbsp;</p>
</body>

"
Nov 13 '06 #1
1 1910
sashi
1,754 Expert 1GB
It run w/o error but no image appears. Please help me. Here si the code I do
"
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>

<body>
<%
Function RandomImage(ImageFolderPath, ImageFileTypes, ImageDescription)
'Declare variables
Dim CompleteImagesFolderPath
Dim FileSystemObject
Dim ImageFolder
Dim Files
Dim i
Dim ImageFiles
Dim File
Dim FileName
Dim FileExtension
Dim RandomNumber

'Find the complete path to image folder by using Server.MapPath
CompleteImagesFolderPath = Server.MapPath(ImageFolderPath)

'Create an instance of the FileSystemObject which allows ASP to access the file system
Set FileSystemObject = Server.CreateObject("Scripting.FileSystemObject")

'Check that the folder containing the images exists
If Not FileSystemObject.FolderExists(CompleteImagesFolder Path) Then
RandomImage = "Error 0: Cannot find requested folder"
Set FileSystemObject = nothing
Exit Function
End If

'Get the folder containing the images
Set ImageFolder = FileSystemObject.GetFolder(CompleteImagesFolderPat h)

'Get a list of all the files within the images folder
Set Files = ImageFolder.Files

'Use a dictionary object to temporarily store the image file names
i = 1
Set ImageFiles = Server.CreateObject("Scripting.Dictionary")

'Loop through the list of files within the images folder.
'If the file has a file extension that is in the list of
'file types specified in the ImageFileTypes function parameter,
'then add the file name to the ImageFiles dictionary object
For Each File in Files
FileName = File.Name
FileExtension = Right(FileName, Len(FileName) - (InStrRev(FileName, ".")))

If InStr(1,ImageFileTypes,FileExtension,vbTextCompare ) > 0 then
ImageFiles.Add i, FileName
i = i + 1

End If

Next

'Destroy objects that are no longer required
Set ImageFolder = nothing
Set Files = nothing
Set FileSystemObject = nothing

'Initialise the random number generator
Randomize

' Check that image file(s) have been found
If ImageFiles.Count = 0 Then
RandomImage = "Error 1: Requested folder does not contain any image files"
Exit Function
End If

'Generate a random number between 1 and the number of image files
RandomNumber = Int((ImageFiles.Count) * Rnd + 1)

'Return a hyperlink to a random image file
RandomImage = "<img src=" & Chr(34) & ImagesFolderPath & ImageFiles.Item(RandomNumber) & Chr(34) & " border=" & Chr(34) & "3" & Chr(34) & " width=" & Chr(34) & "450" & Chr(34) & " height=" & Chr(34) & "600" & Chr(34) & " alt=" & Chr(34) & ImageDescription & Chr(34) & ">"

Set ImageFiles = nothing

End Function
%>

<%Response.Write RandomImage("Chi/", "jpg", "My Image")%>

<p>&nbsp;</p>
</body>

"
Hi there,

Kindly refer to the links attached below, make proper modification to your existing code, hope it helps. Good luck & take care.

http://www.asp101.com/samples/viewas...ndom_image.asp
http://www.codeave.com/asp/code.asp?u_log=85
Nov 13 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Nathan Given | last post by:
Hello All, I am trying to randomly change the background image of my home page but I can't seem to figure it out. Here is a snippet of my css .... BODY {background:transparent...
1
by: Ronny | last post by:
Hi, When ever I use image lists in a tree view the image list is behaving inconsistent. When I add images they first look good and clean, after I have close and open the code in the IDE the...
9
by: Michael Burtenshaw | last post by:
I would like to make a slide show using random images. The problem is my host is 250.com, and they don't support cgi-programs. Is there another way to accomplish random images?
0
by: mhospodarsky | last post by:
Hi-- I am using VB.Net 2002 for this app. I am working with Tiff and jpeg images. I have a picturebox set up that I use to view the images. I have the picture box inside of a scrollable...
2
by: bedges | last post by:
okay, the scenario: i have a header image which changes randomly across all pages in the site. that works fine. i also have an image preloader within the random header picker which theoretically...
4
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
2
by: news.tele.dk | last post by:
Hi We have a SQL server 2000 SP4 on a windows 2003 2x3Ghz XEON 4 GB ram. We have a table looking like this with currently 6 rows. Total data is aprox 10 kb i all row all together. CREATE...
3
Nert
by: Nert | last post by:
hi everyone, Could anyone please teach me how to make the randomly generated text as an image? same as the image we have seen when we were signing up in this forum (www.thescripts.com). this is my...
1
by: rsteph | last post by:
I bought a book to help me learn to use DirectX with windows programming. It's first trying to walk me through some basic windows programming and graphics before getting into DirectX. I'm trying to...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.