.NET displayplay GIF animation

zhaozj2021-02-16  61

'================================= Description ======================================================================================================================================================================= ========================1, the principle Description: 'This is just a simple GIF picture play control' principle actually a simple GIF file is from three parts Composition '1, header file' 2, frame '3, file end flag' header first five letters fixed by GIF89, thereby determining whether it is a GIF file 'header file, and fixed by the flag & h21 & h21 & The HF9 is connected, 'The fourth byte starting from & h21 represents the latency between the frames. 'The result of each frame, header file, and file end flag & H3B to constitute a single frame GIF file' by the program frame frame to display '================================================================================================================================================= ============== / Description ====================================================================================================================================================== ======== Imports System.IO Imports System.Drawing Imports System.Threading Imports System.ComponentModel _ Public Class GifAnimation Inherits System.Windows.Forms.UserControl Const GifBz1 As Byte = 33 'frame flag & H21 Const GifBz2 As Byte = 249' frame flag & HF9 Const GifEnd As Byte = 179 'ending flag & H3B #Region" windows window The code generated by the body designer "Public Sub New () MyBase.new () 'This call is required for the Windows Form Designer. InitializationComponent () adds any initialization End Sub 'UserControl to override the component list after the initializeComponent () call.

Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose () End If End If MyBase.Dispose (disposing) End Sub 'Windows Form Designer Private components required As system.componentmodel.icontainer 'Note: The following procedure is required to use the Windows Form Designer to modify this process using the Windows Form Designer. 'Don't modify it using the code editor.

Private WithEvents PictureBox1 As System.Windows.Forms.PictureBox Friend WithEvents Timer1 As System.Windows.Forms.Timer Private Sub InitializeComponent () Me.components = New System.ComponentModel.Container Me.PictureBox1 = new System.Windows.Forms.PictureBox Me.Timer1 = new System.Windows.Forms.Timer (Me.components) Me.SuspendLayout () '' PictureBox1 'Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle Me .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.CenterImage Me.PictureBox1.TabIndex = 0 Me.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 'set to get each frame information private m_col_gif as collection' Stop Sign Private M blnStop As Boolean = True Private mintCurrentPosition As Integer 'current playback position Private mimgGif As Image Private mth As Thread Public Event Stoped (ByVal sender As Object)' stop event Public WriteOnly Property GifFile () As String Set (ByVal Value As String) If Value IS nothing kilif = image.fromfile (value) m_col_gif = formatgif (value) if m_col_gif is nothing oralse m_col_gif.count = 0 Then me.PictureBox1.image = nothing mimggif=

Nothing Exit Property End If SetPicToPicturbox (CType (m_col_Gif.Item (1), GifFrame) .Frame) End Set End Property Public Property SizeModel () As PictureBoxSizeMode Get Return Me.PictureBox1.SizeMode End Get Set (ByVal Value As PictureBoxSizeMode) Me. PictureBox1.SizeMode = Value End Set End Property Public Property BorderStyle () As BorderStyle Get Return Me.PictureBox1.BorderStyle End Get Set (ByVal Value As BorderStyle) Me.PictureBox1.BorderStyle = Value End Set End Property Public Property GifImage () As Image Get Return mimgGif End Get Set (ByVal Value As Image) If Value Is Nothing Then Exit Property mimgGif = Value m_col_Gif = FormatGIF (Value) If m_col_Gif Is Nothing OrElse m_col_Gif.Count = 0 Then Me.PictureBox1.Image = Nothing Exit Property End If SetPicToPicturbox (CType (m_col_Gif.Item (1), GifFrame) .Frame) End Set End Property Public Sub StopView () mblnStop = True End Sub Private Sub Start () Dim i As Integer Dim gif As GifFrame Do Until mblnStop = True i = 1 IF I> M_COL_GIF.COUNT THEN I = 1 End If GIF = M_COL_GIF.Item (i) Thread.Sleep Delayed Delay between Frames and Frame 'Display Image SetPictopicturBox (Gif.Frame) Loop SetPictopicturbox (CType (m_col_Gif.Item (1), GifFrame) .Frame) RaiseEvent stoped (Me) End Sub Public Sub StartView (Optional ByVal useTimer As Boolean = True) If m_col_Gif Is Nothing Then Exit Sub If m_col_Gif.Count = 0 Then Exit Sub mblnStop = false If useTimer Then If m_col_Gif Is Nothing Then Exit Sub If m_col_Gif.Count = 0 Then Exit Sub mblnStop = false Timer1.Enabled = True Else mth = New Thread (AddressOf Start) mth.Priority = ThreadPriority.Normal mth.Start () End IF end sub '

Working from text GUNCTION FORMATGIF (BYVAL GIFFILE AS STRING) AS Collection 'Opening Image File DIM FS AS New FileStream (Giffile, FileMode.open, FileAccess.Read)' File Length Dim Filelen As Long = Fs.LENGTH DIM BR AS New BinaryReader () DIM BUFF AS BYTE () 'Reads all image information into one byte array buff = br.readBytes (Filelen) Return Formatgif (BUFF) End Function' Gets Frame Information Private Function Formatgif (ByVal Gifimage As Collection DIM COL AS New Collection 'Creating a memory DIM SR AS New MemoryStream' writes the image to the stream gifimage.save (Sr, gifimage.rawformat.gif) DIM BUFF AS BYTE () 'Transforms the image into byte array buff = sr.ToArray Return FormatGIF (buff) End Function 'frame information obtained Private Function FormatGIF (ByVal buff () As byte) from a byte array As Collection Dim col As New Collection Dim Index1 As Long Dim Index2 As Long DIM INDEXTMP AS Long Dim Intenchage As INTEGER DIM Buffhead () AS BYTE DIM BUFFBODY () AS BYTE DIM BUFFBODY () AS BYTE DIM IMG As Image IF Buff.Length <3 Ten Return Col 'Transplates the header three bytes into string DIM TMP AS STRING = SYSTEM. Text.Encoding.default.getstring (buff, 0, 3) .tolower 'is gif text IF TMP <> "GIF" THROW New Exception ("Image Format Error! Must be GIF file ") End if do 'Find the first flag & H21 index1 = buff.indexof (buff, gifbz1, index1 1) if Index1 <0 or index1> = buff.length - 1 Ten Return Col' Find the second flag & H21 Index2 = buff.IndexOf (buff, GifBz2, Index1) 'flag is two consecutive if Index2 - Index1 = 1 Then Exit Do Loop IndexTmp = Index1' to create a buffer buffHead = buffHead.CreateInstance (GetType (System.Byte) , Index1) 'get header information buff.copy (buff, buffhead, index1)' read gifhead do do index1 = buff.indexof (buff, gifbz1, index1 1) if index1 <0 or index1> =

Buff.Length - 1 Then Exit do index2 = buff.indexof (buff, gifbz2, index1) if index2 - index1 = 1 THEN EXIT do loop 'is the last frame if index1 <0 or index1> = buff.length - 1 Then Exit Do 'to create a buffer buffBody = buffBody.CreateInstance (GetType (Byte), Index1 - IndexTmp)' acquires frame information buffBody.Copy (buff, IndexTmp, buffBody, 0, Index1 - IndexTmp) 'acquisition time interval of each frame intTime = Val (buff (INDEXTMP 4)) 'Rebuilding each frame image img = createImage (buffhead, buffbody)' Creating a frame object Dim Gif as new gifframe (IMG, INTTIME) 'Add to Collection Col.Add (GIF) Indextmp = Index1 Loop 'Finally a buffBody = buffBody.CreateInstance (GetType (Byte), buff.Length - IndexTmp) buffBody.Copy (buff, IndexTmp, buffBody, 0, buff.Length - IndexTmp)' acquired per frame interval intTime = Val ( buff (IndexTmp 4)) img = CreateImage (buffHead, buffBody, False) Dim gifs As New GifFrame (img, intTime) col.Add (gifs) Return col End Function 'to create 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 current DIM SM AS N EW MemoryStream DIM IMG AS Image 'Write Head Information Sm.Write (Gifhead, 0, Gifhead.Length)' Write Frame Information Sm.Write (Gifbody, 0, Gifbody.length) 'If not the last frame, write End Sign If Addend Then Sm.WriteByte (Me.gifend) 'Creating Graphical IMG = Image.FromStream (SM)' Close Stream Sm.Close () Return Img End Function 'Displaying a Frame Image Private Sub SetPictopicturBox (Byval Img as Image) Me.PictureBox1.Image = img PictureBox1.Top = 0 PictureBox1.Left = 0 If PictureBox1.SizeMode <> PictureBoxSizeMode.AutoSize Then Exit Sub Me.Width = Me.PictureBox1.Width Me.Height =

转载请注明原文地址:https://www.9cbs.com/read-24725.html

New Post(0)