Example #1
0
        /// <summary>
        /// Downloads a cover and sets it to a picturebox.
        /// </summary>
        /// <param name="coverArtBox">
        /// The picturebox to display the cover.
        /// </param>
        /// <param name="coverID">
        /// The cover id.
        /// </param>
        /// <param name="drawReflection">
        /// The draw reflection.
        /// </param>
        public void SetCoverArt(Image coverArtBox, string coverID, Constants.CoverType coverType)
        {
            string url  = null;
            string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + Constants.AlbumCoverPath;

            switch (coverType)
            {
            case Constants.CoverType.Album:
                url = _server + "getCoverArt.view?id=" + coverID + _authentication + "size=" + Constants.MaxCoverSize;
                break;

            case Constants.CoverType.Artist:
                path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + Constants.ArtistCoverPath;
                break;
            }
            string imagePath = path + coverID + ".png";

            if (File.Exists(imagePath))
            {
                coverArtBox.Source  = new BitmapImage(new Uri(imagePath));
                coverArtBox.Stretch = Stretch.UniformToFill;
                return;
            }

            if (coverType == Constants.CoverType.Artist)
            {
                url = LastFm.GetLastFmArtistUrl(coverID);
            }

            if (string.IsNullOrEmpty(url))
            {
                url = _server + "getCoverArt.view?id=" + "1" + _authentication + "size=" + Constants.MaxCoverSize;
            }

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            var webClient = new WebClient();

            //webClient.DownloadDataCompleted += delegate(object sender, DownloadDataCompletedEventArgs e)
            //{

            //byte[] graphicData = e.Result;
            byte[] graphicData       = webClient.DownloadData(url);
            System.Drawing.Image img = null;
            using (var ms = new MemoryStream(graphicData, 0, graphicData.Length))
            {
                ms.Write(graphicData, 0, graphicData.Length);
                try
                {
                    img = System.Drawing.Image.FromStream(ms, true);
                    img.Save(imagePath, ImageFormat.Png);
                }
                catch (ArgumentException)
                {
                    img = null;
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }

            if (img == null)
            {
                return;
            }

            coverArtBox.Source  = new BitmapImage(new Uri(imagePath));
            coverArtBox.Stretch = Stretch.UniformToFill;
            //};

            //webClient.DownloadDataAsync(new Uri(url));
        }
Example #2
0
        public string GetCoverArtPath(MediaItem mediaItem, Constants.CoverType coverType)
        {
            string url          = null;
            var    assemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

            string path = Path.Combine(assemblyPath, "cache");

            switch (coverType)
            {
            case Constants.CoverType.Album:
                path = Path.Combine(path, "albums");
                break;

            case Constants.CoverType.Artist:
                path = Path.Combine(path, "artists");
                break;
            }
            if (!path.EndsWith("\\"))
            {
                path = path + "\\";
            }

            string fileName  = GeneralExtensions.CleanFileName(mediaItem.AlbumName + ".png");
            string imagePath = GeneralExtensions.CleanPath(path) + fileName;


            if (!File.Exists(imagePath))
            {
                if (coverType == Constants.CoverType.Artist)
                {
                    url = LastFm.GetLastFmArtistUrl(mediaItem.ArtistName);
                }
                else
                {
                    url = LastFm.GetLastFmAlbumUrl(mediaItem);
                }

                // last fm does not know this artist
                if (string.IsNullOrEmpty(url))
                {
                    string backupFile =
                        GeneralExtensions.CleanPath(path) + nameof(Resources.unknownartist) + ".png";
                    if (!File.Exists(backupFile))
                    {
                        if (Resources.unknownartist != null)
                        {
                            File.WriteAllBytes(backupFile,
                                               (byte[])converter.ConvertTo(Resources.unknownartist, typeof(byte[])));
                        }
                    }

                    return(backupFile);
                }


                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                var webClient = new WebClient();

                //webClient.DownloadDataCompleted += delegate(object sender, DownloadDataCompletedEventArgs e)
                //{

                //byte[] graphicData = e.Result;
                byte[] graphicData       = webClient.DownloadData(url);
                System.Drawing.Image img = null;
                using (var ms = new MemoryStream(graphicData, 0, graphicData.Length))
                {
                    ms.Write(graphicData, 0, graphicData.Length);
                    try
                    {
                        img = System.Drawing.Image.FromStream(ms, true);
                        img.Save(imagePath, ImageFormat.Png);
                    }

                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                    }
                }
            }


            return(imagePath);
        }
