Random Header Image

As mentioned in my previous post I quite liked my old skins as I incorporated random header images. This was done by a simple aspx file called RandonHeader.apsx funnily enough!

What this file does is it looks in a folder that I specify for jpg images and will set one of those images as your header image on a page load.

The script is shown below. I originally had it running as a php page (if you want a copy of that please let me know.

The only real issue with this is that it needs to be a physical directory that the images are called up from. I tried to use http://www.kemponline.co.uk/images/headers but it did not work. I am guessing if you enable directory browsing to the specified folder it might work, but I only have the option to enable directory browsing at the top level.

<%@ Page Language="C#"%>
<script runat="server">
   
    protected void Page_Load(object sender, EventArgs e)
    {
        System.IO.DirectoryInfo di =
new System.IO.DirectoryInfo(@"D:\wwwroot\images\headers\");
       
     
        System.IO.FileInfo[] rgFiles = di.GetFiles("
*.jpg");     

       int iCount = rgFiles.Length;

        Random random = new Random();
        int iRan = random.Next(0, iCount);
        //Response.Write(di.GetFiles(iRan).ToString());
        Response.ContentType = "
image/jpeg";
        Response.WriteFile(@"
D:\wwwroot\images\headers\" +
rgFiles.GetValue(iRan).ToString());
                
    } 
</script>

So any way if you copy and paste this into an aspx file and then change the path to that of your images path on your hosting you should have an up and running random header image on page reloads/refresh.


Comments

Gravatar # re: Random Header Image
Posted by Wayne Hartman on 5/1/2008 4:21 PM
You'd get better performance out of your script if you didn't embed the script into the page and compiled it into a separate class. Then, you could add a configuration section in the web.config so that you could parameterize some of the values that you have in there as literals. In addition, you can replace the literals of the server path with Server.MapPath("~/"+ConfigurationManager.AppSettings["HeaderDirectory"]);.

Post Comment

Title *
Name *
Email
Url
Comment *  


Please add 5 and 5 and type the answer here: