Countdown to ASP.NET 2.0 Unleashed

written by Ryan Olshan on Saturday, March 25 2006

I thought it would be cool to put together a countdown to the release of ASP.NET 2.0 Unleashed, by Stephen Walther, on June 9, 2006. So, I created the below dynamic image. Cool, huh?

Now here's the code to the above countdown image.

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Drawing" %>

<%@ Import Namespace="System.Drawing.Imaging" %>

<%@ Import Namespace="System.IO" %>

<%@ Import Namespace="System.Text" %>

 

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)

    {

        bool fontSizeParse;

        int fontSize;

        string fontSizeText = Request.QueryString["FontSize"];

 

        fontSizeParse = int.TryParse(fontSizeText, out fontSize);

        if (!fontSizeParse || fontSize <= 0)

        {

            fontSize = 14;

        }

 

        string countDownDate = Request.QueryString["Date"];

 

        if (!String.IsNullOrEmpty(countDownDate))

        {

            string countdownText = GetCountdownText(countDownDate);

            GenerateCountdownBitmap(fontSize, "Verdana", Color.Black, Color.White, countdownText);

        }

    }

 

    private void GenerateCountdownBitmap(int fontSize, string fontName, Color fontColor, Color backgroundColor, string text)

    {

        Response.Expires = 0;

 

        Bitmap bitmap = null;

        Graphics graphic = null;

 

        try

        {

            Font font = new Font(fontName, fontSize);

 

            bitmap = new Bitmap(1, 1, PixelFormat.Format32bppArgb);

 

            graphic = Graphics.FromImage(bitmap);

            SizeF size = graphic.MeasureString(text, font);

 

            int height = (int)size.Height;

            int width = (int)size.Width;

 

            graphic.Dispose();

            bitmap.Dispose();

 

            bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);

            graphic = Graphics.FromImage(bitmap);

 

            graphic.FillRectangle(new SolidBrush(backgroundColor), new Rectangle(0, 0, width, height));

 

            graphic.DrawString(text, font, new SolidBrush(fontColor), 0, 0);

 

            MemoryStream memoryStream = new MemoryStream();

            bitmap.Save(memoryStream, ImageFormat.Gif);

 

            Response.ClearContent();

            Response.ContentType = "image/gif";

            Response.BinaryWrite(memoryStream.ToArray());

            Response.End();

        }

        finally

        {

            if (graphic != null)

            {

                graphic.Dispose();

            }

 

            if (bitmap != null)

            {

                bitmap.Dispose();

            }

        }

    }

 

    private string GetCountdownText(string futureDate)

    {

        TimeSpan countdown = GetCountdownTicks(futureDate);

 

        StringBuilder countdownText = new StringBuilder();

 

        int countdownDays = countdown.Days;

        if (countdownDays == 0 || countdownDays > 1)

        {

            countdownText.AppendFormat("{0} Days ", countdownDays);

        }

        else

        {

            countdownText.AppendFormat("{0} Day ", countdownDays);

        }

 

        int countdownHours = countdown.Hours;

        if (countdownHours == 0 || countdownHours > 1)

        {

            countdownText.AppendFormat("{0} Hours ", countdownHours);

        }

        else

        {

            countdownText.AppendFormat("{0} Hour ", countdownHours);

        }

 

        int countdownMinutes = countdown.Minutes;

        if (countdownMinutes == 0 || countdownMinutes > 1)

        {

            countdownText.AppendFormat("{0} Minutes ", countdownMinutes);

        }

        else

        {

            countdownText.AppendFormat("{0} Minute ", countdownMinutes);

        }

 

        int countdownSeconds = countdown.Seconds;

        if (countdownSeconds == 0 || countdownSeconds > 1)

        {

            countdownText.AppendFormat("{0} Seconds ", countdownSeconds);

        }

        else

        {

            countdownText.AppendFormat("{0} Second ", countdownSeconds);

        }

 

        int countdownMilliseconds = countdown.Milliseconds;

        if (countdownMilliseconds < 1 || countdownMilliseconds > 1)

        {

            countdownText.AppendFormat("{0} Milliseconds", countdownMilliseconds);

        }

        else

        {

            countdownText.AppendFormat("{0} Millisecond", countdownMilliseconds);

        }

 

        return countdownText.ToString();

    }

 

    private TimeSpan GetCountdownTicks(string futureDate)

    {

        bool futureDateParse;

        DateTime futureDateParsed;

        TimeSpan? ticksDifference = null;

 

        futureDateParse = DateTime.TryParse(futureDate, out futureDateParsed);

 

        if (futureDateParse)

        {

            DateTime currentDate = DateTime.Now;

 

            if (currentDate < futureDateParsed)

            {

                long currentDateTicks = DateTime.Now.Ticks;

                long futureDateTicks = DateTime.Parse(futureDate).Ticks;

                long longDifference = futureDateTicks - currentDateTicks;

 

                ticksDifference = TimeSpan.FromTicks(longDifference);

            }

        }

 

        return ticksDifference ?? TimeSpan.Zero;

    }

</script>

Kick this post on .NET Kicks

Similar Posts

  1. C# GridView Sorting/Paging w/o a DataSourceControl DataSource
  2. VB.NET GridView Sorting/Paging w/o a DataSourceControl DataSource
  3. GridView without DataSourceControl DataSource

Post a comment