管理

图标工具类 - C#小函数类推荐

Posted on 2024-09-10 09:30  lzhdim  阅读(10146)  评论(0编辑  收藏  举报

       此文记录的是图标操作的工具类。

/***

    图标工具类

    Austin Liu 刘恒辉
    Project Manager and Software Designer

    E-Mail: lzhdim@163.com
    Blog:   http://lzhdim.cnblogs.com
    Date:   2024-01-15 15:18:00

    使用说明:
        1、获取图标;
            IconUtil.GetIconFromDll(filePath, iconIndex);
        2、保存图标;
            IconUtil.SaveIconToFile(icon, filePath);
        3、获取图标列表;
            3.1、获取图标列表;
                private uint _nIcons = 0;

                _nIcons = IconUtil.PrivateExtractIcons(filePath, 0, 0, 0, null, null, 0, 0);
                if (_nIcons == 0)
                {
                    return;
                }

                IntPtr[] phicon = new IntPtr[_nIcons];
                uint[] piconid = new uint[_nIcons];
                uint nIcons = IconUtil.PrivateExtractIcons(filePath, 0, 32, 32, phicon, piconid, _nIcons, 0);

                for (int i = 0; i < nIcons; i++)
                {
                    Icon icon = Icon.FromHandle(phicon[i]);
                    imageList.Images.Add(icon.ToBitmap());
                    icon.Dispose();

                    IconUtil.DestroyIcon(phicon[i]);
                }
            3.2、保存某个图标;
                IntPtr[] phicon = new IntPtr[_nIcons];
                uint[] piconid = new uint[_nIcons];
                uint nIcons = IconUtil.PrivateExtractIcons(_FilePath, 0, 256, 256, phicon, piconid, _nIcons, 0);

                int iIndex = 0;

                if (listView1.SelectedItems.Count > 0)
                {
                    iIndex = int.Parse(listView1.SelectedItems[0].Text);
                }

                IconUtil.SaveIconToFile(Icon.FromHandle(phicon[iIndex]), Path.Combine(folderBrowserDialog.SelectedPath, $"{iIndex}.ico"));

***/

namespace Lzhdim.LPF.Utility
{
    using System;
    using System.Drawing;
    using System.IO;
    using System.Runtime.InteropServices;

    /// <summary>
    /// 图标工具类
    /// </summary>
    public class IconUtil
    {
        #region 使用Win32API获取图标

        [DllImport("User32.dll")]
        public static extern int DestroyIcon(
                    /* _In_ */ IntPtr hIcon);

        [DllImport("User32.dll")]
        public static extern uint PrivateExtractIcons(
                    /* _In_reads_(MAX_PATH) */ string szFileName,
                    /* _In_ */ int nIconIndex,
                    /* _In_ */ int cxIcon,
                    /* _In_ */ int cyIcon,
                    /* _Out_writes_opt_(nIcons) */ IntPtr[] phicon,
                    /* _Out_writes_opt_(nIcons) */ uint[] piconid,
                    /* _In_ */ uint nIcons,
                    /* _In_ */ uint flags);

        #endregion 使用Win32API获取图标

        /// <summary>
        /// 从DLL中提取图标
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="iconIndex">图标索引</param>
        /// <returns>图标文件</returns>
        public static Icon ExtractIconFromFile(string filePath, int iconIndex)
        {
            IntPtr largeIconHandle = Win32API.LoadImage(IntPtr.Zero, filePath, Win32API.IMAGE_ICON, 0, 0, Win32API.LR_LOADFROMFILE | Win32API.LR_DEFAULTSIZE);
            if (largeIconHandle == IntPtr.Zero)
            {
                return null;
            }

            IntPtr iconHandle = Win32API.ImageList_GetIcon(largeIconHandle, iconIndex, Win32API.ILD_TRANSPARENT);
            if (iconHandle == IntPtr.Zero)
            {
                Win32API.DestroyIcon(largeIconHandle);

                return null;
            }

            Icon icon = Icon.FromHandle(iconHandle);
            Win32API.DestroyIcon(largeIconHandle);

            return icon;
        }

        /// <summary>
        /// 保存图标到文件
        /// </summary>
        /// <param name="icon"></param>
        /// <param name="filePath"></param>
        public static void SaveIconToFile(Icon icon, string filePath)
        {
            using (Stream output = File.Create(filePath))
            {
                icon.Save(output);
            }
        }

        /// <summary>
        /// 使用到的Windows API
        /// </summary>
        private static class Win32API
        {
            public const int ILD_TRANSPARENT = 0x00000001;
            public const int IMAGE_ICON = 1;
            public const uint LR_DEFAULTSIZE = 0x00000040;
            public const uint LR_LOADFROMFILE = 0x00000010;

            [DllImport("user32.dll")]
            public static extern bool DestroyIcon(IntPtr hIcon);

            [DllImport("user32.dll")]
            public static extern IntPtr ImageList_GetIcon(IntPtr himl, int i, uint flags);

            [DllImport("user32.dll", CharSet = CharSet.Unicode)]
            public static extern IntPtr LoadImage(IntPtr hInstance, string lpsz, uint nImageType, int cxDesired, int cyDesired, uint fuLoad);
        }
    }
}

 

Copyright © 2000-2022 Lzhdim Technology Software All Rights Reserved