private void LoadNewPreviews(List <SearchResult> searchResults, CancellationToken token) { foreach (SearchResult searchResult in searchResults) { if (token.IsCancellationRequested) { return; } DBImage img = searchResult.AttachedImage; _mutex.WaitOne(); // Just precaution measures, not necessary if (!_loadedThumbnails.ContainsKey(img.Id)) { Image thumb = CreateImagePreview(img); if (thumb != null) { _loadedThumbnails.Add(img.Id, thumb); } } _mutex.ReleaseMutex(); } }
private Image CreateImagePreview(DBImage image) { string imagePath = _searcher.GetImageFullPath(image); if (!File.Exists(imagePath)) { return(null); } FileInfo imgFile = new FileInfo(imagePath); if (imgFile.Length > FILE_SIZE_LIMIT) { return(null); } Image img; using (Stream s = File.OpenRead(imgFile.FullName)) { img = Image.FromStream(s); } // Get image thumbnail that stays in the boundaries of a square // of size ROW_HEIGHT float ratio = (float)Math.Max(img.Width, img.Height) / (float)SearchResult.ROW_HEIGHT; int sizeX = Convert.ToInt32((float)img.Width / ratio); int sizeY = Convert.ToInt32((float)img.Height / ratio); Image.GetThumbnailImageAbort callback = new Image.GetThumbnailImageAbort(() => true); Image thumb = img.GetThumbnailImage(sizeX, sizeY, callback, IntPtr.Zero); return(thumb); }