Exemplo n.º 1
0
        public static ImageSource GetIcon(string path, SHIL size)
        {
            IntPtr     result     = IntPtr.Zero;
            SHFILEINFO shFileInfo = new SHFILEINFO();

            if (path.StartsWith(".") || path.StartsWith(@"\\"))
            {
                result = SHGetFileInfo(path, FileAttributes.Normal, ref shFileInfo, Marshal.SizeOf(shFileInfo), SHGFI.SHGFI_SYSICONINDEX | SHGFI.SHGFI_USEFILEATTRIBUTES);
            }
            else
            {
                PIDL pidl = ILCreateFromPath(path);
                result = SHGetFileInfo(pidl, FileAttributes.Normal, ref shFileInfo, Marshal.SizeOf(shFileInfo), SHGFI.SHGFI_SYSICONINDEX | SHGFI.SHGFI_PIDL);
            }

            if (result == IntPtr.Zero)
            {
                return(null);
            }

            SHGetImageList(size, ImageListId, out IImageList iImageList);
            SafeHICON hIcon = iImageList.GetIcon(shFileInfo.iIcon, IMAGELISTDRAWFLAGS.ILD_IMAGE);

            ImageSource imageSource = hIcon.ToBitmapSource();

            imageSource.Freeze();
            DestroyIcon(hIcon);

            return(imageSource);
        }
Exemplo n.º 2
0
        protected virtual Icon GetIcon(SafeResourceId name, Size size)
        {
            var hIcon = new SafeHICON(LoadImage(hLib, name, LoadImageType.IMAGE_ICON, size.Width, size.Height, LoadImageOptions.LR_LOADTRANSPARENT));

            return(!hIcon.IsNull ? hIcon.ToIcon() : throw new Win32Exception());
        }
