一
动画打开与帧读取
☆Function gifLoad([gifFile As String = ""]) As Long
【功能】打开GIF动画文件,也可以打开GIF图像。
【参数】gifFile输入要打开的GIF动画或图像文件,文件后缀通常为.GIF。当省略该输入参数时,使用gifFilename属性指定的文件名称。
【返回】调用成功返回值大于0,否则返回值小于或等于0。
【参考】gifFilename,gifFree。
☆Property gifFilename As String
【功能】设置或获取GIF文件名称,只读属性。
☆Function gifFirstFrame() As Long
【功能】获取动画的第1帧作为当前帧图像。
【返回】调用成功返回值大于0,否则返回值小于或等于0。
【参考】gifNextFrame。
☆Function gifNextFrame() As Long
【功能】获取下一帧作为当前帧图像。
【返回】调用成功返回值大于0,否则返回值小于或等于0。
【参考】gifFirstFrame。
☆Sub gifFree()
【功能】释放gifLoad打开的动画内存。
【参考】gifLoad。
二
当前帧图像操作
☆Function DisplayFrame() As Integer
【功能】显示当前帧图像。
【返回】调用成功返回值大于0,否则返回值小于或等于0。
☆Property BorderStyle As Integer
【功能】设置与读取控件边框。
☆Sub Cls()
【功能】清除控件上所显示的图像。
☆Property gifWidth As Long
【功能】设置或读取当前帧图像的宽度(像素)。
【参考】gifHeight, gifRowBytes, gifBits, gifHandle。
☆Property gifHeight As Long
【功能】设置或读取当前帧图像的高度(像素)。
【参考】gifWidth, gifRowBytes, gifBits, gifHandle。
☆Property gifBits As Integer
【功能】设置或读取当前帧图像的颜色比特数。
【参考】gifWidth, gifHeight,gifRowBytes, gifHandle。
☆Property gifRowBytes As Long
【功能】获取当前帧图像的每一行所占内存的字节数。
【参考】gifWidth, gifHeight, gifBits, gifHandle。
☆Property Handle As Long
【功能】当前帧图像数据的内存句柄。
【参考】gifWidth, gifHeight, gifBits, gifRowBytes。
☆Sub gifPaletteGet(ByVal index As Integer, cRed As Integer, cGreen As Integer, cBlue As Integer)
【功能】获取当前帧图像的颜色调色板。
【参数】index输入调色板序号,取值范围是0~255。cRed,cGreen,cBlue返回调色板的颜色分量红、绿、蓝。
【参考】gifPaletteSet。
☆Sub gifPaletteSet(ByVal index As Integer, ByVal cRed As Integer, ByVal cGreen As Integer, ByVal cBlue As Integer)
【功能】设置当前帧图像的颜色调色板。
【参数】index输入调色板序号,取值范围是0~255。cRed,cGreen,cBlue输入调色板的颜色分量红、绿、蓝。
【参考】gifPaletteSet。
☆Function OpenBMPFile(ByVal Fname As String) As Long
【功能】打开BMP格式图像作为当前帧。
【参数】Fname输入BMP格式图像文件名和路径。
【返回】调用成功返回值大于0,否则返回值小于或等于0。
【参考】SaveBMPFile。
☆ Function SaveBMPFile(ByVal Fname As String) As Long
【功能】保存当前帧图像为BMP格式图像文件。
【参数】Fname输入BMP格式图像文件名和路径。
【返回】调用成功返回值大于0,否则返回值小于或等于0。
【参考】OpenBMPFile。
三
创建动画文件
☆ Function gifCreateFile([gifFile As String = ""]) As Long
【功能】新建GIF动画文件。
【参数】gifFile输入新建动画文件名称和路径,当该参数省略时,用gifFilename属性指定的文件名作为新建动画文件名。
【返回】调用成功返回值大于0,否则返回值小于或等于0。
【参考】gifCreateFileClose。
☆ Sub gifCreateFileClose()
【功能】关闭由gifCreateFile创建的动画文件。
【参考】gifCreateFile。
☆ Function gifAppendFrame() As Long
【功能】将当前帧图像增加到新建动画中。
【返回】调用成功返回值大于0,否则返回值小于或等于0。
【参考】OpenBMPFile,NewFrame。
☆ Function NewFrame(ByVal width As Long, ByVal height As Long) As Long
【功能】新建当前帧图像。
【参数】width输入新建图像宽度,height输入新建图像高度。
【返回】调用成功返回值大于0,否则返回值小于或等于0。
【参考】gifSetFrameData,gifGetFrameData。
☆ Function gifGetFrameData(pdata() As Byte) As Long
【功能】获取当前帧图像数据。
【参数】pdata()数组返回整幅图像的数据,数组的大小为gifWidth*gifRowBytes。
【返回】调用成功返回值大于0,否则返回值小于或等于0。
【参考】gifSetFrameData。
☆ Function gifSetFrameData(pdata() As Byte) As Long
【功能】设置当前帧图像数据。
【参数】pdata()数组输入整幅图像的数据,数组的大小为gifWidth*gifRowBytes。
【返回】调用成功返回值大于0,否则返回值小于或等于0。
【参考】gifGetFrameData。
☆ Function GetContinousColor(ByVal id As Integer, ByVal index As Integer, cRGB() As Byte) As Long
【功能】获取连续变化的颜色。
【参数】id输入参数,表示要使用的颜色类型,id=0表示亮度由暗变亮的灰度色,id=1表示HSV颜色,id=2表示红色调颜色集合,id=3表示蓝色调颜色集合。index输入参数,取值范围从0到255,是连续颜色号,cRGB()输出数祖,长度为3,表示所取得的颜色,cRGB(0)红色分量,cRGB(1)绿色分量,cRGB(2)蓝色分量。
【返回】长整数形式表示的颜色。
四
控件的事件
☆ Event Click()
【功能】鼠标在控件上单击发生的事件。
☆ Event DblClick()
【功能】鼠标在控件上双击发生的事件。
☆ Event KeyDown(KeyCode As Integer, Shift As Integer)
【功能】键盘按下键时发生的事件。
☆ Event KeyPress(KeyAscii As Integer)
【功能】键盘一直按下键时发生的事件。
☆ Event KeyUp(KeyCode As Integer, Shift As Integer)
【功能】键盘按下键松开时发生的事件。
☆ Event MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
【功能】鼠标按下键时发生的事件。
☆ Event MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
【功能】鼠标在控件上移动时发生的事件。
☆ Event MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
【功能】鼠标按下键松开时发生的事件。
五
控件的相关文件
控件由两个文件组成,它们是gifAni45.ocx, gifAni45.dll,使用时需要把这两个文件复制到WINDOWS目录的SYSTEM32目录下,或者是应用程序当前目录下,并且对gifAni45.ocx文件用Regsvr32.exe程序进行注册。
一
打开并显示GIF动画、GIF图像、BMP图像文件
Private Sub idm_Open_Click()
Dim Fname As String, iRet As Long
'使用公用对话框获取文件名称:Fname
With CommonDialog1
.CancelError = True
.Filter = "Available|*.bmp;*.gif|GIF File(*.gif)|*.gif|BMP Files(*.bmp)|*.bmp"
.DialogTitle = "选择BMP图像文件或GIF文件"
On Error Resume Next
.ShowOpen
If Err = 0 Then
Fname = .FileName
Else
Exit Sub
End If
End With
With GifAnimation1
If LCase(Right(Fname, 4)) = ".gif" Then
'读取GIF文件并显示第1帧
.gifFree '释放先前打开的动画内存
.gifFilename = Fname
iRet = .gifLoad()
If iRet > 0 Then
.Cls '清除控件上的图像
.gifFirstFrame '读取动画中的第1帧图像作为当前帧图像
.DisplayFrame '显示当前帧图像
Me.Caption = Fname
End If
ElseIf LCase(Right(Fname, 4)) = ".bmp" Then
'读取BMP文件并显示图像
iRet = .OpenBMPFile(Fname)
If iRet > 0 Then
.Cls '清除控件上的图像
iRet = .DisplayFrame() '显示当前帧图像
Me.Caption = Fname
End If
End If
End With
End Sub
'如果GIF动画文件打开成功,则以下程序可以显示GIF动画中的下一帧图像
Private Sub idm_Next_Click()
If GifAnimation1.gifNextFrame() > 0 Then '获取GIF动画中的下一帧图像作为当前图像
GifAnimation1.DisplayFrame '显示当前图像
End If
End Sub
'如果GIF动画文件打开成功,则可以采用下列程序从第1帧开始播放动画
Private Sub idm_Play_Click()
'显示第1帧图像
If GifAnimation1.gifFirstFrame() > 0 Then '获取第1帧图像为当前图像
GifAnimation1.DisplayFrame '显示当前图像
Else
Exit Sub
End If
Do While GifAnimation1.gifNextFrame() > 0 '获取下一帧图像为当前图像成功继续,失败退出循环
GifAnimation1.DisplayFrame '显示当前图像
DoEvents '允许鼠标键盘等其他事件输入
SleepEx 40, 0 '延时毫秒
Loop
End Sub
二
当前图像信息获取
(1)当前图像的信息
Private Sub idm_Info_Click()
Dim s As String
With GifAnimation1
s = "图像宽度(像素):" & .gifWidth & vbLf
s = s & "图像高度(像素):" & .gifHeight & vbLf
s = s & "图像颜色比特数:" & .gifBits & vbLf
s = s & "图像每一行所占内存字节数目:" & .gifRowBytes & vbLf
s = s & "图像内存句柄:" & .Handle & vbLf
s = s & "动画文件名称:" & .gifFilename
MsgBox s, vbOKOnly + vbInformation, "当前图像信息"
End With
End Sub
(2)当前帧图像调色板显示
'在Form2窗体显示当前调色板的256种颜色
Private Sub idm_Palette_Click()
Dim pal(0 To 768) As Byte
Dim n As Long, i As Integer
Dim cRed As Integer, cGreen As Integer, cBlue As Integer
'当前图像调色板颜色的个数
Select Case GifAnimation1.gifBits
Case 1: n = 2
Case 2: n = 4
Case 4: n = 16
Case 8: n = 256
Case Else
n = 256
End Select
'获取所有的调色板颜色
For i = 0 To n - 1
GifAnimation1.gifPaletteGet i, cRed, cGreen, cBlue
pal(i * 3) = cRed
pal(i * 3 + 1) = cGreen
pal(i * 3 + 2) = cBlue
Next i
'在窗体Form2中显示所有颜色
Form2.Show 0, Me
Form2.Caption = "调色板之颜色"
Form2.DispPal pal, n '绘制调色板颜色,参见后面关于该函数的说明
End Sub
窗体Form2中调色板颜色显示函数
Public Sub DispPal(pal() As Byte, ByVal n As Long)
Dim i As Long, j As Long, k As Long
Dim dx As Single, dy As Single, color As Long
Dim x1 As Single, y1 As Single, x2 As Single, y2 As Single
Me.ScaleMode = 3 '窗体按照像素来度量
'设置显示区域(x1,y1)-(x2,y2)
x1 = 10: y1 = 10
x2 = Me.ScaleWidth - 10
y2 = Me.ScaleHeight - 10
If x2 - x1 < 32 Or y2 - y1 < 32 Then '区域太小
Exit Sub
End If
dx = (x2 - x1) / 16
dy = (y2 - y1) / 16
Me.Cls '清除窗体
'绘制256种调色板颜色,每个颜色占1小方块
For i = 0 To 15
For j = 0 To 15
k = i * 16 + j
If k < n Then
color = RGB(CInt(pal(k * 3)), CInt(pal(k * 3 + 1)), CInt(pal(k * 3 + 2)))
Me.Line (x1 + j * dx, y1 + i * dy)-(x1 + j * dx + dx, y1 + i * dy + dy), color, BF
End If
If i = 15 Then
Me.Line (x1 + j * dx, y1)-(x1 + j * dx, y2), 0&
End If
Next j
Me.Line (x1, y1 + i * dy)-(x2, y1 + i * dy), 0&
Next i
End Sub
三
保存动画为系列图像文件
'保存动画中的每一帧为当前目录下的一系列BMP格式图像文件
Private Sub idm_WriteFrames_Click()
Dim p As String, Fname As String, c As Long
p = App.Path '获取应用程序当前目录
If Right(p, 1) <> "\" Then
p = p & "\"
End If
c = 0 '图像个数计数器置0
GifAnimation1.gifFirstFrame '获取第1帧作为当前图像
Fname = p & "Frame000.bmp" '第1帧图像文件名
Call GifAnimation1.SaveBMPFile(Fname) '保存第1帧图像
c = 1 '图像个数计数器置1
Do While GifAnimation1.gifNextFrame() > 0 '获取下一帧图像为当前图像成功,则继续,否则退出循环
Fname = p & "Frame" & Format(c, "000") & ".bmp" '建立当前帧图像文件名
GifAnimation1.SaveBMPFile Fname '保存当前图像为BMP格式文件
c = c + 1 '图像个数计数器增加1
Loop
MsgBox c & " 帧图像保存成功。", vbOKOnly + vbInformation, "提示用户"
End Sub
四
把一系列图像文件转换成GIF动画
本例设在当前目录下有一些BMP格式的256色图像文件,把这些文件按照文件名由小到大顺序作为GIF动画的帧,形成一个GIF动画文件保存在当前目录下,名称为Ani.GIF。
Private Sub idm_WriteGif_Click()
Dim i As Long, iRet As Long, c As Long, goon As Boolean
Dim GifFile As String, bmpFile As String
'把当前目录下的所有bmp文件当做动画的每一帧图片,顺序是按照文件名由小到大排列。
'要求每张图像文件都是256色的。
'使用VB文件列表控件File1来获取当前目录下的*.BMP文件名称
File1.Path = App.Path '设置文件列表控件File1的路径为当前目录
File1.Pattern = "*.bmp" '设置文件列表控件File1的文件后缀
If File1.ListCount > 1 Then '如果BMP文件不只一个
'最后在当前目录下生成的动画文件名称Ani.GIF
If Right(App.Path, 1&) = "\" Then
GifFile = App.Path & "Ani.Gif"
Else
GifFile = App.Path & "\Ani.Gif"
End If
goon = False
c = 0
For i = 0 To File1.ListCount - 1
'读入帧图片文件,要求BMP格式,256色图像
If Right(App.Path, 1&) = "\" Then
bmpFile = App.Path & File1.List(i)
Else
bmpFile = App.Path & "\" & File1.List(i)
End If
'打开BMP图像文件为控件当前图像
iRet = GifAnimation1.OpenBMPFile(bmpFile)
If iRet > 0 And GifAnimation1.gifBits = 8 Then
'只能生成256色GIF动画
If i = 0 Then
'依据第1帧的图片的参数(宽度、高度)来建立GIF动画
iRet = GifAnimation1.gifCreateFile(GifFile)
If iRet <= 0 Then
Exit For
Else
goon = True
End If
Else
If goon = False Then
Exit For
End If
End If
'增加动画帧图片
GifAnimation1.gifAppendFrame
c = c + 1 '帧记数器加一
End If
Next i
If c > 0 And goon Then
'关闭动画文件
GifAnimation1.gifCreateFileClose
'释放当前动画空间
GifAnimation1.gifFree
End If
MsgBox "GIF动画制作完成,共含" & c & "帧", vbOKOnly + vbInformation, "信息提示"
End If
End Sub
五
用程序产生帧图像创建GIF动画文件
本例中,用随时间变化的二元函数z(x, y, t), x = -1~1,y = 1~1,t = 0.1~2
来产生每个时刻的动画帧图像,由一系列时刻的帧图像构成GIF动画。
Private Sub idm_New_Click()
Dim Fname As String, nsi As Long
'GIF动画文件设置为当前目录下new.gif
If Right(App.Path, 1) = "\" Then
Fname = App.Path & "new.gif"
Else
Fname = App.Path & "\new.gif"
End If
With GifAnimation1
'建立8比特调色板图像,宽度为229,高度为220
.NewFrame 229, 220
'设置256色调色板的值
Dim index As Integer
Dim cRGB(0 To 3) As Byte, cRed As Integer, cGreen As Integer, cBlue As Integer
For index = 0 To 255
.GetContinousColor 1, index, cRGB '获取连续变化颜色
cRed = cRGB(0): cGreen = cRGB(1): cBlue = cRGB(2)
.gifPaletteSet index, cRed, cGreen, cBlue '设置调色板的颜色值
Next index
'建立动画文件
If .gifCreateFile(Fname) <= 0 Then
Exit Sub
End If
'随时间变化的数据产生 z(x,y,t)=exp[-(x*x+y*y)/(d(t)*d(t)]
'd(t)=0.1 + t*0.02, t=0,1, ..., 100; x=(-1,1); y=(-1,1)
Dim t As Long, x As Double, y As Double, z As Double, d As Double, i As Long, j As Long
Dim pos As Long
nsi = .gifHeight * .gifRowBytes '帧图像数据内存大小
ReDim p(0 To nsi - 1) As Byte '为数据分配空间
Screen.MousePointer = 11
For t = 0 To 99 '循环产生100幅图像
'对应时间t,产生一个图像帧
d = 0.02 * t + 0.1
For i = 0 To .gifHeight - 1
pos = (.gifHeight - 1 - i) * .gifRowBytes
y = -1 + 2# / .gifHeight * i
For j = 0 To .gifWidth - 1
x = -1 + 2# / .gifWidth * j
z = Exp(-(x * x + y * y) / (d * d))
p(pos) = CByte(z * 255)
pos = pos + 1
Next j
Next i
'设置当前图像数据
.gifSetFrameData p
'把图像添加到动画之中
.gifAppendFrame
Next t
'关闭GIF动画文件
.gifCreateFileClose
Screen.MousePointer = 0
End With
End Sub
ChengBo Software Workshop
Email: chengbosoft@126.com