If IsPostBack = False Then
Dim di As New IO.DirectoryInfo(System.IO.Path.Combine(Server.MapPath("/"), "Images")) 'Load your images path'
Dim diar1 As IO.FileInfo() = di.GetFiles("*.*") 'change to only include certain image extensions'
Dim dra As IO.FileInfo
Dim rnd As New Random()
Dim pictureNumber As Integer = rnd.Next(0, diar1.Count) 'get a random number'
dra = diar1(pictureNumber) 'select the index of the files for the random number'
Image1.ImageUrl = ResolveUrl("~/Images/" & dra.Name) 'map the url'
End If
Try this and please remember to mark as answered if it works for you.
I have that code running and working. Are you sure you have the top part?
Dim di As New IO.DirectoryInfo(System.IO.Path.Combine(Server.MapPath("/"), "Images")) 'Load your images path'
Dim diar1 As IO.FileInfo() = di.GetFiles("*.*") 'change to only include certain image extensions'
ICollection.CountGets the number of elements contained in the
Array.
ICollection.Count is an explicit interface implementation so the array has to be cast as ICollection explicitly before gaining access to the Count property.
Therefore instead of Count just use Length. You save yourself from the cast.
BTW, this is why I don't usually volunteer code like this. People need to go through the process of translating instructions in plain English to code. Giving code like this usually teaches very little, if any.
Sorry j_007, that's as simple as it gets. I recommend that you study the language syntax more closely so as to have less trouble understanding code as you move along in your project(s).
Sorry j_007, that's as simple as it gets. I recommend that you study the language syntax more closely so as to have less trouble understanding code as you move along in your project(s).
Haha, yeah... I see your point. :) I was just trying to help him out some since I already had the code available.
j_007
0 Points
7 Posts
choose pictures randomly to preview in Image
Feb 21, 2013 05:34 PM|LINK
Hey there,,,
i'm newbie in asp,net
and i'm working in vb.net
so i want to make a web site
one of it's content is an image control
that choose randomly from many pictures
which are located in folder in the project ....
what is the code
so anyone can help .
webJose
Member
730 Points
186 Posts
Re: choose pictures randomly to preview in Image
Feb 21, 2013 05:45 PM|LINK
j_007
0 Points
7 Posts
Re: choose pictures randomly to preview in Image
Feb 21, 2013 05:48 PM|LINK
can you explain to me more
please
speshulk926
Participant
904 Points
204 Posts
Re: choose pictures randomly to preview in Image
Feb 21, 2013 06:05 PM|LINK
If IsPostBack = False Then Dim di As New IO.DirectoryInfo(System.IO.Path.Combine(Server.MapPath("/"), "Images")) 'Load your images path' Dim diar1 As IO.FileInfo() = di.GetFiles("*.*") 'change to only include certain image extensions' Dim dra As IO.FileInfo Dim rnd As New Random() Dim pictureNumber As Integer = rnd.Next(0, diar1.Count) 'get a random number' dra = diar1(pictureNumber) 'select the index of the files for the random number' Image1.ImageUrl = ResolveUrl("~/Images/" & dra.Name) 'map the url' End IfTry this and please remember to mark as answered if it works for you.
j_007
0 Points
7 Posts
Re: choose pictures randomly to preview in Image
Feb 21, 2013 06:19 PM|LINK
Sir ,
I have tried to put this code in form_load
put there is an error appear
in this line :
Dim pictureNumber As Integer = rnd.Next(0, diar1.Count) 'get a random number'
which tells that :
count is not member of System.array
speshulk926
Participant
904 Points
204 Posts
Re: choose pictures randomly to preview in Image
Feb 21, 2013 06:28 PM|LINK
I have that code running and working. Are you sure you have the top part?
Dim di As New IO.DirectoryInfo(System.IO.Path.Combine(Server.MapPath("/"), "Images")) 'Load your images path' Dim diar1 As IO.FileInfo() = di.GetFiles("*.*") 'change to only include certain image extensions'also, check this out:
http://msdn.microsoft.com/en-us/library/system.array.aspx
ICollection.CountGets the number of elements contained in the Array.
webJose
Member
730 Points
186 Posts
Re: choose pictures randomly to preview in Image
Feb 21, 2013 06:44 PM|LINK
ICollection.Count is an explicit interface implementation so the array has to be cast as ICollection explicitly before gaining access to the Count property.
Therefore instead of Count just use Length. You save yourself from the cast.
BTW, this is why I don't usually volunteer code like this. People need to go through the process of translating instructions in plain English to code. Giving code like this usually teaches very little, if any.
j_007
0 Points
7 Posts
Re: choose pictures randomly to preview in Image
Feb 21, 2013 07:35 PM|LINK
can you give me more simple code to do this
webJose
Member
730 Points
186 Posts
Re: choose pictures randomly to preview in Image
Feb 21, 2013 08:25 PM|LINK
My point exactly.
Sorry j_007, that's as simple as it gets. I recommend that you study the language syntax more closely so as to have less trouble understanding code as you move along in your project(s).
speshulk926
Participant
904 Points
204 Posts
Re: choose pictures randomly to preview in Image
Feb 22, 2013 04:04 AM|LINK
Haha, yeah... I see your point. :) I was just trying to help him out some since I already had the code available.