void AddImage(IntPtr himage, PIDL pidl, ListViewItem item) { int imageSize = largeimagelist.ImageSize.Width; Bitmap bmp = null; //if (thumbDB.HasThumbnail(System.IO.Path.GetFileName(pidl.PhysicalPath))) //{ // using (Image img = thumbDB.GetThumbnailImage(System.IO.Path.GetFileName(pidl.PhysicalPath))) // { // largeimagelist.Images.Add(item.Name, Thumbnail.ScaleImage((Bitmap)img, imageSize, imageSize)); // item.ImageIndex = largeimagelist.Images.Count - 1; // return; // } //} try { if (largeimagelist.Images.IndexOfKey(item.Name) < 0) { if (himage == IntPtr.Zero) { switch (imageSize) { case 16: sysimagelist = new SysImageList(SysImageListSize.smallIcons); break; case 48: sysimagelist = new SysImageList(SysImageListSize.largeIcons); break; case 96: sysimagelist = new SysImageList(SysImageListSize.extraLargeIcons); break; case 256: sysimagelist = new SysImageList(SysImageListSize.JumboIcons); break; } int index = sysimagelist.IconIndex(pidl.Pidl); LargeImageList.Images.Add(item.Name, Thumbnail.CreateAlphaBitmap(sysimagelist.Icon(index).ToBitmap(), System.Drawing.Imaging.PixelFormat.Format32bppArgb)); smallimagelist.Images.Add(item.Name, Thumbnail.CreateAlphaBitmap(sysimagelist.Icon(index).ToBitmap(), System.Drawing.Imaging.PixelFormat.Format32bppArgb)); item.ImageIndex = LargeImageList.Images.Count - 1; } else { //bmp = Thumbnail.GetCachedImage(pidl.Pidl, imageSize); //if (bmp == null) { bmp = Thumbnail.GetBitmapFromHBitmap(himage); { smallimagelist.Images.Add(item.Name, Thumbnail.CentreImage(bmp, smallimagelist.ImageSize.Width, smallimagelist.ImageSize.Height)); if ((bmp.Width != imageSize && bmp.Height != imageSize))// && pidl.PhysicalPath != "") { bmp = Thumbnail.CentreImage(bmp, imageSize, imageSize); } else { if (bmp.Width != imageSize || bmp.Height != imageSize) { bmp = Thumbnail.ScaleImage(bmp, imageSize, imageSize); } } } //Thumbnail.SaveThumb(pidl.Pidl, bmp); } largeimagelist.Images.Add(item.Name, bmp); item.ImageIndex = largeimagelist.Images.Count - 1; } return; } item.ImageIndex = largeimagelist.Images.IndexOfKey(item.Name); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (bmp != null) { bmp.Dispose(); } Thumbnail.DeleteObject(himage); } }
void ImageToolTip_Popup(object sender, PopupEventArgs e) { try { Size oldSize = e.ToolTipSize; Filename = Filename.Replace(@"\\", @"\"); if (lastFilename != Filename) { using (PIDL pidl = new PIDL(Filename)) { IntPtr hicon = Thumbnail.GetIcon(pidl.Pidl, 32); using (Bitmap bmp = Thumbnail.GetBitmapFromHBitmap(hicon)) icon = Thumbnail.ScaleImage(bmp, 32, 32); Thumbnail.DeleteObject(hicon); IntPtr himage = Thumbnail.GetThumbNail(pidl.Pidl, ImageSize); using (Bitmap bmp = Thumbnail.GetBitmapFromHBitmap(himage)) { if ((bmp.Width != ImageSize && bmp.Height != ImageSize)) { thumbnail = Thumbnail.CentreImage(bmp, ImageSize, ImageSize); } else if (bmp.Width != ImageSize || bmp.Height != ImageSize) { thumbnail = Thumbnail.ScaleImage(bmp, ImageSize, ImageSize); } else { thumbnail = Thumbnail.GetBitmapFromHBitmap(himage); } //ThumbDB.SaveBitmap(pidl.Pidl, thumbnail); } Thumbnail.DeleteObject(himage); } } string[] texts = Caption.Split(new char[] { '\r', '\n' }); using (Graphics g = Graphics.FromImage(thumbnail)) { foreach (string s in texts) { int width = (int)g.MeasureString(s, e.AssociatedControl.Font).Width + 35; if (width > oldSize.Width) { oldSize.Width = width; } } } if (ImageSize != 16) { if (thumbnail != null) { oldSize.Height += ImageSize + 3; } } oldSize.Width = oldSize.Width; if (oldSize.Width < ImageSize + 18) { oldSize.Width = ImageSize + 18; } e.ToolTipSize = oldSize; lastFilename = Filename; } catch (Exception ex) { } }