Imports System.ComponentModel
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports gif.Components
Public Class Form1
Private imagesPaths As New List(Of String)
Private currentImageIndex As Integer
Private timer1 As New Timer
Private gifImage As Image
Private sourceImage As Bitmap
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
timer1.Interval = 100 ' 调整为 0.15 秒切换一张图片
AddHandler timer1.Tick, AddressOf Timer1_Tick
Timer2.Interval = 100 ' 调整为 0.15 秒切换一张图片
AddHandler Timer2.Tick, AddressOf Timer2_Tick
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "图片文件|*.jpg;*.png;*.bmp;*.gif" ' 注意:添加了 *.gif 以允许选择 GIF 图片
openFileDialog1.Multiselect = True
If openFileDialog1.ShowDialog() = DialogResult.OK Then
imagesPaths.Clear()
For Each filePath As String In openFileDialog1.FileNames
imagesPaths.Add(filePath)
ListBoxImages.Items.Add(Path.GetFileName(filePath))
Next
End If
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
If imagesPaths.Count > 0 Then
currentImageIndex = 0
timer1.Start()
Else
MessageBox.Show("请先选择图片")
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
If currentImageIndex < imagesPaths.Count Then
Dim imagePath = imagesPaths(currentImageIndex)
Dim image As Image
If Path.GetExtension(imagePath).ToLower() = ".gif" Then
image = Image.FromFile(imagePath)
Else
image = New Bitmap(imagePath)
' 对非 GIF 图片进行缩放以适应 PictureBox
Dim newWidth As Integer = PictureBox1.Width
Dim newHeight As Integer = PictureBox1.Height
Dim newImage As Image = New Bitmap(image, newWidth, newHeight)
image = newImage
End If
PictureBox1.Image = image
currentImageIndex += 1
Else
timer1.Stop()
currentImageIndex = 0
End If
End Sub
Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If imagesPaths.Count > 0 Then
currentImageIndex = 0
timer2.Start()
Else
MessageBox.Show("请先选择图片")
End If
End Sub
Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As EventArgs)
If currentImageIndex < imagesPaths.Count Then
Dim imagePath = imagesPaths(currentImageIndex)
Dim image As Image
If Path.GetExtension(imagePath).ToLower() = ".gif" Then
image = image.FromFile(imagePath)
Else
image = New Bitmap(imagePath)
' 对非 GIF 图片进行缩放以适应 PictureBox
Dim newWidth As Integer = PictureBox1.Width
Dim newHeight As Integer = PictureBox1.Height
Dim newImage As Image = New Bitmap(image, newWidth, newHeight)
image = newImage
End If
PictureBox1.Image = image
currentImageIndex += 1
Else
currentImageIndex = 0
End If
End Sub
Private Sub Button5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button5.Click
' 创建 Gif
Dim imagePath = imagesPaths(currentImageIndex)
Dim image As Image
' 以下这行代码应该是 Image.FromFile(imagePath) 而不是 image.FromFile(imagePath)
image = image.FromFile(imagePath)
Dim outputFilePath As String = "a.gif"
Dim gifdll As New AnimatedGifEncoder()
gifdll.Start(outputFilePath)
gifdll.SetDelay(100)
' 延迟间隔
gifdll.SetRepeat(0)
'-1:不循环,0:总是循环 播放
Dim i As Integer = 0
' 以下这行代码应该是 imagesPaths.Count 而不是 imagesPaths(currentImageIndex)
Dim count As Integer = imagesPaths.Count
While i < count
gifdll.AddFrame(image.FromFile(imagesPaths(i)))
i += 1
End While
gifdll.Finish()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
' 创建 Gif
Dim imageFilePaths As [String]() = New [String]() {"1.png", "9.png", "17.png", "23.png", "29.png"}
Dim outputFilePath As [String] = "e.gif"
Dim gifdll As New AnimatedGifEncoder()
gifdll.Start(outputFilePath)
gifdll.SetDelay(200)
' 延迟间隔
gifdll.SetRepeat(0)
'-1:不循环,0:总是循环 播放
Dim i As Integer = 0, count As Integer = imageFilePaths.Length
While i < count
gifdll.AddFrame(Image.FromFile(imageFilePaths(i)))
i += 1
End While
gifdll.Finish()
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Timer2.Stop()
End Sub
End Class