ASP. net gdi + generates dynamic GIF animation.

Source: Internet
Author: User
ArticleDirectory
    • Draw frames and pack into an animation
    • Load and modify or extract frames from existing GIF Animation

Update: deom download from here

I want to use GDI + to generate an animated GIF image, and Google I found an article on codeproject. Create GIF animation on fly The author uses a component. Unfortunately, the component Source code Not released, Download the demo here , Example Code : 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: \ vmlinux \ gifimage.gif " , Filemode. Create );
// (Write animation to gifimage.gif)
GIF. Save (FS );
FS. close;

There is also an article about the GIF file structure:
Http://www.hf110.com/Article/jswz/bcjs/ozh/200504/889.html

Reference Original:
Introduction

. Net Framework does not provide a way to create GIF animations. finally we have to do it ourselves. this is my solution, I write it down and share my component with everyone who loves codeproject. source code is currently not shared.

Because of server error, demo and picture upload is failed. Here is the another download address http://www.newsmth.net/bbscon.php? FTYPE = 0 & bid = 288 & id = 50708 & AP = 2454
Sorry for inconvenience, here is an updated demo package with test images http://www.newsmth.net/bbscon.php? FTYPE = 0 & bid = 288 & id = 50723 & AP = 495
It's pain to upload files onto codeproject Server

Use this componentdraw frames and pack into an animation

The following code piece will create gifimage.gif

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: \ vmlinux \ gifimage.gif" , Filemode. Create ); // (Write animation to gifimage.gif) GIF. Save (FS); FS. close;
Load and modify or extract frames from existing GIF Animation
Fileinfo Fi = New Fileinfo (@ "E: \ vmlinux \ x.gif" ); // (Load animation from file) Gifimage. gifanimation GIF = New Gifimage. gifanimation (Fi. openread ()); // (Set global animation control) GIF. control = New Gifimage. graphiccontrolext ( 1  /* (Disposal method )*/ , False  /* (Wait for user input and then continue ?) */ , False  /* (Set this option if transparent color is used )*/ , 36 /* (Time to wait before the next frame is displayed )*/ , 0  /* (Index of transparent color in color table )*/ ); // (Add a new frame to the end) GIF. addframe ( New Bitmap (@ "E: \ vmlinux \ 2.bmp" )); // (Insert a new frame at the specified position) GIF. insertframe ( New Bitmap (@ "E: \ vmlinux \ 1.bmp" ), 2 ); Fileinfo FRM = New Fileinfo (@ "E: \ vmlinux \ frm.gif" ); // (Save the frame at the specified position) GIF. saveframe (FRM. openwrite (), 4 ); Filestream FS = New Filestream (@ "E: \ vmlinux \ out.gif" , Filemode. Create ); // (Write the modified animation to file) GIF. Save (FS); FS. Close ();

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.