A recent picture viewer, which uses a generic pcturebox, does not meet the expected requirements in performance and scaling control, so all component rendering is drawn by overriding the control's OnPaint event. When viewing GIF images, Graphics.DrawImage found only the first frame, unable to meet the expected requirements, so after groping to find a way to solve the self-painted GIF better.
This article describes a. NET-carrying class Imageanimator, which is similar to controlling the timeline of animations, using Imageanimator.cananimate to determine whether a picture is animated, and calling Imageanimator.animate can begin to play the animation, that is, every A frame of time triggers a onframechanged delegate, we can animate the animation by selecting the active frame of the image to the next frame in the delegate and then forcing the interface to redraw.
To facilitate later use, I have incorporated these codes together to form a animateimage class that provides attributes such as Cananimate, Framecount, Currentframe, and Play (), Stop (), Reset () such as the usual method of animation, the code is as follows
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Drawing;
5 using System.Drawing.Imaging;
6
7 namespace Giftest
8 {
9/**////<summary>
10///represents a class of images that drive the drawing function.
One///</summary>
public class Animateimage
13 {
Image Image;
Framedimension framedimension;
/**////<summary>
17///The animation is triggered when the current frame changes.
///</summary>
public event eventhandler<eventargs> Onframechanged;
20
/**////<summary>
22///instantiation of a animateimage.
///</summary>
///<param name= "img" > Animated pictures. </param>
Animateimage Public (Image img)
26 {
Image = img;
28
Lock (image)
30 {
Mcananimate = imageanimator.cananimate (image);
if (mcananimate)
33 {
guid[] Guid = image. Framedimensionslist;
Framedimension = new Framedimension (guid[0]);
Mframecount = image. Getframecount (framedimension);
37}
38}
39}
40
mcananimate bool;
KM INT mframecount = 1, mcurrentframe = 0;
43
/**////<summary>
45///Pictures.
///</summary>
The Public image Image
48 {
"Get" {return image;}
50}
51
/**////<summary>
53///Whether or not to animate.
///</summary>
public bool Cananimate
56 {
mcananimate {return;}
58}
59
/**////<summary>
61///Total Frame number.
///</summary>
Framecount public int
64 {
Mframecount {return}
66}
67
/**////<summary>
69///The current frame to play.
///</summary>
Currentframe public int
72 {
"Get" {return mcurrentframe;}
74}
75
/**////<summary>
77///Play this animation.
///</summary>
+ public void Play ()
80 {
Bayi if (mcananimate)
82 {
Lock (image)
84 {
Imageanimator.animate (Image, New EventHandler (framechanged));
86}
87}
88}
89
/**////<summary>
91///stop playing.
///</summary>
The public void Stop ()
94 {
if (mcananimate)
96 {
The Lock (image)
98 {
Imageanimator.stopanimate (Image, New EventHandler (framechanged));
100}
101}
102}
103
/**////<summary>
105///Reset the animation so that it stops at frame No. 0 position.
///</summary>
The public void Reset ()
108 {
109 if (mcananimate)
110 {
Imageanimator.stopanimate (Image, New EventHandler (framechanged));
112 Lock (image)
113 {
114 image. Selectactiveframe (framedimension, 0);
Mcurrentframe = 0;
116}
117}
118}
119
The private void Framechanged (object sender, EventArgs e)
121 {
122 Mcurrentframe = mcurrentframe + 1 >= mframecount? 0:mcurrentframe + 1;
123 Lock (image)
124 {
Image. Selectactiveframe (Framedimension, mcurrentframe);
126}
127 if (onframechanged!= null)
128 {
129 onframechanged (image, E);
130}
131}
132}
133}
134