Random Background Image

Got Random Background Image Working but need help running with CSS

Page 1 of 1

1 Replies - 6252 Views - Last Post: 23 November 2008 - 10:25 PM Rate Topic: -----

#1 Dave W  Icon User is offline

  • D.I.C Head

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

Random Background Image

Posted 20 November 2008 - 04:21 AM

Hi All,

Hopefully you may be able to help me, I have got a random image reading from a file and it sends it to the screen fine, but is there a way to do it through CSS?

Here is the code I have so far.

Imports System.IO

Partial Class ImagesFromDirectory
	Inherits System.Web.UI.MasterPage

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Not Page.IsPostBack Then
			'Programmatically pick a random image from the ~/Images directory
			HeaderImage.ImageUrl = PickImageFromDirectory("~/Images/Image")
		End If
	End Sub

	'Returns the virtual path to a randomly-selected image in the specified directory
	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
End Class

In file to display to page
<asp:Image ID="HeaderImage" alt="Main Page Image" title="Main Page Image" runat="server" />

all help would be greatful.


Is This A Good Question/Topic? 0
  • +

Replies To: Random Background Image

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Random Background Image

Posted 23 November 2008 - 10:25 PM

You could always load a different CSS file that has the background image you're looking for
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1