Use GDI + to generate the animated GIF image sample code

Source: Internet
Author: User

Bitmap bmp = new Bitmap (100,100 );
Graphics g = Graphics. FromImage (bmp );
Font f = new Font ("arial", 11f );
Brush B = Brushes. Blue;

String txt = "Rotate text animation! ";
SizeF sz = g. MeasureString (txt, f );
G. Clear (Color. WhiteSmoke );
G. DrawString (txt, f, B, 50-sz.Width/2, 50-sz.Height/2 );
G. Flush ();
// (The following code create a starting frame from bmp)
GifImage. GifAnimation gif = new GifImage. GifAnimation (bmp,
GifImage. GraphicControlExt. Default );
// (Set this property otherwise the animation will not play circularly)
Gif. Application = GifImage. ApplicationExt. Default;
// (Use global color table only, set this option will greatly decrease the size of output file)
Gif. UseGlobalColorTableOnly = true;

For (int I = 1; I <36; ++ I)
{
G. Clear (Color. WhiteSmoke );
G. TranslateTransform (50, 50 );
G. RotateTransform (10f * I );
G. DrawString (txt, f, B, sz. Width/-2, sz. Height/-2 );
G. ResetTransform ();
G. DrawString ("Hello", f, Brushes. Red,-50 + I * 4, 20 );
G. DrawString ("Yeah", f, Brushes. Orange, 60,-20 + I * 4 );

G. Flush ();
// (Create a frame from bitmap)
Gif. AddFrame (bmp );
}

F. Dispose ();
G. Dispose ();
Bmp. Dispose ();

FileStream fs = new FileStream (@ "E: vmlinuxGifImage.gif", FileMode. Create );
// (Write animation to GifImage.gif)
Gif. Save (fs );
Fs. Close;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.