trying to create a list of 12 random images from a folder that contains hundreds that will bind to a datalist. My code below shows all images in the file. I have gotten as far as adding a new random but i am stuck there. Private Private Sub BindList()
Dim imagePaths = Directory.GetFiles(Server.MapPath("Images/Burning"))
Dim imageNames = New String(imagePaths.Length - 1) {}
Dim randy As New Random
For i As Integer = 0 To imagePaths.Length - 1
imageNames(i) = imagePaths(i).Substring(imagePaths(i).LastIndexOf("\") + 1)
Next
Dim dt = New DataTable()
dt.Columns.Add("ImageName", GetType(String))
dt.Columns.Add("ImagePath", GetType(String))
For Each imgName In imageNames
Dim dr As DataRow = dt.NewRow()
dr("ImageName") = RemoveExtension(imgName)
dr("ImagePath") = "Images/Burning/" & imgName
dt.Rows.Add(dr)
Next
DataList1.DataSource = dt
DataList1.DataBind()
End Sub
Dim numberToTake = <you can set the default number of files to be selected>
Dim imagePaths = Directory.GetFiles(Server.MapPath("Images/Burning"))
Dim randomFiles = imagePaths.Select(Function(f As String) f.Split("\").Last()).OrderBy(Function(f As String) r.Next()).Take(numberToTake)
DataList1.DataSource = randomFiles
DataList1.DataBind()
Data type(s) of the type parameter(s) in extension method 'Public Function OrderBy(Of TKey)(keySelector As System.Func(Of String, TKey)) As System.Linq.IOrderedEnumerable(Of String)' defined in 'System.Linq.Enumerable' cannot be inferred from these arguments.
Specifying the data type(s) explicitly might correct this error.
'r' is not accessible in this context because it is 'Friend'.
I'm not too familiar with VB.NET but i've given it my best shot. I've created a Function which returns a random sequence of integer values. You specify the number of integers you wish to take and the maximum number to choose from:
Private Function GetSequence(ByVal take As Integer, ByVal total As Integer) As List(Of Integer)
Dim sequence As New List(Of Integer)
Dim random As New Random()
While (take > sequence.Count)
Dim number As Integer = random.Next(1, total)
If sequence.Contains(number) Then
Continue While
End If
sequence.Add(number)
End While
Return sequence
End Function
To use this, you will get the files from your chosen directory e.g.
Dim files As String() = Directory.GetFiles(Server.MapPath("Images/Burning"))
Imports System.IO
Public Class RandomImages
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim r As New Random()
Dim numberToTake = 3 'change this to what you want
Dim imagePaths = Directory.GetFiles(Server.MapPath("~/Images/Burning/"))
Dim randomFiles = imagePaths.Select(Function(f As String) f.Split("\").Last()).OrderBy(Function(f As String) r.Next()).Take(numberToTake)
DataList1.DataSource = randomFiles
DataList1.DataBind()
End Sub
End Class
Still can't get it work like I need. I need both name and path and the datatable, since it is used by jquery & lightbox . Right now my code works but returns the whole directory on
Dim
imagePaths = Directory.GetFiles(Server.MapPath("Images/Burning"))
I guess what I am really going for is how to select a set number of random images for the imagepaths.
Even if I have to loop through the directory to return 12 let's say.
Went about a slightly different way in the end but it produces what I need.
'select random number of files from imageNames
Dim imagePaths() As String = Directory.GetFiles(Server.MapPath("Images/Burning"))
Dim imageNames As New List(Of String)
For Each Path As String In imagePaths
imageNames.Add(Path.Substring(Path.LastIndexOf("\") + 1))
Next
Static R As New Random
While imageNames.Count > 6
imageNames.RemoveAt(R.Next(imageNames.Count))
End While
Dim dt = New DataTable()
dt.Columns.Add("ImageName", GetType(String))
dt.Columns.Add("ImagePath", GetType(String))
For Each imgName In imageNames
Dim dr As DataRow = dt.NewRow()
dr("ImageName") = RemoveExtension(imgName)
dr("ImagePath") = "Images/Burning/" & imgName
dt.Rows.Add(dr)
Next
DataList1.DataSource = dt
DataList1.DataBind()
Marked as answer by bicosteel on Jan 30, 2013 12:45 PM
bicosteel
Member
12 Points
33 Posts
How do I create a random list of images from a folder
Jan 25, 2013 05:39 PM|LINK
trying to create a list of 12 random images from a folder that contains hundreds that will bind to a datalist. My code below shows all images in the file. I have gotten as far as adding a new random but i am stuck there. Private Private Sub BindList() Dim imagePaths = Directory.GetFiles(Server.MapPath("Images/Burning")) Dim imageNames = New String(imagePaths.Length - 1) {} Dim randy As New Random For i As Integer = 0 To imagePaths.Length - 1 imageNames(i) = imagePaths(i).Substring(imagePaths(i).LastIndexOf("\") + 1) Next Dim dt = New DataTable() dt.Columns.Add("ImageName", GetType(String)) dt.Columns.Add("ImagePath", GetType(String)) For Each imgName In imageNames Dim dr As DataRow = dt.NewRow() dr("ImageName") = RemoveExtension(imgName) dr("ImagePath") = "Images/Burning/" & imgName dt.Rows.Add(dr) Next DataList1.DataSource = dt DataList1.DataBind() End Subgeniusvishal
All-Star
15157 Points
2950 Posts
Re: How do I create a random list of images from a folder
Jan 25, 2013 06:37 PM|LINK
Refer this link:
http://www.dotnetspider.com/resources/18034-Shuffle-Images-DataList-using-Java-Script.aspx
and http://www.aspdotnet-suresh.com/2011/05/how-to-bind-images-from-folder-to.html
My Website
www.dotnetvishal.com
Mikesdotnett...
All-Star
155593 Points
19979 Posts
Moderator
MVP
Re: How do I create a random list of images from a folder
Jan 27, 2013 08:12 PM|LINK
Try this:
Dim numberToTake = <you can set the default number of files to be selected> Dim imagePaths = Directory.GetFiles(Server.MapPath("Images/Burning")) Dim randomFiles = imagePaths.Select(Function(f As String) f.Split("\").Last()).OrderBy(Function(f As String) r.Next()).Take(numberToTake) DataList1.DataSource = randomFiles DataList1.DataBind()Web Pages CMS | My Site | Twitter
bicosteel
Member
12 Points
33 Posts
Re: How do I create a random list of images from a folder
Jan 28, 2013 01:16 PM|LINK
tried the code but i get
Data type(s) of the type parameter(s) in extension method 'Public Function OrderBy(Of TKey)(keySelector As System.Func(Of String, TKey)) As System.Linq.IOrderedEnumerable(Of String)' defined in 'System.Linq.Enumerable' cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.
'r' is not accessible in this context because it is 'Friend'.
Any idea what I did wrong with your code?
michaelalex7
Member
170 Points
35 Posts
Re: How do I create a random list of images from a folder
Jan 28, 2013 10:25 PM|LINK
Hiya,
I'm not too familiar with VB.NET but i've given it my best shot. I've created a Function which returns a random sequence of integer values. You specify the number of integers you wish to take and the maximum number to choose from:
Private Function GetSequence(ByVal take As Integer, ByVal total As Integer) As List(Of Integer)
Dim sequence As New List(Of Integer)
Dim random As New Random()
While (take > sequence.Count)
Dim number As Integer = random.Next(1, total)
If sequence.Contains(number) Then
Continue While
End If
sequence.Add(number)
End While
Return sequence
End Function
To use this, you will get the files from your chosen directory e.g.
Dim files As String() = Directory.GetFiles(Server.MapPath("Images/Burning"))
Dim sequence = GetSequence(12, files.Count())
For i As Integer = 0 To sequence.Count - 1
Dim index as Integer = sequence(i)
Dim filename = files(index)
Next
Hope this helps,
Mike
Mikesdotnett...
All-Star
155593 Points
19979 Posts
Moderator
MVP
Re: How do I create a random list of images from a folder
Jan 29, 2013 04:53 AM|LINK
Oops - I missed a line out from my working version:
Here's the whole (working) code for for the aspx and the code behind:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="RandomImages.aspx.vb" Inherits="VBWebFormsTests.RandomImages" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList ID="DataList1" runat="server"> <ItemTemplate> <asp:Image ID="Image1" runat="server" ImageUrl='<%# string.Format("~/images/{0}", Container.DataItem) %>' /> </ItemTemplate> </asp:DataList> </div> </form> </body> </html>Code behind:
Imports System.IO Public Class RandomImages Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim r As New Random() Dim numberToTake = 3 'change this to what you want Dim imagePaths = Directory.GetFiles(Server.MapPath("~/Images/Burning/")) Dim randomFiles = imagePaths.Select(Function(f As String) f.Split("\").Last()).OrderBy(Function(f As String) r.Next()).Take(numberToTake) DataList1.DataSource = randomFiles DataList1.DataBind() End Sub End ClassWeb Pages CMS | My Site | Twitter
bicosteel
Member
12 Points
33 Posts
Re: How do I create a random list of images from a folder
Jan 29, 2013 03:00 PM|LINK
Still can't get it work like I need. I need both name and path and the datatable, since it is used by jquery & lightbox . Right now my code works but returns the whole directory on
Dim imagePaths = Directory.GetFiles(Server.MapPath("Images/Burning"))
I guess what I am really going for is how to select a set number of random images for the imagepaths. Even if I have to loop through the directory to return 12 let's say.
Mikesdotnett...
All-Star
155593 Points
19979 Posts
Moderator
MVP
Re: How do I create a random list of images from a folder
Jan 29, 2013 07:14 PM|LINK
The next line of my code does exactly that. You pass the number you want to select in to the Take() method:
Dim randomFiles = imagePaths.Select(Function(f As String) f.Split("\").Last()).OrderBy(Function(f As String) r.Next()).Take(12)That gives you a collection of strings which represent the file names. You can bind that to a DataList.
Web Pages CMS | My Site | Twitter
bicosteel
Member
12 Points
33 Posts
Re: How do I create a random list of images from a folder
Jan 30, 2013 12:45 PM|LINK
Went about a slightly different way in the end but it produces what I need. 'select random number of files from imageNames Dim imagePaths() As String = Directory.GetFiles(Server.MapPath("Images/Burning")) Dim imageNames As New List(Of String) For Each Path As String In imagePaths imageNames.Add(Path.Substring(Path.LastIndexOf("\") + 1)) Next Static R As New Random While imageNames.Count > 6 imageNames.RemoveAt(R.Next(imageNames.Count)) End While Dim dt = New DataTable() dt.Columns.Add("ImageName", GetType(String)) dt.Columns.Add("ImagePath", GetType(String)) For Each imgName In imageNames Dim dr As DataRow = dt.NewRow() dr("ImageName") = RemoveExtension(imgName) dr("ImagePath") = "Images/Burning/" & imgName dt.Rows.Add(dr) Next DataList1.DataSource = dt DataList1.DataBind()