【问题标题】:Resizing ID3 tag picture调整 ID3 标签图片大小
【发布时间】:2012-06-11 09:33:39
【问题描述】:

您好,我正在制作一个小应用程序,它将加载一些 .mp3 歌曲并调整它们的封面大小以字节为单位。
我认为最好的办法是改变实际分辨率,直到它不会低于要求。但我真的不知道如何操作或如何保存 ID3 pic'。

歌曲从 OpenFileDialog 加载,所需大小从简单 textBox 加载。
我正在使用 taglib# 和 C#(WPF),但是如果有更好的库来解决这个问题,我不会抗拒。

这是我的示例,它真正调整了图片的大小,但它缩短了它。

private void MenuItem_Click(object sender, RoutedEventArgs e)
{
            int size;
            try
            {                
                size = int.Parse(textBox1.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("Enter requiered size!", "Err");
                return;
            }

            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();         
            dlg.DefaultExt = ".mp3";
            dlg.Filter = "mp3 files (.mp3) | *.mp3";
            dlg.Multiselect = true;

            Nullable<bool> result = dlg.ShowDialog();            

            if (result == true)
            {
                foreach (string file in dlg.FileNames)
                {
                    var song = TagLib.File.Create(file);
                    if (song.Tag.Pictures.Length > 0)
                    {
                        // var bin = (byte[])(song.Tag.Pictures[0].Data.Data);                                                
                        song.Tag.Pictures[0].Data.Resize(size);
                    }
                }
            }            
}

【问题讨论】:

    标签: c# wpf id3 taglib-sharp


    【解决方案1】:

    Data 属性是一个代表原始图像文件的ArrayList&lt;byte&gt;。通过砍掉最后一个字节来缩小尺寸就像通过删除最后一半或将一本书切成两半来缩小 MP3。您需要获取图像数据,将其转换为图像表示(例如,System.Drawing.Image),缩放该图像,将其转换为字节数组,然后将其存储回图片属性中。它看起来像:

    MemoryStream inputStream = new MemoryStream(song.Tag.Pictures[0].Data.Data);
    Image picture = Image.FromStream(inputStream);
    // Scale the image: http://www.codeproject.com/Articles/2941/Resizing-a-Photographic-image-with-GDI-for-NET
    MemoryStream ouputStream = new MemoryStream();
    picture.Save(outputStream, imageFormat);
    song.Tag.Pictures[0].Data = outputStream.ToArray();
    

    您必须在如何调整图像大小、如何选择输出格式等方面做一些工作。

    【讨论】:

    • 我知道如何使用 WinForms,但我希望您了解 WinForms 不是我正在使用的 WPF。WPF Image 没有像 Save() 或 FromStream() 这样的方法
    • 如果这部分确实需要 WPF,请看这个例子:social.msdn.microsoft.com/forums/en-US/wpf/thread/…
    猜你喜欢
    • 2012-10-02
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多