使用标签库从MP3文件中提取专辑封面 - 有没有更好的方式编写代码?
-
20-08-2019 - |
题
我使用Visual Basic 9(VS2008)和标签库。
下面的代码提取来自MP3文件的专辑封面,并将其显示在一个图片。
有没有更好的方式来写这个代码?
Dim file As TagLib.File = TagLib.File.Create(filepath)
If file.Tag.Pictures.Length >= 1 Then
Dim bin As Byte() = DirectCast(file.Tag.Pictures(0).Data.Data, Byte())
PreviewPictureBox.Image = Image.FromStream(New MemoryStream(bin)).GetThumbnailImage(100, 100, Nothing, System.IntPtr.Zero)
End If
解决方案
我不十分熟悉标签库,但它并不像有很多更好的方法来写这个。我唯一能给你的建议是,你可以通过采取类型推断的优势减少代码量。如果“选项推断”是目前这两个变量的声明并不需要一个明确的类型。这实际上并不改变代码的质量,虽然,它只是减少了它的量。
实施例
Option Infer On
...
Dim file = TagLib.File.Create(filepath)
If file.Tag.Pictures.Length >= 1 Then
Dim bin = DirectCast(file.Tag.Pictures(0).Data.Data, Byte())
PreviewPictureBox.Image = Image.FromStream(New MemoryStream(bin)).GetThumbnailImage(100, 100, Nothing, System.IntPtr.Zero)
End If
其他提示
乍一看,它看起来还好我。
如果TagLib.File.Create()
引发错误或返回“Nothing
”你可以添加一些错误处理,例如。此外,如果Tag
财产因为某种原因是空的,一个错误就会被抛出,如果您尝试访问“.Pictures
”。
不隶属于 StackOverflow