본문 바로가기

Technique/C#

MP3 파일에서 앨범 커버 및 음악파일 정보 추출

반응형

 

 

1, 아래 DLL 파일을 참조 추가

 

UltraID3Lib.dll

 

2, 참조 추가후 behind code 상단에 아래 using 추가

 

 

 

 

 

 

 using HundredMilesSoftware.UltraID3Lib;

 

 

 

 

 

 

 

3, 메인 소스 부분에 (필요한부분)에 아래 코드를 수정하여 추출 및 이미지 생성 가능

 

 

 

 

 

 

                        FileInfo ff = new FileInfo(ds.FullName);

                        if (ff.Exists)
                        {
                            try
                            {
                                UltraID3 myMp3 = new UltraID3();

                                myMp3.Read(ff.FullName);

                                ID3FrameCollection myFrames = myMp3.ID3v2Tag.Frames.GetFrames(MultipleInstanceID3v2FrameTypes.ID3v23Picture);

                                System.Drawing.Bitmap bi = ((ID3v23PictureFrame)myFrames[0]).Picture;

                                System.Drawing.Bitmap mpImgBitmap;

                                // 저장할 경로
                                string AlbumArtFilePath = Path.ChangeExtension(ff.FullName, "png");
                                FileInfo fc = new FileInfo(AlbumArtFilePath);
                                if (!fc.Exists)
                                {
                                    try
                                    {
                                        mpImgBitmap = ((ID3v23PictureFrame)myFrames[0]).Picture;
                                        mpImgBitmap.Save(AlbumArtFilePath, System.Drawing.Imaging.ImageFormat.Png);
                                    }
                                    catch
                                    {
                                        Console.WriteLine("noImage");
                                    }
                                }

                                BitmapImage bmp = new BitmapImage();
                                bmp.BeginInit();
                                bmp.UriSource = new Uri(AlbumArtFilePath);
                                bmp.CacheOption = BitmapCacheOption.None;
                                bmp.DecodePixelWidth = 180;
                                bmp.DecodePixelHeight = 100;
                                bmp.EndInit();
                                object[] k = new object[3];
                                k[0] = bmp;
                                k[1] = myMp3.ID3v2Tag.Title;
                                k[2] = ff.FullName;
                                itemSource.Add(k);

                                bmp = null;
                                GC.Collect();
                                GC.WaitForPendingFinalizers();
                            }
                            catch (Exception ex)
                            {
                                //   MessageBox.Show("Error File :" + f.Name + "- Error Messang : " + ex.Message, "ContentBind");
                            }

                   }

 

 

 

 

 

 

반응형