private ThumbCache.ThumbInfo GetThumbInfo(int index) { if (lruIndices.Contains(index)) { lruIndices.Remove(index); } lruIndices.Add(index); if (lruIndices.Count > MAX_LRU_SIZE) { int indexToEvict = lruIndices[0]; lruIndices.RemoveAt(0); if (thumbInfoMap.ContainsKey(indexToEvict)) { thumbInfoMap[indexToEvict].Dispose(); thumbInfoMap.Remove(indexToEvict); } } if (!thumbInfoMap.ContainsKey(index)) { var item = db.GetImage(index, true); if (item != null) { thumbInfoMap.Add(index, item); } return(item); } else { return(thumbInfoMap[index]); } }
private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e) { //e.DrawBackground(); e.DrawDefault = true; //e.DrawText(); Image img = null; try { img = cache.GetImage(e.ItemIndex, true).image; } catch { } if (img == null) { return; } float aspect = (float)img.Width / (float)img.Height; if (aspect == 0f) { aspect = 1f; } Rectangle src = new Rectangle(0, 0, img.Width, img.Height); Rectangle dst; if (aspect >= 1f) { // width >= height if (img.Width > e.Bounds.Width) { dst = new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, (int)(e.Bounds.Height / aspect)); } else { dst = new Rectangle(e.Bounds.Left + e.Bounds.Width / 2 - img.Width / 2, e.Bounds.Top + e.Bounds.Height / 2 - img.Height / 2, img.Width, img.Height); } } else { // height > width if (img.Height > e.Bounds.Height) { dst = new Rectangle(e.Bounds.Left, e.Bounds.Top, (int)(e.Bounds.Width * aspect), e.Bounds.Height); } else { dst = new Rectangle(e.Bounds.Left + e.Bounds.Width / 2 - img.Width / 2, e.Bounds.Top + e.Bounds.Height / 2 - img.Height / 2, img.Width, img.Height); } } e.Graphics.DrawImage(img, dst, src, GraphicsUnit.Pixel); }
private void OpenFile(string fileName) { try { cache = new ThumbCache(fileName); listViewEntries.LargeImageList = null; int w = 120; for (int i = 0; i < 5; i++) { try { var img = cache.GetImage(i, true); w = img.image.Width; if (img.image.Height > w) { w = img.image.Height; } } catch { } } if (w < 32) { w = 32; } if (w > 240) { w = 240; } w += 16; imageList1.ImageSize = new Size(w, w); listViewEntries.LargeImageList = imageList1; listViewEntries.VirtualListSize = cache.ImageCount; listViewEntries.Invalidate(); this.Text = Application.ProductName + " - " + fileName; } catch (Exception ex) { MessageBox.Show(this, ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }