using System; using System.ComponentModel; using System.ComponentModel.Design; using System.Drawing; using System.Drawing.Design; using System.IO; using System.Linq; using System.Windows.Forms; using TagLib; using File = System.IO.File; namespace MediaTagger { public partial class MainForm : Form { private TagLib.File _tagInstance; public MainForm() { InitializeComponent(); MediaFileDragDropLabel.Tag = MediaFileDragDropLabel.Text; } private void Reset() { LyricsTextBox.DataBindings.Clear(); LyricsTextBox.Clear(); MediaFileDragDropLabel.Text = MediaFileDragDropLabel.Tag as string; AdvPropertyGrid.SelectedObject = null; EasyPropertyGrid.SelectedObject = null; CoverPictureBox.Image = null; _tagInstance = null; } private void Common_DragEnter(object sender, DragEventArgs e) { var bMatchDropFormat = e.Data.GetDataPresent(DataFormats.FileDrop); var bMatchDataType = e.Data.GetData(DataFormats.FileDrop) is string[]; var bMatchFileCount = ((string[])e.Data.GetData(DataFormats.FileDrop)).Length == 1; var bMatchExistFile = File.Exists(((string[])e.Data.GetData(DataFormats.FileDrop))[0]); e.Effect = bMatchDropFormat && bMatchDataType && bMatchFileCount && bMatchExistFile ? DragDropEffects.Link : DragDropEffects.None; } private void MediaFileDragDropLabel_DragDrop(object sender, DragEventArgs e) { _tagInstance?.Dispose(); Reset(); var path = ((string[])e.Data.GetData(DataFormats.FileDrop))[0]; _tagInstance = TagLib.File.Create(path); MediaFileDragDropLabel.Text = path + Environment.NewLine + "TagType: " + _tagInstance.TagTypes; AdvPropertyGrid.SelectedObject = _tagInstance.Tag; EasyPropertyGrid.SelectedObject = new EasyTag(_tagInstance.Tag); CoverPictureBox.Image = _tagInstance.Tag.Pictures.FirstOrDefault()?.Data.Data.LoadAsImage(); LyricsTextBox.DataBindings.Add("Text", _tagInstance.Tag, nameof(_tagInstance.Tag.Lyrics)); } private void SaveButton_Click(object sender, EventArgs e) { _tagInstance.Save(); Reset(); } private void DiscardButton_Click(object sender, EventArgs e) { _tagInstance.Dispose(); Reset(); } private void CoverFileDragDropLabel_DragDrop(object sender, DragEventArgs e) { var path = ((string[])e.Data.GetData(DataFormats.FileDrop))[0]; try { var img = Image.FromFile(path); if (false == System.Drawing.Imaging.ImageFormat.Jpeg.Equals(img.RawFormat)) { img.Dispose(); throw new ArgumentException("No a JPEG file"); } var orig = CoverPictureBox.Image; CoverPictureBox.Image = img; orig?.Dispose(); var bytes = File.ReadAllBytes(path); var pic = new Picture //REF: stackoverflow.com/a/30285220/2430943 { Type = PictureType.FrontCover, Description = "Cover", MimeType = "image/jpeg", Data = bytes, }; _tagInstance.Tag.Pictures = new IPicture[] { pic }; } catch (Exception exception) { MessageBox.Show(exception.ToString(), "ERROR"); } } private void CoverPictureBox_Click(object sender, EventArgs e) { if (null == _tagInstance) return; ; var data = _tagInstance.Tag.Pictures.FirstOrDefault()?.Data.Data; if (null == data) { MessageBox.Show("No cover embedded!"); return; } var dialog = new SaveFileDialog { Title = "Save cover", Filter = "JPEG file(*.jpeg;*.jpg)|*.jpeg;*.jpg" }; if (DialogResult.OK == dialog.ShowDialog(this)) { File.WriteAllBytes(dialog.FileName, data); } } } internal class EasyTag { private readonly Tag _raw; public EasyTag(Tag tag) { _raw = tag; } [Category("Frequently Used")] public string Album { get => _raw.Album; set => _raw.Album = value; } [Category("Frequently Used")] public string Artists { get => string.Join(";", _raw.Performers); set => _raw.Performers = value.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries); } [Category("Frequently Used")] public string Title { get => _raw.Title; set => _raw.Title = value; } [Category("Other")] public string Genres { get => string.Join(";", _raw.Genres); set => _raw.Genres = value.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries); } [Category("Other")] public uint Track { get => _raw.Track; set => _raw.Track = value; } [Category("Other")] public uint Year { get => _raw.Year; set => _raw.Year = value; } [Category("Other")] [Editor(typeof(MultilineStringEditor), typeof(UITypeEditor))] public string Lyrics { get => _raw.Lyrics; set => _raw.Lyrics = value; } } internal static class ExtensionMethod { public static Image LoadAsImage(this byte[] data) { using (var ms = new MemoryStream(data)) { return Image.FromStream(ms); } } } }