将其加载到C#中的ImageList后销毁图像

时间:2016-01-11 15:53:49

标签: c# image

我搜索了有关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.我的视觉工作室崩溃了,当我在dev.exe收到崩溃报告后尝试调试时,它到了我试图复制到{{{ 1}} ListView

我尝试过调用SmallImageList/LargeImgaeListb.Dispose()进行处置。但是当我这样做时,我发现图标丢失了。我本可以使用icon.Dispose()返回Bitmap,但我选择使用System.Drawing.Icon.ToBitmap()

0 个答案:

没有答案