473,322 Members | 1,610 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,322 software developers and data experts.

ASP.NET random images

Hi,

I hope someone can help me with this. I'm new to .NET and in fact server
side programming and have a small query I'm sure...

I'm using the System.IO Namespace with a For - each and If then statement to
randomly generate images to display in a <asp:image/image control. The
random images are selected from a fixed folder on the webserver. All works
fine but only if the images in the folder are of image format say jpg or
gif. However sometimes a Thumb.db is created in (imgRotator folder)which of
course on random selection attempts to display. I want to be able to
restrict this so only jpg and gif formats can display in my image control,
just in case any other files are created in this folder either by machine or
human error.

The code I have so far is as follows. I'm using VB.NET

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Const IMGS_DIR As String = "imgRotator/"
Dim intFileLooper As String
Dim myDirInfo As DirectoryInfo
Dim myFileInfo As FileInfo
Dim arrFileInfo As Array
Dim RandomGenerator As New Random
Dim intFileNumberToUse As Integer

'Get directory info
myDirInfo = New DirectoryInfo(Server.MapPath(IMGS_DIR))

'Get file info
arrFileInfo = myDirInfo.GetFiles()

'Use count of files to generate a random nubmer from 1 to count
intFileNumberToUse = RandomGenerator.Next(1, arrFileInfo.Length + 1)

'Loop through the array of FileInfo objects until we ge to the right
one
intFileLooper = 1
For Each myFileInfo In arrFileInfo
If intFileLooper = intFileNumberToUse Then
Image1.ImageUrl = IMGS_DIR & myFileInfo.Name
Exit For
End If
intFileLooper = intFileLooper + 1
Next
End Sub

Any ideas/work arounds or "better way of doing" would be appreciated.

Regards,

Sas
Oct 20 '06 #1
1 2635

Sahus Pilwal wrote:
Hi,

I hope someone can help me with this. I'm new to .NET and in fact server
side programming and have a small query I'm sure...

I'm using the System.IO Namespace with a For - each and If then statement to
randomly generate images to display in a <asp:image/image control. The
random images are selected from a fixed folder on the webserver. All works
fine but only if the images in the folder are of image format say jpg or
gif. However sometimes a Thumb.db is created in (imgRotator folder)which of
course on random selection attempts to display. I want to be able to
restrict this so only jpg and gif formats can display in my image control,
just in case any other files are created in this folder either by machine or
human error.

The code I have so far is as follows. I'm using VB.NET

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Const IMGS_DIR As String = "imgRotator/"
Dim intFileLooper As String
Dim myDirInfo As DirectoryInfo
Dim myFileInfo As FileInfo
Dim arrFileInfo As Array
Dim RandomGenerator As New Random
Dim intFileNumberToUse As Integer

'Get directory info
myDirInfo = New DirectoryInfo(Server.MapPath(IMGS_DIR))

'Get file info
arrFileInfo = myDirInfo.GetFiles()

'Use count of files to generate a random nubmer from 1 to count
intFileNumberToUse = RandomGenerator.Next(1, arrFileInfo.Length + 1)

'Loop through the array of FileInfo objects until we ge to the right
one
intFileLooper = 1
For Each myFileInfo In arrFileInfo
If intFileLooper = intFileNumberToUse Then
Image1.ImageUrl = IMGS_DIR & myFileInfo.Name
Exit For
End If
intFileLooper = intFileLooper + 1
Next
End Sub

Any ideas/work arounds or "better way of doing" would be appreciated.

Regards,

Sas
your code in C#:

string IMGS_DIR = "IMGS_DIR/";
DirectoryInfo myDirInfo = new
DirectoryInfo(Server.MapPath(IMGS_DIR));

Array arrFileInfo = myDirInfo.GetFiles();
Random RandomGenerator = new Random();
int intFileNumberToUse = RandomGenerator.Next(1,
arrFileInfo.Length + 1);

int intFileLooper = 1;
foreach(FileInfo myFileInfo in arrFileInfo)
{
if (intFileLooper == intFileNumberToUse)
{
Image1.ImageUrl =
myDirInfo.Name.ToString()+"/"+myFileInfo.Name.ToString();
}
intFileLooper++;
}

Nov 4 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: gene | last post by:
I have some javascript code that displays random images in random places on the page by making changes to the document's css. This works in Safari and IE, but I can't get it to work on Mozilla. ...
6
by: Olly | last post by:
I've found a basic script, however I also need to add alt and title attributes as well, how would I go about doing this? Here's the script I found: Thanks <script language="JavaScript"> <!--...
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?
5
by: Linda | last post by:
Greetings: I have found a random image script that I like. It is located here: http://www.javascriptcity.com/scripts/local/simage3.htm I'd like to edit this to have 4 different slots for...
3
by: Simon | last post by:
This problem has been driving me mad for months.... Seen a few posts on forums about it but no answers... No mention on MSDN etc. XP Pro SP1, VS.NET (c#) .Net framework 1.1, IIS 5.1. In a...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
7
by: Brian | last post by:
Hi there I have been looking for a script that can randomly rotate 8 different images on 1 page. I used to have a script that did this but can't find it now. I have found loads of script that...
6
by: newsteve1 | last post by:
hi, this should be simple but its stumping me, I am trying to make a slideshow that pulls random images at a random time interval (between 1 and 4 seconds). The images part works fine, and I...
1
by: bn4 | last post by:
I have been trying to modify a script that i had working in the past but have lost.. ORIGINAL URL: http://www.thescripts.com/forum/thread147097.html STEPS: I want it to do the following:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.