Пример #1
0
 public void DevTests()
 {
     using (var mp3 = new Mp3File(@"E:\Temp\Audio\BasicTagsWithImage.mp3", Mp3Permissions.ReadWrite))
     {
         mp3.DeleteAllTags();
         var tag        = new Id3Tag();
         var frontCover = new PictureFrame {
             Description = "The Front Cover",
             MimeType    = "image/jpg",
             PictureType = PictureType.FrontCover
         };
         frontCover.LoadImage(@"E:\Temp\Audio\FrontCover.jpg");
         tag.Pictures.Add(frontCover);
         var fileIcon = new PictureFrame {
             Description = "The File Icon",
             MimeType    = "image/png",
             PictureType = PictureType.Other
         };
         fileIcon.LoadImage(@"E:\Temp\Audio\MSN.png");
         tag.Pictures.Add(fileIcon);
         mp3.WriteTag(tag);
         foreach (Id3Frame frame in tag)
         {
             Console.WriteLine(frame);
         }
     }
 }
Пример #2
0
        public void ReproduceTest()
        {
            using (var mp3 = new Mp3File(@"E:\Temp\Audio\BasicTagsWithImage.mp3", Mp3Permissions.ReadWrite))
            {
                mp3.DeleteAllTags();
                var tag = new Id3Tag();
                var pic = new PictureFrame {
                    Description  = "The Front Cover",
                    EncodingType = Id3TextEncoding.Iso8859_1
                };
                pic.LoadImage(@"E:\Temp\Audio\FrontCover.jpg");
                tag.Pictures.Add(pic);
                mp3.WriteTag(tag, 2, 3);
            }

            using (var mp3 = new Mp3File(@"E:\Temp\Audio\BasicTagsWithImage.mp3"))
            {
                Id3Tag       tag = mp3.GetTag(2, 3);
                PictureFrame pic = tag.OfType <PictureFrame>().First();
                pic.SaveImage(@"E:\Temp\FrontCoverSaved.jpg");
            }
        }
Пример #3
0
        private static void MainProcess(Mp3 mp3, string mp3Path, string[] images)
        {
            var tag = mp3.GetTag(Id3TagFamily.Version2X);

            if (tag == null)
            {
                tag = new Id3Tag();
                if (!mp3.WriteTag(tag, Id3Version.V23, WriteConflictAction.Replace))
                {
                    ShowError($"Failed to create tag to mp3 file.");
                }
            }

            // ask option
            bool addCover;
            bool removeFirst;
            var  originCoverCount = tag.Pictures.Count;

            if (originCoverCount == 0)
            {
                var ok = MessageBoxEx.Show($"There is no cover in the given mp3 file, would you want to add the given {images.Length} cover(s) to this mp3 file?",
                                           MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, new string[] { "&Add", "&Cancel" });
                addCover    = ok == DialogResult.OK;
                removeFirst = false;
            }
            else
            {
                var ok = MessageBoxEx.Show($"The mp3 file has {originCoverCount} cover(s), would you want to remove it first, and add the given {images.Length} cover(s) to this mp3 file?",
                                           MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, new string[] { "&Replace", "&Append", "&Cancel" });
                addCover    = ok != DialogResult.Cancel;
                removeFirst = ok == DialogResult.Yes;
            }

            // handle tag
            if (!addCover)
            {
                return;
            }
            if (removeFirst)
            {
                tag.Pictures.Clear();
            }
            foreach (var image in images)
            {
                var extension = Path.GetExtension(image).ToLower();
                var mime      = "image/";
                if (extension == ".png")
                {
                    mime = "image/png";
                }
                else if (extension == ".jpg" || extension == ".jpeg")
                {
                    mime = "image/jpeg";
                }
                var newCover = new PictureFrame()
                {
                    PictureType = PictureType.FrontCover,
                    MimeType    = mime
                };
                try {
                    newCover.LoadImage(image);
                } catch (Exception ex) {
                    ShowError($"Failed to load image: \"{image}\". Details:\n{ex}");
                    return;
                }
                tag.Pictures.Add(newCover);
            }

            // write tag
            mp3.DeleteTag(Id3TagFamily.Version2X);
            if (!mp3.WriteTag(tag, Id3Version.V23, WriteConflictAction.Replace))
            {
                ShowError($"Failed to write cover(s) to mp3 file.");
                return;
            }

            string msg;

            if (removeFirst)
            {
                msg = $"Success to remove {originCoverCount} cover(s) and add {images.Length} cover(s) to mp3 file \"{mp3Path}\".";
            }
            else if (originCoverCount != 0)
            {
                msg = $"Success to add {images.Length} cover(s), now there are {images.Length + originCoverCount} covers in the mp3 file \"{mp3Path}\".";
            }
            else
            {
                msg = $"Success to add {images.Length} cover(s) to mp3 file \"{mp3Path}\".";
            }
            MessageBoxEx.Show(msg, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }