Beispiel #1
0
        public Bitmap LoadImageFromExtractIcon(string filename)
        {
            if (!System.IO.Directory.Exists(filename) && !System.IO.File.Exists(filename))
            {
                return(null);
            }

            Bitmap bm1     = null;
            IntPtr hbitmap = IntPtr.Zero;

            IShellFolder desktop     = null;
            IntPtr       pidlParent  = IntPtr.Zero;
            IShellFolder shellFolder = null;
            IUnknown     iunk        = null;
            IExtractIcon extractIcon = null;

            try
            {
                Debug.Write("Trying IExtractIcon\r\n");

                Shell32.SHGetDesktopFolder(ref desktop);

                int    cParsed   = 0;
                int    pdwAttrib = 0;
                string filePath  = filename;
                if (System.IO.File.Exists(filename))
                {
                    filePath = System.IO.Path.GetDirectoryName(filename);
                }
                desktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, filePath, out cParsed, out pidlParent, out pdwAttrib);

                Guid uuidShellFolder = new Guid("000214E6-0000-0000-C000-000000000046");
                desktop.BindToObject(pidlParent, IntPtr.Zero, ref uuidShellFolder, out shellFolder);

                Guid uuidExtractIcon = new Guid("000214eb-0000-0000-c000-000000000046");
                iunk = GetUIObject(shellFolder, filename, uuidExtractIcon);

                if (iunk == null)
                {
                    Debug.Write("    IExtractIcon not supported\r\n");
                    return(null);
                }

                int hres = 0;
                extractIcon = (IExtractIcon)iunk;
                StringBuilder location  = new StringBuilder(260, 260);
                int           iconIndex = 0;
                GILFLAGS2     flags     = GILFLAGS2.GIL_None;
                hres = extractIcon.GetIconLocation(0, location, location.Capacity, out iconIndex, out flags);
                IntPtr iconLarge = IntPtr.Zero;
                IntPtr iconSmall = IntPtr.Zero;
                uint   iconSize  = (16 | (48 << 16));
                hres = extractIcon.Extract(location, iconIndex, out iconLarge, out iconSmall, iconSize);
                Icon ico = Icon.FromHandle(iconLarge);
                bm1 = ico.ToBitmap();
            }
            catch (Exception e)
            {
                Debug.Write("    Exception extracting image from IExtractIcon: " + e.Message + "\r\n");
            }
            finally
            {
                if (extractIcon != null)
                {
                    Marshal.ReleaseComObject(extractIcon);
                    extractIcon = null;
                }
                if (iunk != null)
                {
                    Marshal.ReleaseComObject(iunk);
                    iunk = null;
                }
                if (shellFolder != null)
                {
                    Marshal.ReleaseComObject(shellFolder);
                    shellFolder = null;
                }
                if (pidlParent != IntPtr.Zero)
                {
                    Allocator.Free(pidlParent);
                    pidlParent = IntPtr.Zero;
                }
                if (desktop != null)
                {
                    Marshal.ReleaseComObject(desktop);
                    desktop = null;
                }
                if (_allocator != null)
                {
                    Marshal.ReleaseComObject(_allocator);
                    _allocator = null;
                }
            }
            return(bm1);
        }
