通过对LRC文件的解析,可以轻松实现歌词可视化.
函数名:
paintLyrics(ByVal pBox As PictureBox, ByVal CurrentPosition As Integer, ByVal type As Boolean, Optional ByVal pLyric As LyricClass = Nothing)
参数:
pBox:用于绘制歌词的Picturebox
CurrentPosition:当前歌词的时间进度,单位为秒(Second)
type:值为True时强行绘制歌词
pLyric:可选参数,要绘制的歌词类(LyricClass)
功能:
1.歌词居中显示
2.自动匹配显示行数
3.当前歌词突出显示
'绘制歌词 Public Sub paintLyrics(ByVal pBox As PictureBox, ByVal CurrentPosition As Integer, ByVal type As Boolean, Optional ByVal pLyric As LyricClass = Nothing) Dim stringFormat As New StringFormat() With {.LineAlignment = StringAlignment.Center, .Alignment = StringAlignment.Center} Dim tempI, tempII, pWidth, pHeight As Integer Dim myfont As Font Dim mybrush As SolidBrush Dim bmp As Bitmap For i = 0 To LyricData.Count - 1 If CurrentPosition * 100 > LyricData.LyricTime(LyricData.Count - 1 - i) Then tempII = LyricData.Count - 1 - i Exit For End If tempII = -1 Next If Not tempII = tempI Or type = True Then If pLyric Is Nothing Then pLyric = Me tempI = tempII myfont = New Font("微软雅黑", 16) mybrush = New SolidBrush(Color.FromArgb(158, 0, 0, 0)) pWidth = pBox.Width : pHeight = pBox.Height bmp = New Bitmap(pWidth, pHeight) Dim pg As Graphics = Graphics.FromImage(bmp) pg.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias For i = 0 To pHeight '绘制渐变背景 pg.DrawLine(New Pen(Color.FromArgb(10 + Math.Abs(i / (pHeight / 255) - 128), 0, 0, 0), 1), 0, i, pWidth, i) Next If pLyric.LyricData.Count = 0 Then pg.DrawString("未找到歌词", myfont, mybrush, pWidth / 2, pHeight / 2, stringFormat) Else For i = 1 To pHeight / 80 If tempI - i >= 0 Then pg.DrawString(pLyric.LyricData.LyricString(tempI - i), myfont, mybrush, pWidth / 2, pHeight / 2 - i * 40, stringFormat) If tempI + i <= pLyric.LyricData.Count - 1 Then pg.DrawString(pLyric.LyricData.LyricString(tempI + i), myfont, mybrush, pWidth / 2, pHeight / 2 + i * 40, stringFormat) Next myfont = New Font("微软雅黑", 18) mybrush = New SolidBrush(Color.FromArgb(218, 0, 0, 0)) If tempI >= 0 And tempI <= pLyric.LyricData.Count - 1 Then pg.DrawString(pLyric.LyricData.LyricString(tempI), myfont, mybrush, pWidth / 2, pHeight / 2, stringFormat) End If pBox.Image = bmp pg.Dispose() End If End Sub