Exemplo n.º 3
0
        /// <summary>Extracts an icon image from the specified location.</summary>
        /// <param name="exIcon">The <see cref="IExtractIconA"/> instance.</param>
        /// <param name="pszFile">A pointer to a null-terminated string that specifies the icon location.</param>
        /// <param name="nIconIndex">The index of the icon in the file pointed to by pszFile.</param>
        /// <param name="nIconSizeLarge">
        /// The desired size of the large icon, in pixels. The size specified can be the width or height. The width of an icon always equals
        /// its height.
        /// </param>
        /// <param name="phiconLarge">A pointer to an HICON value that receives the handle to the large icon. This parameter may be NULL.</param>
        /// <param name="nIconSizeSmall">
        /// The desired size of the small icon, in pixels. The size specified can be the width or height. The width of an icon always equals
        /// its height.
        /// </param>
        /// <param name="phiconSmall">A pointer to an HICON value that receives the handle to the small icon. This parameter may be NULL.</param>
        /// <returns>Returns S_OK if the function extracted the icon, or S_FALSE if the calling application should extract the icon.</returns>
        public static HRESULT Extract(this IExtractIconA exIcon, string pszFile, uint nIconIndex, ushort nIconSizeLarge, out SafeHICON phiconLarge, ushort nIconSizeSmall, out SafeHICON phiconSmall)
        {
            if (exIcon is null)
            {
                throw new ArgumentNullException(nameof(exIcon));
            }
            var sz = Macros.MAKELONG(nIconSizeLarge, nIconSizeSmall);

            unsafe
            {
                HICON h1 = default, h2 = default;
Exemplo n.º 4
0
        /// <summary>Extracts an icon image from the specified location.</summary>
        /// <param name="exIcon">The <see cref="IExtractIconA"/> instance.</param>
        /// <param name="pszFile">A pointer to a null-terminated string that specifies the icon location.</param>
        /// <param name="nIconIndex">The index of the icon in the file pointed to by pszFile.</param>
        /// <param name="nIconSize">
        /// The desired size of the icon, in pixels. The size specified can be the width or height. The width of an icon always equals
        /// its height.
        /// </param>
        /// <param name="phicon">A pointer to an HICON value that receives the handle to the icon. This parameter may be NULL.</param>
        /// <returns>Returns S_OK if the function extracted the icon, or S_FALSE if the calling application should extract the icon.</returns>
        public static HRESULT Extract(this IExtractIconA exIcon, string pszFile, uint nIconIndex, ushort nIconSize, out SafeHICON phicon)
        {
            if (exIcon is null)
            {
                throw new ArgumentNullException(nameof(exIcon));
            }
            var sz = nIconSize > 16 ? Macros.MAKELONG(nIconSize, 0) : Macros.MAKELONG(0, nIconSize);

            unsafe
            {
                HICON h1 = default;
                var   hr = nIconSize > 16 ? exIcon.Extract(pszFile, nIconIndex, &h1, null, sz) : exIcon.Extract(pszFile, nIconIndex, null, &h1, sz);
                phicon = h1 == default ? null : new SafeHICON((IntPtr)h1);
                return(hr);
            }
        }
Exemplo n.º 5
0
            /// <summary>Loads an icon from the system image list.</summary>
            /// <param name="iIdx">A value of type int that contains the index of the image.</param>
            /// <param name="imgSz">The width, in pixels, of the icon.</param>
            /// <param name="hico">The resulting icon handle, on success, or <c>null</c> on failure.</param>
            /// <returns>The result of function.</returns>
            public static HRESULT LoadIconFromSystemImageList(int iIdx, ref uint imgSz, out SafeHICON hico)
            {
                HRESULT hr;

                if ((hr = SHGetImageList(PixelsToSHIL((int)imgSz), typeof(IImageList).GUID, out object obj)).Succeeded)
                {
                    try
                    {
                        IImageList il = (IImageList)obj;
                        hico = il.GetIcon(iIdx, IMAGELISTDRAWFLAGS.ILD_TRANSPARENT);
                        using ICONINFO icoInfo = new();
                        if (GetIconInfo(hico, icoInfo))
                        {
                            imgSz = (uint)GetObject <BITMAP>(icoInfo.hbmColor).bmWidth;
                        }
                    }
                    catch (COMException e)
                    {
                        hico = null;
                        return(e.ErrorCode);
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(obj);
                    }
                }
Exemplo n.º 6
0
            /// <summary>Gets the icon for the item using the specified characteristics.</summary>
            /// <param name="iei">The IExtractIconA from which to retrieve the icon.</param>
            /// <param name="imgSz">The width, in pixels, of the icon.</param>
            /// <param name="hico">The resulting icon handle, on success, or <c>null</c> on failure.</param>
            /// <returns>The result of function.</returns>
            public static HRESULT LoadIconFromExtractIcon(IExtractIconA iei, ref uint imgSz, out SafeHICON hico)
            {
                StringBuilder szIconFile = new(Kernel32.MAX_PATH);
                HRESULT       hr         = iei.GetIconLocation(GetIconLocationFlags.GIL_FORSHELL, szIconFile, szIconFile.Capacity, out int iIdx, out _);

                if (hr.Succeeded)
                {
                    if (szIconFile.ToString() != "*")
                    {
                        hr = iei.Extract(szIconFile.ToString(), (uint)iIdx, (ushort)imgSz, out hico);
                    }
                    else
                    {
                        hr = LoadIconFromSystemImageList(iIdx, ref imgSz, out hico);
                    }
                }
                else
                {
                    hico = null;
                }

                return(hr);
            }
Exemplo n.º 7
0
            /// <summary>Gets the icon for the item using the specified characteristics.</summary>
            /// <param name="psf">The IShellFolder from which to request the IExtractIcon instance.</param>
            /// <param name="pidl">The PIDL of the item within <paramref name="psf"/>.</param>
            /// <param name="imgSz">The width, in pixels, of the icon.</param>
            /// <param name="hico">The resulting icon handle, on success, or <c>null</c> on failure.</param>
            /// <returns>The result of function.</returns>
            public static HRESULT LoadIconFromExtractIcon(IShellFolder psf, PIDL pidl, ref uint imgSz, out SafeHICON hico)
            {
                hico = default;
                HRESULT hr = psf.GetUIObjectOf((IntPtr)pidl, out IExtractIconW ieiw);

                if (hr.Succeeded)
                {
                    try
                    {
                        return(LoadIconFromExtractIcon(ieiw, ref imgSz, out hico));
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(ieiw);
                    }
                }
                else if ((hr = psf.GetUIObjectOf((IntPtr)pidl, out IExtractIconA iei)).Succeeded)
                {
                    try
                    {
                        return(LoadIconFromExtractIcon(iei, ref imgSz, out hico));
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(iei);
                    }
                }
                return(hr);
            }
Exemplo n.º 8
0
 /// <summary>Creates a managed <see cref="BitmapSource"/> from a SafeHICON instance.</summary>
 /// <returns>A managed bitmap instance.</returns>
 public static BitmapSource ToBitmapSource(this SafeHICON hIcon) => hIcon is null || hIcon.IsInvalid ? null : CreateBitmapSourceFromHIcon(hIcon.DangerousGetHandle(), default, BitmapSizeOptions.FromEmptyOptions());