Beispiel #2
0
    //==========================================================
    //renvoie la miniature (ou à défaut l'icône) du fichier szFileName
    //=========================================================
    //IN szFileName : nom du fichier dont on veut la miniature
    //IN dwCX : largeur de la miniature
    //IN dwCY : hauteur de la miniature
    //IN allowIcon: some videos don't have a true thumbnail.
    //              if true, it will return the icon of the default program used to open this kind of file
    //              (the icon of VLC for example)
    //renvoie une instance d'image VB IPictureDisp
    //==========================================================
    private Bitmap ExtractImage(string szFileName, int dwCX, int dwCY, bool allowIcon = true)
    {
        int           priority = 0;
        int           requestedColourDepth;
        int           flags;
        SIZE          sz;
        IntPtr        pidl = IntPtr.Zero;
        IShellFolder  isf  = null;
        IExtractImage ie   = null;
        IExtractIcon  ii   = null;
        StringBuilder szPath;
        int           pindex     = 0;
        IntPtr        pIconLarge = IntPtr.Zero;
        IntPtr        pIconSmall = IntPtr.Zero;
        SHFILEINFO    shgfi      = new SHFILEINFO();
        Bitmap        ret        = null;

        requestedColourDepth = 32;
        flags = (int)(EIEIFLAG.IEIFLAG_ASPECT | EIEIFLAG.IEIFLAG_OFFLINE | EIEIFLAG.IEIFLAG_SCREEN);

        //on récupère le nom de fichier sous forme ITEMLIST (pidl) et le dossier parent sous forme IShellFolder
        isf = GetShellFolder(szFileName, ref pidl);

        // Try to get the thumbnail
        ie = getThumbnail(isf, pidl);

        // If not possible
        if (ie == null)
        {
            // The icon
            ii = getIcon(isf, pidl);

            // If possible
            if (!(ii == null))
            {
                // Extract the icon
                szPath = new StringBuilder(260);

                ii.GetIconLocation(0, szPath, 260, ref pindex, ref flags);
                ii.Extract(szPath.ToString(), pindex, ref pIconLarge, ref pIconSmall, dwCX + 65536 * dwCX);

                if (pIconLarge == IntPtr.Zero)
                {
                    SHGetFileInfo(szFileName, 0, ref shgfi, Marshal.SizeOf(shgfi), SHGFIFLAGS.SHGFI_LARGEICON | SHGFIFLAGS.SHGFI_ICON | SHGFIFLAGS.SHGFI_OVERLAYINDEX);
                    ret = Icon.FromHandle(shgfi.hIcon).ToBitmap();
                }
                else
                {
                    ret = Icon.FromHandle(pIconLarge).ToBitmap();
                }
            }
            else
            {
                SHGetFileInfo(szFileName, 0, ref shgfi, Marshal.SizeOf(shgfi), SHGFIFLAGS.SHGFI_LARGEICON | SHGFIFLAGS.SHGFI_ICON | SHGFIFLAGS.SHGFI_OVERLAYINDEX);
                ret = Icon.FromHandle(shgfi.hIcon).ToBitmap();
            }
        }
        else             // If possible
                         // Extract the thumbnail with the wanted dimensions
        {
            sz.cx = dwCX;
            sz.cy = dwCY;

            szPath = new StringBuilder(260);
            EIEIFLAG f = (EIEIFLAG)flags;
            int      r = ie.GetLocation(szPath, szPath.Capacity, out priority, ref sz, requestedColourDepth, ref f);

            try {
                pIconLarge = ie.Extract();
            } catch { }

            // If not possible
            if (pIconLarge == IntPtr.Zero)
            {
                // Retry to get the icon
                ii = getIcon(isf, pidl);

                // If possible
                if (!(ii == null))
                {
                    szPath = new StringBuilder(260);

                    ii.GetIconLocation(0, szPath, 260, ref pindex, ref flags);
                    ii.Extract(szPath.ToString(), pindex, ref pIconLarge, ref pIconSmall, dwCX + 65536 * dwCX);

                    if (pIconLarge == IntPtr.Zero)
                    {
                        SHGetFileInfo(szFileName, 0, ref shgfi, Marshal.SizeOf(shgfi), SHGFIFLAGS.SHGFI_LARGEICON | SHGFIFLAGS.SHGFI_ICON);
                        ret = Icon.FromHandle(shgfi.hIcon).ToBitmap();
                    }
                    else
                    {
                        if (!allowIcon)
                        {
                            return(null);
                        }

                        ret = Icon.FromHandle(pIconLarge).ToBitmap();
                    }
                }
                else
                {
                    SHGetFileInfo(szFileName, 0, ref shgfi, Marshal.SizeOf(shgfi), SHGFIFLAGS.SHGFI_LARGEICON | SHGFIFLAGS.SHGFI_ICON);
                    ret = Icon.FromHandle(shgfi.hIcon).ToBitmap();
                }
            }
            else
            {
                ret = (Bitmap)Bitmap.FromHbitmap(pIconLarge);
            }
        }

        ILFree(pidl);

        if (ret == null)
        {
            ret.MakeTransparent();
        }

        if (pIconLarge != IntPtr.Zero)
        {
            DeleteObject(pIconLarge);
        }
        if (pIconSmall != IntPtr.Zero)
        {
            DeleteObject(pIconSmall);
        }

        if (isf != null)
        {
            Marshal.ReleaseComObject(isf);
        }
        if (ie != null)
        {
            Marshal.ReleaseComObject(ie);
        }
        if (ii != null)
        {
            Marshal.ReleaseComObject(ii);
        }

        return(ret);
    }