Пример #1
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);
        }