public List <VideoInfo> BuildImages(List <VideoInfo> videoInfos, DoWorkEventArgs doWorkEvent) { if (BuildGalleryImages.ThumbnailsAlreadySet()) { return(videoInfos); } // Abort the operation if the user has canceled. // Note that a call to CancelAsync may have set // CancellationPending to true just after the // last invocation of this method exits, so this // code will not have the opportunity to set the // DoWorkEventArgs.Cancel flag to true. This means // that RunWorkerCompletedEventArgs.Cancelled will // not be set to true in your RunWorkerCompleted // event handler. This is a race condition. if (backgroundWorker.CancellationPending) { doWorkEvent.Cancel = true; return(null); } int nbrVideoInfos = videoInfos.Count(); int nbrPosters = 0; foreach (VideoInfo videoInfo in videoInfos) { if (videoInfo.filter) { if (videoInfo.files.posterThumbnail != null) { videoInfo.files.posterThumbnail.Dispose(); } continue; } // no files or no video, so skip if (videoInfo.files == null || videoInfo.files.video == null) { continue; } GalleryImageThumbnailInfo galleryImageThumbnailInfo = BuildGalleryImages.GetCachedThumbnail(videoInfo); videoInfo.files.posterThumbnail = galleryImageThumbnailInfo.thumbnail; nbrPosters++; // Thread.Sleep(1000); // dev // meh, smooth out update progress if (nbrPosters % 10 == 0) { percentComplete = 100 - (int)Math.Floor((decimal)(nbrVideoInfos - nbrPosters) / nbrVideoInfos * 100); string progressMessage = ""; if (galleryImageThumbnailInfo.fromCache) { progressMessage += "Using Gallery cache"; } else if (galleryImageThumbnailInfo.createdCache) { progressMessage += "Creating Gallery cache"; } else { progressMessage += "Building Gallery"; } progressMessage += ".."; backgroundWorker.ReportProgress(percentComplete, progressMessage); if (backgroundWorker.CancellationPending) { doWorkEvent.Cancel = true; return(null); } } } percentComplete = 100; backgroundWorker.ReportProgress(percentComplete, "Completed Gallery"); // meh, so completed shows Thread.Sleep(500); return(videoInfos); }
public static GalleryImageThumbnailInfo GetCachedThumbnail(VideoInfo videoInfo) { GalleryImageThumbnailInfo galleryImageThumbnailInfo = new GalleryImageThumbnailInfo(); galleryImageThumbnailInfo.fromCache = false; galleryImageThumbnailInfo.createdCache = false; galleryImageThumbnailInfo.thumbnail = null; Image thumbnail = null; string cachedThumbnailFullName = MyFile.EnsureDataFile("poster_" + videoInfo.hash, "jpg", @"cache\gallery"); if (Config.settings.gallery.cachePosterThumbnails && File.Exists(cachedThumbnailFullName)) { // use file stream so files not locked FileStream fileStream = new FileStream(cachedThumbnailFullName, FileMode.Open, FileAccess.Read); thumbnail = Image.FromStream(fileStream); fileStream.Close(); // thumbnail = Image.FromFile(cachedThumbnailFile); // locks file galleryImageThumbnailInfo.fromCache = true; } else if (videoInfo.files.poster == null) { thumbnail = null; } else { VideoItemFile videoItemFile = videoInfo.files.poster; string posterFullName = videoInfo.GetFullName(videoItemFile); // skip images smaller than y or larger than x if (videoItemFile.Length < 100) { MyLog.Add("Get Thumbnail: Skipping " + posterFullName + " " + MyFile.FormatSize(videoItemFile.Length)); thumbnail = null; } if (videoItemFile.Length > 5 * 1048576) { MyLog.Add("Get Thumbnail: Skipping " + posterFullName + " " + MyFile.FormatSize(videoItemFile.Length)); thumbnail = null; } try { if (!File.Exists(posterFullName)) { thumbnail = null; } else { // size of thumbails .. TODO make a config setting thumbnail = MyImage.GetThumbnail(posterFullName, 165, 250, Color.Black); if (Config.settings.gallery.cachePosterThumbnails && thumbnail != null) { // clone image so files not locked if (MyImage.SaveJpgImage((Image)thumbnail.Clone(), cachedThumbnailFullName)) { galleryImageThumbnailInfo.createdCache = true; } } } } catch (OutOfMemoryException) { // Image.FromFile will throw this if file is invalid/corrupted; Don't ask me why MyLog.Add("Invalid Image; Unable to read " + posterFullName + " " + MyFile.FormatSize(videoItemFile.Length)); thumbnail = null; } catch (Exception ei) { MyLog.Add(ei.ToString()); thumbnail = null; } } galleryImageThumbnailInfo.thumbnail = thumbnail; return(galleryImageThumbnailInfo); }