我搜索了有关ImageLists的stackoverflow,我对答案/回复感到困惑.我有以下代码,它的作用是什么; 基于它抓取的文件扩展名Icon
.因此,当我添加一个文件说Hello.pdf时,我会在列表视图中显示该文件,其.pdf
图标类似于Windows资源管理器.
private void bwUpdateSmallImageList_DoWork(object sender, DoWorkEventArgs e) { lock (smImageLock) { foreach (String ext in _Extension) { if (!_SmallImageLists.Images.ContainsKey(ext)) // .jpg/.pdf/.doc { IntPtr handle; IconSize iSize = IconSize.Small; Icon icon = null; try { icon = FileSystemHelper.IconFromFileExtension(ext, iSize, out handle); if (icon != null) { Image b = icon.ToBitmap() as Image; if (b != null) { _SmallImageLists.Images.Add(ext, b); //b.Dispose(); ****1 call dispose ? //icon.Dispose(); ****2 call dispose ? } } DestroyIcon(handle); // this one is being called //[DllImport("user32.dll")] //private static extern bool DestroyIcon(IntPtr hIcon); } catch { }//(TargetInvocationException ex)// we dont want to throw exception from a background worker // if it can not grab the icon just leave it blank } } } }
在我添加到图像列表后,我应该处理/销毁Image
变量b
以及Icon
变量icon
.
在我添加了图标/图像后,我实际上找不到合适的答案,ImageList
是复制到图像列表自己的内存空间还是保留图像/图标的内存地址?FileSystemHelper.IconFromFileExtension
功能如下;
public static Icon IconFromFileExtension(string fileExtension, IconSize iconSize, out IntPtr handle) { //Icon icon = null; handle = IntPtr.Zero; try { if (fileExtension.StartsWith(".") == false) fileExtension = "." + fileExtension; SHFILEINFO shFileInfo = new SHFILEINFO(); SHGetFileInfo(fileExtension, 0, ref shFileInfo, (uint)Marshal.SizeOf(shFileInfo), SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | (uint)iconSize); handle = shFileInfo.hIcon; //icon = Icon.FromHandle(shFileInfo.hIcon); //DestroyIcon(handle);// this is wrong, icon needs the handle, can't destroy return Icon.FromHandle(shFileInfo.hIcon); //return icon; } catch(Exception) { //return LanganComponent.Properties.Resources.unknown; return null; } }
我问,因为我得到Creation of the ImageList handle did not succeed.
我的Visual Studio的崩溃,当我得到崩溃报告后尝试调试dev.exe
,它得到的地方,我试图复制到一个点ListView
的SmallImageList/LargeImgaeList
.
我试过通过调用b.Dispose()
和处理icon.Dispose()
.但是当我这样做时,我发现图标丢失了.我本可以回来Bitmap
使用System.Drawing.Icon.ToBitmap()
,但我选择使用Icon
.