/
ID3v2UserControl.cs
225 lines (187 loc) · 8.25 KB
/
ID3v2UserControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using IdSharp.AudioInfo;
using IdSharp.AudioInfo.Inspection;
using IdSharp.Tagging.ID3v1;
using IdSharp.Tagging.ID3v2;
using IdSharp.Tagging.ID3v2.Frames;
namespace IdSharp.Tagging.Harness.WinForms.UserControls
{
public partial class ID3v2UserControl : UserControl
{
private IID3v2Tag _id3v2;
public ID3v2UserControl()
{
InitializeComponent();
cmbGenre.Sorted = true;
cmbGenre.Items.AddRange(GenreHelper.GenreByIndex);
cmbImageType.Items.AddRange(PictureTypeHelper.PictureTypeStrings);
}
private void bindingSource_CurrentChanged(object sender, EventArgs e)
{
IAttachedPicture attachedPicture = GetCurrentPictureFrame();
if (attachedPicture != null)
LoadImageData(attachedPicture);
else
ClearImageData();
}
private void imageContextMenu_Opening(object sender, CancelEventArgs e)
{
miSaveImage.Enabled = (this.pictureBox1.Image != null);
miLoadImage.Enabled = (GetCurrentPictureFrame() != null);
}
private void miSaveImage_Click(object sender, EventArgs e)
{
IAttachedPicture attachedPicture = GetCurrentPictureFrame();
SaveImageToFile(attachedPicture);
}
private void miLoadImage_Click(object sender, EventArgs e)
{
IAttachedPicture attachedPicture = GetCurrentPictureFrame();
LoadImageFromFile(attachedPicture);
}
private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
{
IAttachedPicture attachedPicture = GetCurrentPictureFrame();
LoadImageFromFile(attachedPicture);
}
private void cmbImageType_SelectedIndexChanged(object sender, EventArgs e)
{
IAttachedPicture attachedPicture = GetCurrentPictureFrame();
if (attachedPicture != null)
attachedPicture.PictureType = PictureTypeHelper.GetPictureTypeFromString(cmbImageType.Text);
}
private void txtImageDescription_Validated(object sender, EventArgs e)
{
IAttachedPicture attachedPicture = GetCurrentPictureFrame();
if (attachedPicture != null)
attachedPicture.Description = txtImageDescription.Text;
}
private void LoadImageData(IAttachedPicture attachedPicture)
{
pictureBox1.Image = attachedPicture.Picture;
txtImageDescription.Text = attachedPicture.Description;
cmbImageType.SelectedIndex = cmbImageType.Items.IndexOf(PictureTypeHelper.GetStringFromPictureType(attachedPicture.PictureType));
txtImageDescription.Enabled = true;
cmbImageType.Enabled = true;
}
private void ClearImageData()
{
pictureBox1.Image = null;
txtImageDescription.Text = "";
cmbImageType.SelectedIndex = -1;
txtImageDescription.Enabled = false;
cmbImageType.Enabled = false;
}
private void SaveImageToFile(IAttachedPicture attachedPicture)
{
string extension = attachedPicture.PictureExtension;
imageSaveFileDialog.FileName = "image." + extension;
DialogResult dialogResult = imageSaveFileDialog.ShowDialog();
if (dialogResult == DialogResult.OK)
{
using (FileStream fs = File.Open(imageSaveFileDialog.FileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
fs.Write(attachedPicture.PictureData, 0, attachedPicture.PictureData.Length);
}
}
}
private void LoadImageFromFile(IAttachedPicture attachedPicture)
{
DialogResult dialogResult = imageOpenFileDialog.ShowDialog();
if (dialogResult == DialogResult.OK)
{
attachedPicture.Picture = Image.FromFile(imageOpenFileDialog.FileName);
pictureBox1.Image = attachedPicture.Picture;
}
}
private IAttachedPicture GetCurrentPictureFrame()
{
if (imageBindingNavigator.BindingSource == null)
return null;
return imageBindingNavigator.BindingSource.Current as IAttachedPicture;
}
public void LoadFile(string path)
{
ClearImageData();
_id3v2 = new ID3v2Tag(path);
txtFilename.Text = Path.GetFileName(path);
txtArtist.Text = _id3v2.Artist;
txtTitle.Text = _id3v2.Title;
txtAlbum.Text = _id3v2.Album;
cmbGenre.Text = _id3v2.Genre;
txtYear.Text = _id3v2.Year;
txtTrackNumber.Text = _id3v2.TrackNumber;
chkPodcast.Checked = _id3v2.IsPodcast;
txtPodcastFeedUrl.Text = _id3v2.PodcastFeedUrl;
BindingSource bindingSource = new BindingSource();
imageBindingNavigator.BindingSource = bindingSource;
bindingSource.CurrentChanged += bindingSource_CurrentChanged;
bindingSource.DataSource = _id3v2.PictureList;
switch (_id3v2.Header.TagVersion)
{
case ID3v2TagVersion.ID3v22:
cmbID3v2.SelectedIndex = cmbID3v2.Items.IndexOf("ID3v2.2");
break;
case ID3v2TagVersion.ID3v23:
cmbID3v2.SelectedIndex = cmbID3v2.Items.IndexOf("ID3v2.3");
break;
case ID3v2TagVersion.ID3v24:
cmbID3v2.SelectedIndex = cmbID3v2.Items.IndexOf("ID3v2.4");
break;
}
txtPlayLength.Text = string.Empty;
txtBitrate.Text = string.Empty;
txtEncoderPreset.Text = string.Empty;
Thread t = new Thread(LoadAudioFileDetails);
t.Start(path);
}
private void LoadAudioFileDetails(object pathObject)
{
string path = (string)pathObject;
IAudioFile audioFile = AudioFile.Create(path, false);
decimal bitrate = audioFile.Bitrate; // force bitrate calculation
DescriptiveLameTagReader lameTagReader = new DescriptiveLameTagReader(path);
Invoke(new Action<IAudioFile, DescriptiveLameTagReader>(SetAudioFileDetails), audioFile, lameTagReader);
}
private void SetAudioFileDetails(IAudioFile audioFile, DescriptiveLameTagReader lameTagReader)
{
txtPlayLength.Text = string.Format("{0}:{1:00}", (int)audioFile.TotalSeconds / 60, (int)audioFile.TotalSeconds % 60);
txtBitrate.Text = string.Format("{0:#,0} kbps", audioFile.Bitrate);
txtEncoderPreset.Text = string.Format("{0} {1}", lameTagReader.LameTagInfoEncoder, lameTagReader.UsePresetGuess == UsePresetGuess.NotNeeded ? lameTagReader.Preset : lameTagReader.PresetGuess);
}
public void SaveFile(string path)
{
if (_id3v2 == null)
{
MessageBox.Show("Nothing to save!");
return;
}
if (cmbID3v2.SelectedIndex == cmbID3v2.Items.IndexOf("ID3v2.2"))
_id3v2.Header.TagVersion = ID3v2TagVersion.ID3v22;
else if (cmbID3v2.SelectedIndex == cmbID3v2.Items.IndexOf("ID3v2.3"))
_id3v2.Header.TagVersion = ID3v2TagVersion.ID3v23;
else if (cmbID3v2.SelectedIndex == cmbID3v2.Items.IndexOf("ID3v2.4"))
_id3v2.Header.TagVersion = ID3v2TagVersion.ID3v24;
else
throw new Exception("Unknown tag version");
_id3v2.Artist = txtArtist.Text;
_id3v2.Title = txtTitle.Text;
_id3v2.Album = txtAlbum.Text;
_id3v2.Genre = cmbGenre.Text;
_id3v2.Year = txtYear.Text;
_id3v2.TrackNumber = txtTrackNumber.Text;
_id3v2.IsPodcast = chkPodcast.Checked;
_id3v2.PodcastFeedUrl = txtPodcastFeedUrl.Text;
_id3v2.Save(path);
}
private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
{
Console.WriteLine(_id3v2.PictureList.Count);
}
}
}