Exemplo n.º 1
0
        private void anyadir_Caratula(object sender, EventArgs e)
        {
            if (vistalista.SelectedItems.Count == 1)
            {
                var fileContent = string.Empty;

                using (OpenFileDialog openFileDialog = new OpenFileDialog())
                {
                    vistalista.Clear();
                    columns();
                    openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                    openFileDialog.Filter           = "All files (*.*)|*.*|PNG files (*.*)|*.png*|JPG files (*.jpg)|*.jpg";
                    openFileDialog.FilterIndex      = 3; //Number Filter of Names and Extensions
                    openFileDialog.RestoreDirectory = true;
                    openFileDialog.Multiselect      = true;

                    if (openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        string[] filePath  = openFileDialog.FileNames;
                        int      pathLimit = filePath.Length - 1;
                        int      pathCount = 0;

                        foreach (string fileName in openFileDialog.SafeFileNames)
                        {
                            if (pathLimit == -1)
                            {
                                //Get the path of specified file
                                string filePathOne = openFileDialog.FileName;
                                //Read the contents of the file into a stream
                                var fileStream = openFileDialog.OpenFile();

                                string[]    ext    = fileName.Split('.');
                                TagLib.File tgFile = TagLib.File.Create(filePath[pathCount]);

                                //Add Cover CD (To PictureBox1)
                                Image bitmap = Image.FromFile(filePathOne);
                                pictureBox1.Image = bitmap;
                                //And to the file
                                Etiqueta     et         = new Etiqueta();
                                MemoryStream albmStream = new MemoryStream(tgFile.Tag.Pictures[0].Data.Data);
                                if ((ext[ext.Length - 1] == "jpg") ||
                                    (ext[ext.Length - 1] == "JPG"))
                                {
                                    bitmap.Save(albmStream, ImageFormat.Jpeg);
                                }
                                et.SetImg(albmStream);
                                TagLib.Picture picture = new TagLib.Picture(new TagLib.ByteVector(albmStream.ToArray()));
                                TagLib.Id3v2.AttachedPictureFrame albumCoverPictFrame = new TagLib.Id3v2.AttachedPictureFrame(picture);

                                albumCoverPictFrame.Type = TagLib.PictureType.FrontCover;
                                TagLib.IPicture[] pictFrames = { albumCoverPictFrame };

                                tags.Pictures = pictFrames;

                                InfoMP(fileName, filePathOne);
                            }
                            else
                            {
                                if (pathCount <= pathLimit)
                                {
                                    var fileStream = openFileDialog.OpenFile();
                                    InfoMP(fileName, filePath[pathCount]);
                                    pathCount++;

                                    string[] ext = fileName.Split('.');

                                    TagLib.File tgFile = TagLib.File.Create(filePath[pathCount]);

                                    //Add Cover CD (To PictureBox1)
                                    Image bitmap = Image.FromFile(filePath[pathCount]);
                                    pictureBox1.Image = bitmap;
                                    //And to the file
                                    Etiqueta     et         = new Etiqueta();
                                    MemoryStream albmStream = new MemoryStream(tgFile.Tag.Pictures[0].Data.Data);
                                    if ((ext[ext.Length - 1] == "jpg") ||
                                        (ext[ext.Length - 1] == "JPG"))
                                    {
                                        bitmap.Save(albmStream, ImageFormat.Jpeg);
                                    }
                                    et.SetImg(albmStream);
                                    TagLib.Picture picture = new TagLib.Picture(new TagLib.ByteVector(albmStream.ToArray()));
                                    TagLib.Id3v2.AttachedPictureFrame albumCoverPictFrame = new TagLib.Id3v2.AttachedPictureFrame(picture);

                                    albumCoverPictFrame.Type = TagLib.PictureType.FrontCover;
                                    TagLib.IPicture[] pictFrames = { albumCoverPictFrame };

                                    tags.Pictures = pictFrames;
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        protected void InfoMP(string name, string filePath)
        {
            try
            {
                Etiqueta et  = new Etiqueta();
                string[] ext = name.Split('.');

                //If is a file MP3

                if ((ext[ext.Length - 1] == "mp3") ||
                    (ext[ext.Length - 1] == "MP3"))
                {
                    using (var r = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        byte first  = (byte)r.ReadByte();
                        byte second = (byte)r.ReadByte();
                        byte third  = (byte)r.ReadByte();

                        r.Seek(-128, SeekOrigin.End);
                        byte[] data = new byte[128];
                        int    read = r.Read(data, 0, 128);

                        //Reset Basic Info
                        string title   = "";
                        string artist  = "";
                        string album   = "";
                        string year    = "";
                        string comment = "";

                        //Add Title Info
                        for (int i = 3; i < 33; i++)
                        {
                            if (data[i] != 0)
                            {
                                title += Convert.ToChar(data[i]);
                            }
                        }
                        titulotext.Text = title;
                        et.SetTitle(title);

                        //Add Artist Info
                        for (int i = 33; i < 63; i++)
                        {
                            if (data[i] != 0)
                            {
                                artist += Convert.ToChar(data[i]);
                            }
                        }
                        artistatext.Text = artist;
                        et.SetArtist(artist);

                        //Add Album Info
                        for (int i = 63; i < 93; i++)
                        {
                            if (data[i] != 0)
                            {
                                album += Convert.ToChar(data[i]);
                            }
                        }
                        albumtext.Text = album;
                        et.SetAlbum(album);

                        //Add Year Info
                        for (int i = 93; i < 97; i++)
                        {
                            if (data[i] != 0)
                            {
                                year += Convert.ToChar(data[i]);
                            }
                        }
                        anyotext.Text = year;
                        et.SetYear(year);

                        //Add Comment Info
                        for (int i = 97; i < 127; i++)
                        {
                            if (data[i] != 0)
                            {
                                comment += Convert.ToChar(data[i]);
                            }
                        }
                        comtext.Text = comment;
                        et.SetComment(comment);

                        r.Close();
                    }

                    //Add Genre Info
                    TagLib.File tgFile  = TagLib.File.Create(filePath);
                    string[]    toGenre = tgFile.Tag.Genres;
                    foreach (string g in toGenre)
                    {
                        generotext.Text = g;
                        et.SetGenre(g);
                    }

                    if (toGenre == null)
                    {
                        generotext.Text = string.Empty;
                        et.SetGenre(string.Empty);
                    }

                    toGenre = null;

                    //Add Track Number
                    uint   toTrack = tgFile.Tag.Track;
                    string track   = Convert.ToString(toTrack);

                    if (track == "0")
                    {
                        pistatext.Text = string.Empty;
                        et.SetNumber(string.Empty);
                    }
                    else
                    {
                        pistatext.Text = track;
                        et.SetNumber(track);
                    }

                    //Add of Composer
                    string[] toComp = tgFile.Tag.Composers;
                    foreach (string comp in toComp)
                    {
                        comptext.Text = comp;
                        et.SetComp(comp);
                    }

                    if (toComp == null)
                    {
                        comptext.Text = string.Empty;
                        et.SetComp(string.Empty);
                    }

                    toComp = null;

                    //Add of Num CD
                    uint   toNumcd = tgFile.Tag.Disc;
                    string numcd   = Convert.ToString(toNumcd);

                    if (numcd == "0")
                    {
                        numtext.Text = "1";
                        et.SetNumcd("1");
                    }
                    else
                    {
                        numtext.Text = numcd;
                        et.SetNumcd(numcd);
                    }

                    //Add Cover CD (To PictureBox1)
                    MemoryStream img = new MemoryStream(tgFile.Tag.Pictures[0].Data.Data);
                    et.SetImg(img);
                    Image bitmap = new Bitmap(et.GetImg());
                    pictureBox1.Image = bitmap;

                    tgFile.Dispose();

                    DetectFile(name, filePath, et);
                }
                else
                {
                    string message = "Error: " + name + " es un Archivo Incompatible";
                    string caption = "Error";
                    MessageBox.Show(message, caption,
                                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception e)
            {
                string       message = "Error: " + e.Message;
                string       caption = "Error";
                DialogResult result  = MessageBox.Show(message, caption,
                                                       MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemplo n.º 3
0
        private void vistalista_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (vistalista.SelectedItems.Count <= 1)
                {
                    if (vistalista.SelectedItems.Count == 1)
                    {
                        menuItem1.Enabled = true;
                        menuItem2.Enabled = true;
                    }
                    else //If is zero Disabled
                    {
                        menuItem1.Enabled = false;
                        menuItem2.Enabled = false;
                    }


                    //Cleaning...

                    titulotext.Text  = String.Empty;
                    artistatext.Text = String.Empty;
                    albumtext.Text   = String.Empty;
                    pistatext.Text   = String.Empty;
                    anyotext.Text    = String.Empty;
                    comtext.Text     = String.Empty;
                    generotext.Text  = String.Empty;
                    comptext.Text    = String.Empty;
                    numtext.Text     = String.Empty;

                    string filePath = vistalista.FocusedItem.SubItems[4].Text;
                    string name     = vistalista.FocusedItem.SubItems[0].Text;

                    string[] ext = name.Split('.');

                    if ((ext[ext.Length - 1] == "mp3") ||
                        (ext[ext.Length - 1] == "MP3"))
                    {
                        Etiqueta et = new Etiqueta();

                        using (var r = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        {
                            byte first  = (byte)r.ReadByte();
                            byte second = (byte)r.ReadByte();
                            byte third  = (byte)r.ReadByte();

                            r.Seek(-128, SeekOrigin.End);
                            byte[] data = new byte[128];
                            int    read = r.Read(data, 0, 128);

                            //Reset Basic Info
                            string title   = "";
                            string artist  = "";
                            string album   = "";
                            string year    = "";
                            string comment = "";

                            //Add Title Info
                            for (int i = 3; i < 33; i++)
                            {
                                if (data[i] != 0)
                                {
                                    title += Convert.ToChar(data[i]);
                                }
                            }
                            titulotext.Text = title;
                            et.SetTitle(title);

                            //Add Artist Info
                            for (int i = 33; i < 63; i++)
                            {
                                if (data[i] != 0)
                                {
                                    artist += Convert.ToChar(data[i]);
                                }
                            }
                            artistatext.Text = artist;
                            et.SetArtist(artist);

                            //Add Album Info
                            for (int i = 63; i < 93; i++)
                            {
                                if (data[i] != 0)
                                {
                                    album += Convert.ToChar(data[i]);
                                }
                            }
                            albumtext.Text = album;
                            et.SetAlbum(album);

                            //Add Year Info
                            for (int i = 93; i < 97; i++)
                            {
                                if (data[i] != 0)
                                {
                                    year += Convert.ToChar(data[i]);
                                }
                            }
                            anyotext.Text = year;
                            et.SetYear(year);

                            //Add Comment Info
                            for (int i = 97; i < 127; i++)
                            {
                                if (data[i] != 0)
                                {
                                    comment += Convert.ToChar(data[i]);
                                }
                            }
                            comtext.Text = comment;
                            et.SetComment(comment);

                            r.Close();
                        }

                        //Add Genre Info
                        TagLib.File tgFile  = TagLib.File.Create(filePath);
                        string[]    toGenre = tgFile.Tag.Genres;
                        foreach (string g in toGenre)
                        {
                            generotext.Text = g;
                            et.SetGenre(g);
                        }

                        //Add Track Number
                        uint   toTrack = tgFile.Tag.Track;
                        string track   = Convert.ToString(toTrack);

                        pistatext.Text = track;
                        et.SetNumber(track);

                        //Add of Composer
                        string[] toComp = tgFile.Tag.Composers;
                        foreach (string comp in toComp)
                        {
                            comptext.Text = comp;
                            et.SetComp(comp);
                        }

                        //Add of Num CD
                        uint   toNumcd = tgFile.Tag.Disc;
                        string numcd   = Convert.ToString(toNumcd);

                        numtext.Text = numcd;
                        et.SetNumcd(numcd);

                        //Add Cover CD (To PictureBox1)
                        MemoryStream img = new MemoryStream(tgFile.Tag.Pictures[0].Data.Data);
                        et.SetImg(img);
                        Image bitmap = new Bitmap(et.GetImg());

                        pictureBox1.Image = bitmap;

                        tgFile.Dispose();
                    }
                }
                else
                {
                    //Multiple Slections Disabled for now

                    menuItem1.Enabled = false;
                    menuItem2.Enabled = false;

                    titulotext.Text  = "<mantener>";
                    artistatext.Text = "<mantener>";
                    albumtext.Text   = "<mantener>";
                    pistatext.Text   = "<mantener>";
                    anyotext.Text    = "<mantener>";
                    comtext.Text     = "<mantener>";
                    generotext.Text  = "<mantener>";
                    comptext.Text    = "<mantener>";
                    numtext.Text     = "<mantener>";
                }
            }
            catch (Exception e2)
            {
                string       message2 = "Error: " + e2.Message;
                string       caption2 = "Error";
                DialogResult result   = MessageBox.Show(message2, caption2,
                                                        MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }