Random Image Control for BlogEngine.Net

February 7, 2008 00:46 by ptumelty

I've recently started using the BrightSide theme which was converted to BlogEngine.Net by Henrik Stenbæk (link below)

I really like the header photo on the homepage but though it was a shame that its always the same image. It would be great to show one at random, from a selection of appropriate images.

This got me thinking a little. Maybe I could put together a simple control which would enable the user to specify a directory of appropriate images and then write out the appropriate html to show a random image from that directory.

Well thats exactly what I did, and here it is to download.

RandomImage1.0.zip (1.35 kb)

To install, simply drop into the app_code/controls directory.

Here is an example of how I'm using it with the BrightSide theme. Click on the home page and refresh a few times to see the result.

First of all, you need to edit the css file to remove the background-image reference in #headerphoto. This is because we will be using our own random image, leave the height, width and border settings as they are.

Next, you need to edit site.master. Look for a div with the ID="headerphoto" and replace with this:

<blog:RandomImage ID="headerphoto" ImageUrl="/blog/themes/BrightSide/headerphoto/" runat="server" />

The css for the height, width and border will be picked up from the css file. When the control renders, it will render as in image tag with the ID of the server control, and the src as a random image from that directory.

You can optionally pass in CssClsas="CssClassName" if you would rather define your Css that way.

I've slightly enlarged the header image in the theme so I can get more on, but the concept is still the same.  Obviously all of the images need to be the correct size for this to work in this context.

I originally wrote this as a User Control which would have given flexibility to include this in a post or page, but it felt like this was more of a template (site.master) control rather than something to embed in a post/page.

You could use this for example, to do a random image on your sidebar.

Hope someone finds this useful, I'd love to hear any feedback. 

Download the Theme: http://www.onesoft.dk/post/2008/01/New-BlogEngine-Theme---Bright-Side-of-Life.aspx 



Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

February 19. 2008 07:01

Henriik Stenbæk

Cool - I have started to cut up all my holiday photos in 820 x 179…. looking forward to add them to my blog Wink

Henriik Stenbæk

February 20. 2008 11:21

ptumelty

Hi Henriik, you might find that the height needs to be a little shorter on your site.

I made some edit's to your original theme to make the header image a little taller so I could get more image on there. The initial dimensions were 820x120 and I upped this to 820x179.

ptumelty

February 25. 2008 00:02

Henrik Stenbæk

Hi ptumelty

I know that Wink But I decided to go with your format (820x179) since the 120 height didn’t make any usable photos.

I ended up changing somewhere around 70% of your implementation: first I added caching, and then I changed so that the control didn’t run if it wasn’t visible, afterwards I added a description file for each photo and an auto hiding <div> for the description… go take a look. It’s pretty cool….

Henrik

Henrik Stenbæk

April 25. 2008 21:19

Tiffany

Hi Paul,

This is a great feature and I've already implemented it on my site. I am also using the BrightSide theme.

I noticed that you have a cool fade in caption when you mouse over your random header image on your home page. I would like the same feature on my site. Will you be releasing the code for this as well?

Thanks,
T

Tiffany

May 1. 2008 13:45

ptumelty

Hi, I actually borrowed this code from somebody else Smile I'll look at packaging it up as soon as I get chance. Will send you an email as soon as I have it on the site.

ptumelty

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

May 12. 2008 01:17