The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Add my PDC Countdown Image to your Blog

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Brendan Tompkins

Posts: 158
Nickname: brendant
Registered: Apr, 2005

Brendan Tompkins is .NET Developer and founder of CodeBetter.Com
Add my PDC Countdown Image to your Blog Posted: Jul 1, 2005 9:59 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Brendan Tompkins.
Original Post: Add my PDC Countdown Image to your Blog
Feed Title: Brendan Tompkins
Feed URL: /error.htm?aspxerrorpath=/blogs/brendan.tompkins/Rss.aspx
Feed Description: Blog First. Ask Questions Later.
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Brendan Tompkins
Latest Posts From Brendan Tompkins

Advertisement

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 

Read: Add my PDC Countdown Image to your Blog

Topic: Thursday Thoughts Previous Topic   Next Topic Topic: An Argument For C#

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use