原文:C# 获取系统Icon、获取文件相关的Icon

1、获取系统Icon工具下载SystemIcon.exe

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace FileExplorer
{
/// <summary>
/// 系统Icon
/// 1、Get() 获取指定索引对应的系统icon
/// 2、Save() 保存所有系统图像
/// 3、Show() 显示所有系统Icon图像
/// </summary>
public partial class SystemIcon : Form
{
public SystemIcon()
{
InitializeComponent(); Show(this);
Save();
} /// <summary>
/// 在form上显示所有系统icon图像
/// </summary>
public static void Show(Form form)
{
LoadSystemIcon(); FlowLayoutPanel flowLayout = new FlowLayoutPanel();
flowLayout.Dock = System.Windows.Forms.DockStyle.Fill;
flowLayout.AutoScroll = true; for (int i = 0; i < SystemIconList.Count; i++)
{
PictureBox pic = new PictureBox();
pic.Size = new System.Drawing.Size(32, 32);
flowLayout.Controls.Add(pic); Bitmap p = SystemIconList[i].ToBitmap();
pic.Image = p;
}
form.Controls.Add(flowLayout);
} /// <summary>
/// 保存所有系统图像
/// </summary>
public static void Save()
{
LoadSystemIcon(); for (int i = 0; i < SystemIconList.Count; i++)
{
Bitmap p = SystemIconList[i].ToBitmap(); // 保存图像
string path = AppDomain.CurrentDomain.BaseDirectory + "系统图标\\";
string filepath = path + (i + ".png");
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
if (!File.Exists(filepath)) p.Save(filepath);
}
} /// <summary>
/// 获取指定索引对应的系统icon
/// </summary>
public static Icon Get(int index)
{
LoadSystemIcon();
return index < SystemIconList.Count ? SystemIconList[index] : null;
} private static List<Icon> SystemIconList = new List<Icon>(); // 记录系统图标 //[DllImport("user32.dll", CharSet = CharSet.Auto)]
//private static extern bool MessageBeep(uint type); [DllImport("Shell32.dll")]
public extern static int ExtractIconEx(string libName, int iconIndex, IntPtr[] largeIcon, IntPtr[] smallIcon, int nIcons); private static IntPtr[] largeIcon;
private static IntPtr[] smallIcon; /// <summary>
/// 获取所有系统icon图像
/// </summary>
private static void LoadSystemIcon()
{
if (SystemIconList.Count > 0) return; largeIcon = new IntPtr[1000];
smallIcon = new IntPtr[1000]; ExtractIconEx("shell32.dll", 0, largeIcon, smallIcon, 1000); SystemIconList.Clear();
for (int i = 0; i < largeIcon.Length; i++)
{
try
{
Icon ic = Icon.FromHandle(largeIcon[i]);
SystemIconList.Add(ic);
}
catch (Exception ex)
{
break;
}
}
} //private void LoadSystemIcon()
//{
// largeIcon = new IntPtr[1000];
// smallIcon = new IntPtr[1000]; // ExtractIconEx("shell32.dll", 0, largeIcon, smallIcon, 1000); // FlowLayoutPanel flowLayout = new FlowLayoutPanel();
// flowLayout.Dock = System.Windows.Forms.DockStyle.Fill;
// flowLayout.AutoScroll = true; // for (int i = 0; i < largeIcon.Length; i++)
// {
// try
// {
// PictureBox pic = new PictureBox();
// pic.Size = new System.Drawing.Size(32, 32);
// flowLayout.Controls.Add(pic); // Icon ic = Icon.FromHandle(largeIcon[i]);
// SystemIcon.Add(ic); // Bitmap p = ic.ToBitmap();
// pic.Image = p; // // 保存图像
// string path = AppDomain.CurrentDomain.BaseDirectory + "系统图标\\";
// string filepath = path + (i + ".png");
// if (!Directory.Exists(path)) Directory.CreateDirectory(path);
// if (!File.Exists(filepath)) p.Save(filepath);
// }
// catch (Exception ex)
// {
// break;
// }
// }
// this.Controls.Add(flowLayout);
//}
}
}

  

2、获取文件相关的Icon

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using Microsoft.Win32; namespace FileExplorer
{
/// <summary>
/// 获取指定文件的Icon图像getIcon()、getIcon2()
/// </summary>
class FileIcon
{
private const uint SHGFI_ICON = 0x100;
private const uint SHGFI_LARGEICON = 0x0; //大图标
private const uint SHGFI_SMALLICON = 0x1; //小图标 [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; //文件的类型名
}; [DllImport("shell32.dll")]
private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags); /// <summary>
/// 获取文件FilePath对应的Icon
/// </summary>
public static Icon getIcon(string FilePath)
{
SHFILEINFO shinfo = new SHFILEINFO();
//FileInfo info = new FileInfo(FileName); //大图标
SHGetFileInfo(FilePath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_LARGEICON);
Icon largeIcon = Icon.FromHandle(shinfo.hIcon); //Icon.ExtractAssociatedIcon(FileName);
return largeIcon;
} /// <summary>
/// 获取文件FilePath对应的Icon
/// </summary>
public static Icon getIcon2(string FilePath)
{
return Icon.ExtractAssociatedIcon(FilePath);
}
}
}

  

最新文章

  1. 公司内多个公众号实现账号互通(UnionID机制处理)
  2. 经典.net试题
  3. Git subtree和Git submodule
  4. 解决从内部存储设备安装apk提示Permission Denied
  5. DOS 全集
  6. DKNightVersion的基本使用(夜间模式)
  7. ES5 object的新函数
  8. Android——与查询联系人相关的3张表
  9. div中显示某个网页
  10. jeecg入门操作—树型表单开发
  11. CentOS 6.5 minimal 安装配置VMware tools
  12. Python 获取计算机全名(fully qualified host name)
  13. Ubuntu postgres 内网 安装 卸载
  14. 基于 Keras 的 LSTM 时间序列分析——以苹果股价预测为例
  15. jQuery File Upload 判断图片尺寸,限定图片宽高的办法
  16. js跳转页面方法实现汇总
  17. DevExpress XtraScheduler日程管理控件应用实例(2)-- 深入理解数据存储
  18. Oracle透明网关访问SQLServer数据库
  19. “吃神么,买神么”的第一个Sprint计划(第五天)
  20. 【12c OCP】最新CUUG OCP-071考试题库(50题)

热门文章

  1. Vue源码--深入模板渲染
  2. shell脚本if条件总结
  3. ORACLE会话数、连接数配置
  4. ios开发日期的NSDate,NSCalendar分类
  5. bash - trap
  6. HBase总结(十一)hbase Java API 介绍及使用演示样例
  7. js进阶 11-7 jquery如何获取和改变元素的位置
  8. linux 登录windows跳板机
  9. [React] Use Jest&#39;s Snapshot Testing Feature
  10. TensorFlow 学习(十四)—— contrib