Display GIF Animation'================================================ =================================================' I. Principles: 'This is just a simple Gif image playback control' principle is actually very simple. Gif files are composed of three parts: '1. header file '2. Frame '3. End of file'. The first five letters of the header file are fixed. composed of Gif89, in this way, you can determine whether it is a Gif file. The header file, frame and frame are fixed by the Mark & H21 & HF9 connection, and the fourth byte starting from & H21 indicates the latency between frames. 'Thus, each frame, header file, and file end sign & H3B can be used to form a single-frame Gif file', which is displayed by a single frame of the program '======== =========================================== ======================================= Imports System. IO Imports System. drawingImports System. threadingImports System. componentModel <ToolboxBitmap ("E:/Gif_Project/gif. ico "), DefaultProperty (" GifImage "), DefaultEvent (" Stoped ")> _ Public Class GifAnimationInherits System. windows. forms. userControlConst GifBz1 As Byte = 33 'frame flag & H21Const GifBz2 As Byte = 100' frame flag & HF9Const GifEnd As Byte = 100' end flag & H3B # Region "code generated by Windows Form Designer" Public Sub new () myBase. new () 'This call is required by the Windows Forms designer. InitializeComponent () 'After InitializeComponent () is called, add any initialization End Sub 'usercontrol to override dispose to clear the component list. Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean) If disposing ThenIf Not (components Is Nothing) Thencomponents. dispose () End IfEnd IfMyBase. dispose (disposing) End Sub 'private components As System required by the Windows Form Designer. componentModel. IContainer 'note: The following process is required by the Windows Forms designer. 'You can use the Windows Forms designer to modify this process. 'Do not use the code editor to modify it. Private WithEvents PictureBox1 As System. windows. forms. pictureBoxFriend WithEvents Timer1 As System. windows. forms. timer <System. diagnostics. debuggerStepThrough ()> Private Sub InitializeComponent () Me. components = New System. componentModel. containerMe. pictureBox1 = New System. windows. forms. pictureBoxMe. timer1 = New System. windows. forms. timer (Me. components) Me. suspendLayout () ''picturebox1 ''Me. pictureBox1. BorderStyle = System. windows. forms. borderStyle. fixedSingleMe. pictureBox1.Location = New System. drawing. point (4, 1) Me. pictureBox1.Name = "PictureBox1" Me. pictureBox1.Size = New System. drawing. size (72, 70) Me. pictureBox1.SizeMode = System. windows. forms. pictureBoxSizeMode. centerImageMe. pictureBox1.TabIndex = 0Me. pictureBox1.TabStop = False '''timer1' ''' GifAnimation ''Me. controls. add (Me. pictureBox1) Me. name = "GifAnimation" Me. size = New System. drawing. size (77, 72) Me. resumeLayout (False) end Sub # End Region 'Collection is used to obtain information about each frame. Private m_col_Gif As Collection' stop flag Private pause nstop As Boolean = TruePrivate mintCurrentPosition As Integer 'current playback position Private mimgGif As ImagePrivate mth As ThreadPublic Event stoped (ByVal sender As Object) 'Stop the event Public WriteOnly Property GifFile () As StringSet (ByVal Value As String) If Value I S Nothing Then Exit PropertymimgGif = Image. fromFile (Value) m_col_Gif = FormatGIF (Value) If m_col_Gif Is Nothing OrElse m_col_Gif.Count = 0 ThenMe. pictureBox1.Image = NothingmimgGif = NothingExit PropertyEnd IfSetPicToPicturbox (CType (m_col_Gif.Item (1), GifFrame ). frame) End SetEnd PropertyPublic Property SizeModel () As PictureBoxSizeModeGetReturn Me. pictureBox1.SizeModeEnd GetSet (ByVal Value As Pictur EBoxSizeMode) Me. pictureBox1.SizeMode = ValueEnd SetEnd PropertyPublic Property BorderStyle () As BorderStyleGetReturn Me. pictureBox1.BorderStyleEnd GetSet (ByVal Value As BorderStyle) Me. pictureBox1.BorderStyle = ValueEnd SetEnd PropertyPublic Property GifImage () As ImageGetReturn mimgGifEnd GetSet (ByVal Value As Image) If Value Is Nothing Then Exit PropertymimgGif = Valuem_col_Gif = FormatGIF (Value) I F m_col_Gif Is Nothing OrElse m_col_Gif.Count = 0 ThenMe. pictureBox1.Image = NothingExit PropertyEnd IfSetPicToPicturbox (CType (m_col_Gif.Item (1), GifFrame ). frame) End SetEnd PropertyPublic Sub StopView () returns nstop = TrueEnd SubPrivate Sub Start () dim I As IntegerDim gif As GifFrameDo Until quit nstop = Truei + = 1If I> m_col_Gif.Count Theni = 1End Ifgif = m_col_Gif.Item (I) Thread. sleep (gif. invert * 10 )' Latency between frames 'display image SetPicToPicturbox (gif. frame) LoopSetPicToPicturbox (CType (m_col_Gif.Item (1), GifFrame ). frame) RaiseEvent Stoped (Me) End SubPublic Sub StartView (Optional ByVal useTimer As Boolean = True) if m_col_Gif Is Nothing Then Exit SubIf Then = 0 Then Exit subpartition nstop = FalseIf useTimer ThenIf m_col_Gif Is Nothing Then Exit SubIf Then = 0 Then Exit subpartition nstop = FalseTimer1.Enab Led = TrueElsemth = New Thread (AddressOf Start) mth. priority = ThreadPriority. normalmth. start () End IfEnd Sub 'Collect get frame information Private Function FormatGIF (ByVal GifFile As String) As Collection 'Open the image file Dim fs As New FileStream (GifFile, FileMode. open, FileAccess. read) 'file length Dim fileLen As Long = fs. lengthDim br As New BinaryReader (fs) Dim buff As Byte () 'reads all the image information into a Byte array buff = br. readBytes (fileLen) Return FormatGIF (buf F) End Function 'get the frame information from the Image object. Private Function FormatGIF (ByVal GifImage As Image) as CollectionDim col As New Collection create a memory stream Dim sr As New MemoryStream to write the image to the stream GIF image. save (sr, GifImage. rawFormat. gif) Dim buff As Byte () 'converts the image into a Byte array buff = sr. toArrayReturn FormatGIF (buff) End Function 'obtains frame information from a Byte array. Private Function FormatGIF (ByVal buff () As Byte) As CollectionDim col As New CollectionDim Index1 As Long Dim Index2 As LongDim IndexTmp As LongDim intTime As IntegerDim buffHead () As ByteDim buffBody () As ByteDim img As ImageIf buff. length <3 Then Return col 'converts the first three bytes to the String Dim tmp As String = System. text. encoding. default. getString (buff, 0, 3 ). whether ToLower is a Gif file If tmp <> "gif" ThenThrow New Exception ("incorrect image format! It must be a Gif file ") End If Do 'to find the first flag & H21Index1 = buff. indexOf (buff, GifBz1, Index1 + 1) If Index1 <0 Or Index1> = buff. length-1 Then Return col 'find the second sign & H21Index2 = buff. indexOf (buff, GifBz2, Index1) 'two flags are consecutive If Index2-Index1 = 1 Then Exit DoLoopIndexTmp = Index1' to create a buffer buffHead = buffHead. createInstance (GetType (System. byte), Index1) 'obtains the header information buff. copy (buff, buffHead, Index1) 'read gifheadDoDoIndex1 = buff. indexOf (buff, GifBz1, Index1 + 1) If Index1 <0 Or Index1> = buff. length-1 Then Exit DoIndex2 = buff. indexOf (buff, GifBz2, Index1) If Index2-Index1 = 1 Then Exit DoLoop 'is the last frame If Index1 <0 Or Index1> = buff. length-1 Then Exit Do 'create buffer buffBody = buffBody. createInstance (GetType (Byte), Index1-IndexTmp) 'gets the frame information buffBody. copy (buff, IndexTmp, buffBody, 0, Index1-IndexTmp) 'gets the interval of each frame intTime = Val (buff (IndexTmp + 4 )) 'recreate each frame image img = CreateImage (buffHead, buffBody) 'create a frame object Dim gif As New GifFrame (img, intTime)' and add it to the set col. add (gif) IndexTmp = Index1Loop 'last frame buffBody = buffBody. createInstance (GetType (Byte), buff. length-IndexTmp) buffBody. copy (buff, IndexTmp, buffBody, 0, buff. length-IndexTmp) 'Get the interval of each frame intTime = Val (buff (IndexTmp + 4) img = CreateImage (buffHead, buffBody, False) Dim gifs As New GifFrame (img, intTime) col. add (gifs) Return colEnd Function 'creates a frame image Private Function CreateImage (ByVal gifHead () As Byte, ByVal gifBody () As Byte, Optional ByVal AddEnd As Boolean = True) as image' create a memory stream Dim sm As New MemoryStreamDim img As image' write header information sm. write (gifHead, 0, gifHead. length) 'write frame information sm. write (gifBody, 0, gifBody. length) 'If it is not the last frame, the write end flag If AddEnd Then sm. writeByte (Me. gifEnd) 'create Image img = Image. fromStream (sm) 'closes the stream sm. close () Return imgEnd Function 'shows a frame Image Private Sub SetPicToPicturbox (ByVal img As Image) Me. pictureBox1.Image = imgPictureBox1.Top = 0PictureBox1. left = 0If PictureBox1.SizeMode <> PictureBoxSizeMode. autoSize Then Exit SubMe. width = Me. pictureBox1.WidthMe. height = Me. pictureBox1.HeightEnd SubPrivate Sub timerspontick (ByVal sender As Object, ByVal e As System. eventArgs) Handles Timer1.TickIf mintCurrentPosition <symbol ThenmintCurrentPosition = mintCurrentPosition + 1 ElsemintCurrentPosition = 1End If 'Get the current frame Dim gif As GifFrame = second (mintCurrentPosition) SetPicToPicturbox. frame) 'display Frame image Timer1.Interval = gif. invert * 10' Frame delay If required nstop = True ThenTimer1.Enabled = FalseSetPicToPicturbox (CType (m_col_Gif.Item (1), GifFrame ). frame) RaiseEvent Stoped (Me) 'triggers the event End IfEnd SubPrivate Sub GifAnimation_BackColorChanged (ByVal sender As Object, ByVal e As System. eventArgs) Handles MyBase. backColorChangedPictureBox1.BackColor = Me. backColorEnd SubPrivate Sub GifAnimation_Resize (ByVal sender As Object, ByVal e As System. eventArgs) Handles MyBase. resizePictureBox1.Top = 0PictureBox1. left = 0If PictureBox1.SizeMode <> PictureBoxSizeMode. autoSize ThenPictureBox1.Width = Me. widthPictureBox1.Height = Me. heightElseMe. width = PictureBox1.WidthMe. height = PictureBox1.HeightEnd IfEnd SubPrivate Sub GifAnimation_Disposed (ByVal sender As Object, ByVal e As System. eventArgs) Handles MyBase. disposedTryMe. required nstop = TrueIf Not mth Is Nothing Then 'Stop the thread mth. abort () End IfCatch ex As ExceptionEnd TryEnd Sub 'frame object' is used to store the information of each frame: frame delay, Image Private Class GifFramePublic Frame As Image 'frame Image Public Invert As Integer 'Frame delay Public Sub New (ByVal img As Image, ByVal time As Integer) frame = imgInvert = timeEnd SubEnd ClassEnd Class