Using the AnimatedGif Class

Using the AnimatedGif Class

The AnimatedGif class is easy to use. All you need to do is instantiate the class, add frames, create the animation, and then use the file name in whatever way is appropriate. I've created a simple demo program that illustrates this class's use. Users simply type in a text string, and a series offrames are created with the text string at various positions in the GIF image. In Figure 11.2, you can see the demo program as it appears when it first starts.

Figure 11.2. The Demo Program Has a Simple User Interface.

graphics/11fig02.jpg

Once the user clicks on the Render Banner button, the animation is created, and the animated image appears on the screen, as shown in Figure 11.3.

Figure 11.3. The Animated Banner Is Simple, but This Demo Program Gives You the Idea of How to Use the AnimatedGif Class.

graphics/11fig03.jpg

Listing 11.10 shows the <IMG> tag being output to the HTML stream. The file name is held in a session variable that won't exist until the animation has been created.

Listing 11.10 Outputing the Banner Image Tag into the HTML Stream
 public void OutputBannerImage() {     if( Session["Banner"] != null )     {         Response.Write( "<IMG SRC=\"BannerImages/" +             Convert.ToString( Session["Banner"] ) + "\">" );     } } 

Listing 11.11 shows the process for creating the animation. First, an AnimatedGif object is created. Next, a series of images is created and added to the AnimatedGif class. Last, the animation is created with the CreateAnimation() method.

Listing 11.11 Rendering the Banner
 private void RenderBanner_Click(object sender, System.EventArgs e) {     AnimatedGif anim = new AnimatedGif();     for( int i=0; i<10; i++ )     {         Bitmap objBitmap = new Bitmap( 668, 53,             PixelFormat.Format24bppRgb );         Graphics g = Graphics.FromImage( objBitmap );         g.FillRectangle( new SolidBrush( Color.Red ), 0, 0, 668, 53 );             g.DrawString( BannerText.Text, new Font( "Times New Roman",                 15 ),             new SolidBrush( Color.White ), i * 10, 10 );         anim.AddFrame( objBitmap, Request.MapPath( "BannerImages" ) );     }     Session["Banner"] =        anim.CreateAnimation( Request.MapPath( "BannerImages" ) ); } 


ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ISBN: 321159659
EAN: N/A
Year: 2003
Pages: 175

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net