ASP.NET random images

Discussion in 'ASP .Net' started by Sahus Pilwal, Oct 20, 2006.

  1. Sahus Pilwal

    Sahus Pilwal Guest

    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
     
    Sahus Pilwal, Oct 20, 2006
    #1
    1. Advertising

  2. Sahus Pilwal

    Guest

    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, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darren Clark

    Random NOt random?

    Darren Clark, Jun 24, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    469
    mikeb
    Jun 24, 2004
  2. Maziar Aflatoun

    Random not really random...

    Maziar Aflatoun, Aug 4, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    26,751
    Maziar Aflatoun
    Aug 5, 2004
  3. Lars-Erik Aabech
    Replies:
    8
    Views:
    860
    Lars-Erik Aabech
    Apr 28, 2005
  4. globalrev
    Replies:
    4
    Views:
    782
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,218
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page