0

Hi i'm making a small app' that will load some .mp3 songs and resize their covers to required size in bytes.
I think the best will be to changing real resolution until it won't be less than required. But i really don't know how to it or how to save the ID3 pic'.

Songs are loaded from OpenFileDialog and required size is loaded from simple textBox.
I'm working with taglib# and C#(WPF), but if there is better library for this problem, i will not resist.

Here's my example, which truly resize the pic', but it shortened it.

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);
                    }
                }
            }            
}
hradecek
  • 2,455
  • 2
  • 21
  • 30

1 Answers1

1

The Data property is an ArrayList<byte> representing a raw image file. Cutting the size down by chopping off the last bytes is like shrinking an MP3 by deleting the last half or cutting a book in half. You need to take the image data, convert it into an image representation (say, a System.Drawing.Image), scale that image, convert it into a byte array, and store it back in the picture attribute. It would look something like:

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();

You'll have to do some work with regards to how to size the image, how to choose the output format, etc.

Brian Nickel
  • 26,890
  • 5
  • 80
  • 110
  • I know how to do it with WinForms, but i hope you understand that WinForms are not WPF that i'm working with.WPF Image has no methods like Save() or FromStream() – hradecek Jun 22 '12 at 13:43
  • If you really need WPF for this part, see this example: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/580fca03-d15b-48b2-9fb3-2e6fec8a2c68/ – Brian Nickel Jul 08 '12 at 13:40