Example #3
0
        public static Canvas DrawCover(MediaItem item, WrapPanel targetPanel, string Text, MediaItem tag, Constants.CoverType coverType)
        {
            var displayedCanvas = new Canvas()
            {
                Width  = 250,
                Height = 250,
                Margin = new Thickness(10, 10, 0, 0)
            };

            var imagePath =
                LibraryManagement.CoverDownload(item, coverType).Result;

            var cover = new Image()
            {
                VerticalAlignment   = VerticalAlignment.Top,
                HorizontalAlignment = HorizontalAlignment.Left,
                Stretch             = Stretch.UniformToFill,
                Width  = 250,
                Height = 250,
                Source = new BitmapImage(new Uri(imagePath))
            };



            var label = new TextBlock
            {
                MaxWidth     = 200,
                Text         = Text,
                FontSize     = 15,
                Foreground   = Brushes.White,
                TextWrapping = TextWrapping.Wrap
            };

            var labelOutline1 = new TextBlock {
                MaxWidth = 200, Text = Text, FontSize = 15, Foreground = Brushes.Black, TextWrapping = TextWrapping.Wrap
            };
            var labelOutline2 = new TextBlock {
                MaxWidth = 200, Text = Text, FontSize = 15, Foreground = Brushes.Black, TextWrapping = TextWrapping.Wrap
            };
            var labelOutline3 = new TextBlock {
                MaxWidth = 200, Text = Text, FontSize = 15, Foreground = Brushes.Black, TextWrapping = TextWrapping.Wrap
            };
            var labelOutline4 = new TextBlock {
                MaxWidth = 200, Text = Text, FontSize = 15, Foreground = Brushes.Black, TextWrapping = TextWrapping.Wrap
            };

            const int labelFromTop  = 200;
            const int labelFromLeft = 20;

            // White label
            Canvas.SetLeft(label, labelFromLeft);
            Canvas.SetTop(label, labelFromTop);

            // making the outline
            const int borderWith = 1;


            Canvas.SetTop(labelOutline1, labelFromTop - borderWith);
            Canvas.SetLeft(labelOutline1, labelFromLeft - borderWith);

            Canvas.SetTop(labelOutline2, labelFromTop - borderWith);
            Canvas.SetLeft(labelOutline2, labelFromLeft + borderWith);

            Canvas.SetTop(labelOutline3, labelFromTop + borderWith);
            Canvas.SetLeft(labelOutline3, labelFromLeft - borderWith);

            Canvas.SetTop(labelOutline4, labelFromTop + borderWith);
            Canvas.SetLeft(labelOutline4, labelFromLeft + borderWith);

            displayedCanvas.Children.Add(cover);

            displayedCanvas.Children.Add(labelOutline1);
            displayedCanvas.Children.Add(labelOutline2);
            displayedCanvas.Children.Add(labelOutline3);
            displayedCanvas.Children.Add(labelOutline4);

            displayedCanvas.Children.Add(label);
            displayedCanvas.Tag = tag;

            targetPanel.Children.Add(displayedCanvas);

            return(displayedCanvas);
        }
Example #4
0
        /// <summary>
        /// The cover download.
        /// </summary>
        /// <param name="coverBox">
        /// The cover box in which the Image is displayed.
        /// </param>
        /// <param name="id">
        /// The Cover ID for the Download.
        /// </param>
        /// <param name="drawReflection">
        /// Draw a Coverreflection.
        /// </param>
        internal async static Task <string> CoverDownload(MediaItem item, Constants.CoverType coverType)
        {
            var x = Access.GetCoverArtPath(item, coverType);

            return(x);
        }