Brendan Tompkins

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Add my PDC Countdown Image to your Blog

Thinking more about this PDC contest thing, I realized that it be cool to have an image on my blog that would countdown the days till the PDC.  This way, if I win, I’ll know when to start really getting juiced!  And, I’ve got some extra time lately, since the CS 1.1 roll out here at CodeBetter is pretty much done. So, I threw together an ASHX http handler which can add this image to your blog:

It grabs the PDC logo from Microsoft, calculates the days until PDC, and renders a new image to the outgoing Response stream, as a JPEG image.  I've tossed this code up on CodeBetter, so if you want to add this image to your blog, you just need to add this HTML:

<a href="http://msdn.microsoft.com/events/pdc/"><img src="/pdccountdownimage.ashx"></a>

I also thought it was a neat way to demonstrate some cool things, like

using System;

using System.Drawing;

using System.Drawing.Imaging;

using System.Globalization;

using System.IO;

using System.Net;

using System.Web;

using System.Web.Caching;

 

public class PdcCountdownImage : IHttpHandler

{

  public bool IsReusable

  {

    get { return true; }

  }

 

  /// <summary>

  /// Processes the request.

  /// </summary>

  /// <param name="ctx">CTX.</param>

  public void ProcessRequest(HttpContext ctx)

  { 

 

    Byte[] imageBytes = null;

 

    // Check if the cache contains the image.

    Object cachedImageBytes = HttpRuntime.Cache.Get("PDCBYTES");

 

    // We have some cached bytes, use em!

    if (cachedImageBytes != null)

    {

      imageBytes = cachedImageBytes as byte [];

    }

    else

    {   

      // Gotta go get the PDC image, hope they don't change the location

      System.Net.WebClient webClient = new WebClient();

      byte [] pdcImageBytes = webClient.DownloadData("http://msdn.microsoft.com/events/pdc/images/home_pdc_masthead.gif");

      using(Image inputImage = Image.FromStream(new MemoryStream(pdcImageBytes)))

      using(Image outputImage = new Bitmap(inputImage))

      { 

        using(Graphics g = Graphics.FromImage(outputImage))

        {

 

          IFormatProvider culture = new CultureInfo("en-US", true);

          DateTime pdcStartDate =

            DateTime.Parse("9/13/2005",

            culture,

            DateTimeStyles.NoCurrentDateDefault);

 

          DateTime pdcEndDate =

            DateTime.Parse("9/16/2005",

            culture,

            DateTimeStyles.NoCurrentDateDefault);

 

 

          DateTime today = DateTime.Today;

 

          string message = String.Empty;

 

          if(today < pdcStartDate)

          {

            message = String.Format("It's in {0} days!", (pdcStartDate - today).Days);

          }

          else if(today >= pdcStartDate && today <= pdcEndDate)

          {

            message = " It's on baby!";         

          }

          else

          {

            message = "  It's over baby!";

          }

 

          using(Font font = new Font("Arial", 16))

          {

            g.DrawString(message, font, Brushes.Gray,1,1);

          }       

 

          using(MemoryStream stream = new MemoryStream())

          {

            outputImage.Save(stream, ImageFormat.Jpeg);

            imageBytes = stream.GetBuffer();

          }

 

          HttpRuntime.Cache.Add("PDCBYTES", imageBytes, null,

            today.AddDays(1), new TimeSpan(0, 0, 0),

            CacheItemPriority.Normal, null);

        }       

 

      }   

    }

 

    ctx.Response.Cache.SetExpires(DateTime.Today.AddDays(1));

    ctx.Response.Cache.SetCacheability(HttpCacheability.Public);

    ctx.Response.Cache.SetValidUntilExpires(true); 

    ctx.Response.ContentType = "image/jpg";

    ctx.Response.BufferOutput = false;

    ctx.Response.OutputStream.Write(imageBytes, 0, imageBytes.Length);

    ctx.Response.End(); 

  }

}

You can download the ASHX file here, if you want to tweak it, and host it yourself. 

Have fun!  Hope to see you at the PDC!

-Brendan 


Posted Fri, Jul 1 2005 10:59 AM by Brendan Tompkins

[Advertisement]

Comments

Darrell wrote re: Add my PDC Countdown Image to your Blog
on Fri, Jul 1 2005 10:52 AM
Great. I hope you win!
pvanooijen wrote re: Add my PDC Countdown Image to your Blog
on Fri, Jul 1 2005 12:32 PM
Beauty.
It's on my homepage. Thanks!
Brendan Tompkins wrote re: Add my PDC Countdown Image to your Blog
on Fri, Jul 1 2005 12:38 PM
Cool!
Angus Logan wrote Love the PDC Countdown image
on Sat, Jul 2 2005 12:55 AM
scellini's WebLog wrote As if I need to be reminded...
on Wed, Jul 6 2005 11:57 PM
Brendan Tompkins made my day - if anyone cares - when he offered up a PDC Countdown image on his blog...
scellini's WebLog wrote As if I need to be reminded...
on Wed, Jul 6 2005 11:58 PM
Brendan Tompkins made my day - if anyone cares - when he offered up a PDC Countdown image on his blog...
scellini's WebLog wrote As if I need to be reminded...
on Wed, Jul 6 2005 11:58 PM
Brendan Tompkins made my day - if anyone cares - when he offered up a PDC Countdown image on his blog...
Brendan Tompkins wrote PDC Countdown Image V2
on Thu, Jul 7 2005 7:23 AM
The other day, I created a PDC Countdown Image&amp;nbsp;that you can add to your blog.

It grabs the PDC...
Brendan Tompkins wrote PDC Countdown Image V2
on Thu, Jul 7 2005 7:24 AM
The other day, I created a PDC Countdown Image&amp;nbsp;that you can add to your blog.

It grabs the PDC...
Brendan Tompkins wrote PDC Countdown Image V2
on Fri, Jul 8 2005 6:43 AM
The other day, I created a PDC Countdown Image&amp;nbsp;that you can add to your blog.

It grabs the PDC...

Add a Comment

(required)  
(optional)
(required)  
Remember Me?