try
{
FileStream fs = new FileStream(@"D:\音乐\陈冠蒲 - 太多.mp3", FileMode.Open);
byte[] header = new byte[10]; //标签头
fs.Read(header, 0, 10);
string head = Encoding.Default.GetString(header, 0, 3);
if (head.Equals("ID3"))
{
int sizeAll = header[6] * 0x200000 //获取该标签的尺寸,不包括标签头
+ header[7] * 0x4000
+ header[8] * 0x80
+ header[9];
int size = 0;
byte[] body = new byte[10]; //数据帧头,这里认为数据帧头不包括编码方式
fs.Read(body, 0, 10);
head = Encoding.Default.GetString(body, 0, 4);
while (!("APIC".Equals(head))) //当数据帧不是图片的时候继续查找
{
size = body[size + 4] * 0x1000000 //(不包括帧头)
+ body[size + 5] * 0x10000
+ body[size + 6] * 0x100
+ body[size + 7];
body = new byte[size + 10];
fs.Read(body, 0, size + 10);
head = Encoding.Default.GetString(body, size, 4);
}
size = body[size + 4] * 0x1000000
+ body[size + 5] * 0x10000
+ body[size + 6] * 0x100
+ body[size + 7];
byte[] temp = new byte[4];
fs.Read(temp, 0, 4);
string tmp = Encoding.Default.GetString(temp);
while (!("JFIF".Equals(tmp))) //我发现一个规律就是所有在ID3v2中的图片都会有
{ //JFIF的标志,而图片开始的位置就是这个标志尾部
fs.Seek(-3, SeekOrigin.Current); //所在的字节位置-10,所以当获取到图片数据帧的
fs.Read(temp, 0, 4); //时候,要先查找这个位置,来获取图片
tmp = Encoding.Default.GetString(temp);
}
fs.Seek(-10, SeekOrigin.Current);
byte[] image = new byte[size];
fs.Read(image, 0, size);
MemoryStream ms = new MemoryStream(image);
BitmapImage newBitmapImage = new BitmapImage();
newBitmapImage.BeginInit();
newBitmapImage.StreamSource = ms;
newBitmapImage.EndInit();
image1.Source = newBitmapImage;
}
else { }
}
catch
{ }
来源:http啊://hi.啊baidu.com/dingguidong/item/4577681eb203b7fa9c778acc