8 Replies - 1470 Views - Last Post: 30 September 2009 - 06:38 AM Rate Topic: -----

#1 Dave W  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 18-October 08

Random Images

Posted 28 September 2009 - 09:30 AM

I have a site which I am trying to show 7 random images (when the page loads) from a Directory which has 360 images in.

Anyone point me in the right direction please, I have being searching the net and only got something to show 1 image at the moment.

Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Random Images

#2 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • Posts: 530
  • Joined: 17-September 08

Re: Random Images

Posted 28 September 2009 - 09:54 AM

What do you have so far?
Was This Post Helpful? 0
  • +
  • -

#3 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 102
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Re: Random Images

Posted 28 September 2009 - 09:56 AM

Hi, :D

Are you using MySQL?
Was This Post Helpful? 0
  • +
  • -

#4 Dave W  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 18-October 08

Re: Random Images

Posted 28 September 2009 - 10:16 AM

	Private Function PickImageFromDirectory(ByVal directoryPath As String) As String

		Dim dirInfo As New DirectoryInfo(Server.MapPath(directoryPath))
		Dim fileList() As FileInfo = dirInfo.GetFiles()
		Dim numberOfFiles As Integer = fileList.Length

		'Pick a random image from the list
		Dim rnd As New Random
		Dim randomFileIndex As Integer = rnd.Next(numberOfFiles)
		Dim imageFileName As String = fileList(randomFileIndex).Name
		Dim fullImageFileName As String = Path.Combine(directoryPath, imageFileName)

		Return fullImageFileName

	End Function


I am reading straight from the Directory and not using mysql. I have tried putting a loop in so I get the amount of images I need but to no joy.

This post has been edited by Dave W: 28 September 2009 - 10:17 AM

Was This Post Helpful? 0
  • +
  • -

#5 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • Posts: 530
  • Joined: 17-September 08

Re: Random Images

Posted 28 September 2009 - 10:25 AM

Well, you can shove it in a loop, or you can just call the method 7 times in a row.

Is the problem that you only get one image, or is it that when you loop it you get the same image seven times?

Also, make sure that you aren't overwriting the image each time you loop.
Was This Post Helpful? 0
  • +
  • -

#6 Dave W  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 18-October 08

Re: Random Images

Posted 28 September 2009 - 10:28 AM

Same image 7 times when I loop it.
Was This Post Helpful? 0
  • +
  • -

#7 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • Posts: 530
  • Joined: 17-September 08

Re: Random Images

Posted 28 September 2009 - 10:33 AM

That is... odd. Mind showing your loop function? Also, does it show a different image each time you run it, or do you get a different image?
Was This Post Helpful? 0
  • +
  • -

#8 Dave W  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 18-October 08

Re: Random Images

Posted 30 September 2009 - 05:12 AM

Managed to figure out where the problem is.

Dim randomFileIndex As Integer = rnd.Next(numberOfFiles)

now I am trying to sort a loop out that will allow 7 images. the current one is giving me 2 images at random.
Was This Post Helpful? 0
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Random Images

Posted 30 September 2009 - 06:38 AM

here is how I would do it...

I will first give you the code that I wrote in C#. I will then give the code that an online converter gave me for VB.Net. If the VB.Net doesn't work, you can look at the C# and do the conversion yourself.

Also, this is in 3.5.

public partial class _Default : System.Web.UI.Page
{
    List<string> list = new List<string>();
    FileInfo[] fileArray;

    protected void Page_Load(object sender, EventArgs e)
    {
        string path = Server.MapPath("~/images");
        DirectoryInfo di = new DirectoryInfo(path);
        fileArray = di.GetFiles("*.jpg");

        for (int i = 0; i < 7; i++)
        {
            string imageName = PickImageFromDirectory(path);

            AddImageToPage(imageName, i);
        }
    }

    private string PickImageFromDirectory(string path)
    {
        string filename = string.Empty;
        Random rand = new Random();

        do
        {
            int nextID = rand.Next(0, fileArray.Length);
            FileInfo fi = fileArray.ElementAt(nextID);
            filename = fi.Name;
        } while (list.Contains(filename));

        list.Add(filename);

        return filename;
    }

    private void AddImageToPage(string imageName, int id)
    {
        Image i = new Image();
        i.ID = id.ToString();
        i.ImageUrl = GetNormalizedURL() + "/images/" + imageName;
        divImages.Controls.Add(i);
    }

    private string GetNormalizedURL()
    {
        string normalizedUrl = string.Format("{0}://>/{1}", Request.Url.Scheme, Request.Url.Host);
        if (!((Request.Url.Scheme == "http" && Request.Url.Port == 80) || (Request.Url.Scheme == "https" && Request.Url.Port == 443)))
        {
            normalizedUrl += ":" + Request.Url.Port;
        }

        return normalizedUrl;
    }
}




now for the VB.Net code
Public partial Class _Default
	 Inherits System.Web.UI.Page
 
    Public stringList As Generic.List(Of String) = New Generic.List(Of String)
    Dim fileArray() As FileInfo

    Protected  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim path As String =  Server.MapPath("~/images") 
        Dim di As DirectoryInfo =  New DirectoryInfo(path) 
        fileArray = di.GetFiles("*.jpg")
 
        Dim i As Integer
        For  i = 0 To  7- 1  Step  i + 1
            Dim imageName As String =  PickImageFromDirectory(path) 
 
            AddImageToPage(imageName, i)
        Next
    End Sub

    Private Function PickImageFromDirectory(ByVal path As String) As String
            Dim filename As String =  String.Empty 
            Dim rand As Random =  New Random() 
 
            Do 
                Dim nextID As Integer =  rand.Next(0,fileArray.Length) 
                Debug.WriteLine(nextID)
                Dim fi As FileInfo =  fileArray.ElementAt(nextID) 
                filename = fi.Name
            Loop Function list.Contains(ByValfilename) As While
            Dim i As Image =  New Image() 
            i.ID = id.ToString()
            i.ImageUrl = GetNormalizedURL() + "/images/" + imageName
            divImages.Controls.Add(i)
            End Function
 
        Private Function GetNormalizedURL() As String
            Dim normalizedUrl As String =  String.Format("{0}://>/{1}",Request.Url.Scheme,Request.Url.Host) 
            If Not ((Request.Url.Scheme = "http" And Request.Url.Port = 80) ||(Request.Url.Scheme = "https" And Request.Url.Port = 443)) Then
                normalizedUrl += ":" + Request.Url.Port
            End If
 
            Return normalizedUrl
        End Function
End Class


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1