• WPF 获取指定文件的Icon


    C#
               var icon = System.Drawing.Icon.ExtractAssociatedIcon(@"filepath");
                var m = new MemoryStream();
                icon.Save(m);
                var bitmapiamge = new BitmapImage();
                bitmapiamge.BeginInit();
                bitmapiamge.StreamSource = m;
                bitmapiamge.EndInit();
                this.Icon = bitmapiamge;

    shell32.dll

    public class IconExt
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct SHFILEINFO
        {
            public IntPtr hIcon;
            public IntPtr iIcon;
            public uint dwAttributes;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szDisplayName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
            public string szTypeName;
        }
    
        /// <summary>  
        /// 返回系统设置的图标  
        /// </summary>  
        /// <param name="pszPath">文件路径 如果为""  返回文件夹的</param>  
        /// <param name="dwFileAttributes">0</param>  
        /// <param name="psfi">结构体</param>  
        /// <param name="cbSizeFileInfo">结构体大小</param>  
        /// <param name="uFlags">枚举类型</param>  
        /// <returns>-1失败</returns>  
        [DllImport("shell32.dll")]
        public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref   SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
    
        public enum SHGFI
        {
            SHGFI_ICON = 0x100,
            SHGFI_LARGEICON = 0x0,
            SHGFI_USEFILEATTRIBUTES = 0x10
        }
    
    
        /// <summary>  
        /// 获取文件图标 
        /// </summary>  
        /// <param name="p_Path">文件全路径</param>  
        /// <returns>图标</returns>  
        public static System.Drawing.Icon GetFileIcon(string p_Path)
        {
            SHFILEINFO _SHFILEINFO = new SHFILEINFO();
            IntPtr _IconIntPtr = SHGetFileInfo(p_Path, 0, ref _SHFILEINFO, (uint)Marshal.SizeOf(_SHFILEINFO), (uint)(SHGFI.SHGFI_ICON | SHGFI.SHGFI_LARGEICON | SHGFI.SHGFI_USEFILEATTRIBUTES));
            if (_IconIntPtr.Equals(IntPtr.Zero)) return null;
            System.Drawing.Icon _Icon = System.Drawing.Icon.FromHandle(_SHFILEINFO.hIcon);
            return _Icon;
        }
        /// <summary>  
        /// 获取文件夹图标 
        /// </summary>  
        /// <returns>图标</returns>  
        public static System.Drawing.Icon GetDirectoryIcon()
        {
            SHFILEINFO _SHFILEINFO = new SHFILEINFO();
            IntPtr _IconIntPtr = SHGetFileInfo(@"", 0, ref _SHFILEINFO, (uint)Marshal.SizeOf(_SHFILEINFO), (uint)(SHGFI.SHGFI_ICON | SHGFI.SHGFI_LARGEICON));
            if (_IconIntPtr.Equals(IntPtr.Zero)) return null;
            System.Drawing.Icon _Icon = System.Drawing.Icon.FromHandle(_SHFILEINFO.hIcon);
            return _Icon;
        }
    }

    shell32.dll 代码来自:http://www.cnblogs.com/jxsoft/archive/2011/06/22/2087440.html

    网友的文章:http://blog.csdn.net/soft2buy/article/details/6979351

  • 相关阅读:
    Docker 实战(2)- 在 Jenkins 容器上的搭建持续集成环境和完成自动化测试
    Docker
    Jenkins(9)- 汉化 Jenkins 网站
    Docker 实战(1)- 使用 Jenkins 镜像创建容器,并搭建 Python + Pytest +Allure 的自动化测试环境
    Python
    Docker
    Linux
    Docker(20)- docker rmi 命令详解
    Docker(19)- docker pause/unpause 命令详解
    Docker(18)- docker kill 命令详解
  • 原文地址:https://www.cnblogs.com/gaobing/p/4267840.html
Copyright © 2020-2023  润新知