473,804 Members | 3,031 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Show image randomly

2 New Member
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(Ima geFolderPath, ImageFileTypes, ImageDescriptio n)
'Declare variables
Dim CompleteImagesF olderPath
Dim FileSystemObjec t
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
CompleteImagesF olderPath = Server.MapPath( ImageFolderPath )

'Create an instance of the FileSystemObjec t which allows ASP to access the file system
Set FileSystemObjec t = Server.CreateOb ject("Scripting .FileSystemObje ct")

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

'Get the folder containing the images
Set ImageFolder = FileSystemObjec t.GetFolder(Com pleteImagesFold erPath)

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

'Use a dictionary object to temporarily store the image file names
i = 1
Set ImageFiles = Server.CreateOb ject("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(FileN ame, ".")))

If InStr(1,ImageFi leTypes,FileExt ension,vbTextCo mpare) > 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 FileSystemObjec t = nothing

'Initialise the random number generator
Randomize

' Check that image file(s) have been found
If ImageFiles.Coun t = 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) & ImagesFolderPat h & 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) & ImageDescriptio n & Chr(34) & ">"

Set ImageFiles = nothing

End Function
%>

<%Response.Writ e RandomImage("Ch i/", "jpg", "My Image")%>

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

"
Nov 13 '06 #1
1 1928
sashi
1,754 Recognized Expert Top Contributor
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(Ima geFolderPath, ImageFileTypes, ImageDescriptio n)
'Declare variables
Dim CompleteImagesF olderPath
Dim FileSystemObjec t
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
CompleteImagesF olderPath = Server.MapPath( ImageFolderPath )

'Create an instance of the FileSystemObjec t which allows ASP to access the file system
Set FileSystemObjec t = Server.CreateOb ject("Scripting .FileSystemObje ct")

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

'Get the folder containing the images
Set ImageFolder = FileSystemObjec t.GetFolder(Com pleteImagesFold erPath)

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

'Use a dictionary object to temporarily store the image file names
i = 1
Set ImageFiles = Server.CreateOb ject("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(FileN ame, ".")))

If InStr(1,ImageFi leTypes,FileExt ension,vbTextCo mpare) > 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 FileSystemObjec t = nothing

'Initialise the random number generator
Randomize

' Check that image file(s) have been found
If ImageFiles.Coun t = 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) & ImagesFolderPat h & 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) & ImageDescriptio n & Chr(34) & ">"

Set ImageFiles = nothing

End Function
%>

<%Response.Writ e RandomImage("Ch i/", "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
44336
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 url(../images/homepagebg3.jpg) no-repeat; font:normal 90% Arial, Helvetica, sans-serif; color:#263158;}
1
3343
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 icons have this blue shadow on them and they look really bad. I can then remove the icons and add them again and for some reason they look good but only until I add new icons to the same image list. Another thing I noticed is that when I add the...
9
12406
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
2670
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 panel.There are two viewing preferences available: to view the image with scrollbars (the original image unsized), and to view the iamge without scrollbars (the image resized to fit within the panel's viewing range.) When I resize the image so that...
2
2076
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 loads the header image before the rest of the graphical content on the page. however, what appears to be happening, judging by the status bar messages, is that the banner always loads last. here's the random header code -
4
2761
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. It should put a blue and yellow box on the page with "This is a test" as part of the picture. But what I get is a broken Gif. The other problem is that I can't view the source???? The view source is disabled for this page. What causes this?
2
3160
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 TABLE . ( IDENTITY (1, 1) NOT NULL , NOT NULL ,
3
1215
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 code when i generate the random text. function random_password(){ $rand_pword = mt_rand(0,9999999); $pword = substr(md5($rand_pword), 0, 5); return $pword; }
1
3802
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 expand on one of the example programs in the book but I'm having some problems. I can get a frame to appear, then an image gets randomly placed in the box. It is suppose to move right, then when it hits the edge move left; it should repeate this...
0
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10564
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10320
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10308
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10073
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9134
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2981
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.