Portraitpress image question

Topics: ASP.NET 2.0, Themes
Aug 5, 2010 at 7:32 PM
Edited Aug 5, 2010 at 7:32 PM
I am using the theme PortraitPress which displays an image file called portrait.jpg, I frequently replace this image with others that I rotate. My question is this, is it possible to have code do this automatically, say if I had 6 different images that were names portrait1.jpg, portrait2.jpg etc., maybe switching in sequence every x amount of hours. See website here: http://sahcinfo.org Thank you for a great project and all the help that is available here to a novice like myself. Jerry Dean
Coordinator
Aug 6, 2010 at 12:28 AM

Here's one approach.  In the site.master file is this:

<div id="portrait"></div>

Replace that with:

<asp:Literal ID="litPortrait" runat="server"></asp:Literal>

Then in the site.master.cs file, in the Page_Load handler, you can output the same DIV tag with a background image -- based on conditional logic.  Here's code I put into the existing Page_Load handler -- it looks at the time of the day to determine which BG image to show.

string bgImage;

if
(
  DateTime.Now.Hour == 0 ||
  DateTime.Now.Hour == 12 ||
  DateTime.Now.Hour == 15
)
  bgImage = "portrait-1.jpg";
else if
(
  DateTime.Now.Hour == 1 ||
  DateTime.Now.Hour == 6 ||
  DateTime.Now.Hour == 18
)
  bgImage = "portrait-2.jpg";
else
  bgImage = "portrait.jpg";

litPortrait.Text = string.Format(
  "<div id=\"portrait\" " +
  "style=\"background-image:" +
  "url({0}themes/portraitpress/images/{1});\"></div>",
  BlogEngine.Core.Utils.RelativeWebRoot, bgImage);

Aug 6, 2010 at 12:40 AM
Thank you once again Ben, I will give it a try.
Coordinator
Aug 6, 2010 at 9:40 AM

Here's another version that changes even more often.  This one picks the BG image based on the Seconds.  If the seconds are between 0 and 9 (for the current minute), it'll show one image, if between 10 and 19, it'll show another image, and so on.  With this, you would get up to 6 different images a minute.

string bgImage;

if (DateTime.Now.Second < 10)
  bgImage = "portrait-1.jpg";
else if (DateTime.Now.Second < 20)
  bgImage = "portrait-2.jpg";
else if (DateTime.Now.Second < 30)
  bgImage = "portrait-3.jpg";
else if (DateTime.Now.Second < 40)
  bgImage = "portrait-4.jpg";
else if (DateTime.Now.Second < 50)
  bgImage = "portrait-5.jpg";
else
  bgImage = "portrait.jpg";

litPortrait.Text = string.Format(
  "<div id=\"portrait\" " +
  "style=\"background-image:" +
  "url({0}themes/portraitpress/images/{1});\"></div>",
  BlogEngine.Core.Utils.RelativeWebRoot, bgImage);

Aug 6, 2010 at 4:03 PM
Thank you Ben, I implemented the second version and it is working great, a great addition and improvement to our hiking club website. Thank you for taking your time to do this for me, I greatly appreciate it.