Blog ( Of Ryan Olshan)

Recent Posts

News

About Ryan Olshan

LinkedIn Profile

Follow Ryan via twitter

Join StrongTypes - Free Generics Help By Email
Join StrongTypes

Community Credit Winner

View my MVP profile

The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

Subscribe to Rolog via My AOL
Subscribe to Rolog via Bloglines


Subscribe to Rolog via Feedburner

Furl Rolog




Subscribe to Rolog via Newsburst


Subscribe to Rolog via Pluck

Add Rolog to Technorati Favorites




Thycotic Secret Server

Receive Email Updates


.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • LearnAsp.com
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Charles Carroll
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Doug Nelson
  • Kevin Jones

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

Countdown to ASP.NET 2.0 Unleashed

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 >

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Mar 25 2006, 10:29 PM by Ryan Olshan | with no comments
Filed under:

Comments

No Comments