/// <summary> ///forms that can display GIF/// </summary> Public classWingif:form {PrivateImage _img =NULL; PublicImage Img {Get{return_img;} Set{_img =value;} } PrivateEventHandler Evthandler =NULL; Publicwingif (Image img) {//Initialize SettingsEvthandler =NewEventHandler (onimageanimate); This. IMG =img; This. Width = _img. width+ -; This. Height = _img. Height + -; //Start Animationbeginanimate (); } //the method that the delegate is associated with Private voidonimageanimate (Object sender, EventArgs e) {//This method only causes the current control to redraw, calling OnPaint () This. Invalidate (); } //Set start animation Public voidbeginanimate () {if(_img! =NULL) { //An event is triggered when an animated GIF is converted to a frame every time after a certain period .//This method invokes the method associated with the current delegate when the current image is transformed one frame at a time .imageanimator.animate (_img,evthandler); } } //turns off the display animation, which stops rendering the current GIF animation when the window is closed or when an event is triggered Public voidstopanimate () {_img=NULL; Imageanimator.stopanimate (_img,evthandler); } /// <summary> ///overloaded Current WinForm OnPaint method that displays a frame of a GIF when the interface is drawn/// </summary> protected Override voidOnPaint (PaintEventArgs e) {Base. OnPaint (e); if(_img! =NULL) { //gets the next frame to render for the current GIF animationUpdateImage (); //The frame you want to render for the current GIF animation is displayed somewhere in the interfaceE.graphics.drawimage (_img,NewRectangle (0,0, _img. Width,_img. Height)); } } //gets the next frame of the current GIF animation that needs to be rendered, and any next action on the current GIF animation is to manipulate the frame Private voidUpdateImage () {imageanimator.updateframes (_img); } }
Use and Results display:
Private void button1_click (object sender, EventArgs e) { string'\ \2.gif"; = System.Drawing.Image.FromFile (file); New wingif (IMG); Gif. Show (); }
Overriding the OnPaint event to redraw a form (display animated GIF) Instance 2