TraktPlugin.GUI.GUIImageHandler.DownloadImage C# (CSharp) Méthode

DownloadImage() public static méthode

Download an image if it does not exist locally
public static DownloadImage ( string url, string localFile ) : bool
url string Online URL of image to download
localFile string Local filename to save image
Résultat bool
        public static bool DownloadImage(string url, string localFile)
        {
            WebClient webClient = new WebClient();
            webClient.Headers.Add("user-agent", TraktSettings.UserAgent);

            try
            {
                Directory.CreateDirectory(Path.GetDirectoryName(localFile));
                if (!File.Exists(localFile))
                {
                    TraktLogger.Debug("Downloading new image. Url = '{0}', Filename = '{1}'", url, localFile);
                    webClient.DownloadFile(url, localFile);
                }
                return true;
            }
            catch (Exception)
            {
                TraktLogger.Warning("Image download failed. Url = '{0}', Filename = '{1}'", url, localFile);
                try { if (File.Exists(localFile)) File.Delete(localFile); } catch { }
                return false;
            }
        }

Usage Example

        private void DownloadFanart()
        {
            if (Movie == null || Movie.Ids == null || Movie.Ids.Tmdb == null)
            {
                return;
            }

            var getFanartthread = new Thread((o) =>
            {
                var movieImages = TmdbCache.GetMovieImages(Movie.Ids.Tmdb);
                if (movieImages == null)
                {
                    return;
                }

                var movie         = o as TraktMovieSummary;
                string localFile  = TmdbCache.GetMoviePosterFilename(movieImages);
                string remoteFile = TmdbCache.GetMoviePosterUrl(movieImages);

                if (localFile == null || remoteFile == null)
                {
                    return;
                }

                GUIImageHandler.DownloadImage(remoteFile, localFile);
                GUIUtils.SetProperty("#Trakt.Movie.FanartImageFilename", localFile);
            })
            {
                Name = "ImageDownload", IsBackground = true
            };

            getFanartthread.Start(Movie);
        }
All Usage Examples Of TraktPlugin.GUI.GUIImageHandler::DownloadImage