namespace Helpers
{
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Win32;
using System.Windows.Interop;
public static class GetSystemIcon
{
#region 中间处理过程
[DllImport("shell32.dll")]
static extern uint ExtractIconEx(string lpszFile, int nIconIndex, int[] phiconLarge, int[] phiconSmall, uint nIcons);
static IntPtr ReturnByFileName(string fileName, bool isLarge, int iconIndex = 0)
{
var phiconLarge = new int[1];
var phiconSmall = new int[1];
ExtractIconEx(fileName, iconIndex, phiconLarge, phiconSmall, 1);
return new IntPtr(isLarge ? phiconLarge[0] : phiconSmall[0]);
}
static dynamic ReturnByFileType(string fileType)
{
if (string.IsNullOrEmpty(fileType)) return null;
string regIconString = null;
if (fileType[0] == '.')
{
//读系统注册表中文件类型信息
var regVersion = Registry.ClassesRoot.OpenSubKey(fileType, false);
if (regVersion != null)
{
var regFileType = regVersion.GetValue("") as string;
regVersion.Close();
regVersion = Registry.ClassesRoot.OpenSubKey(regFileType + @"\DefaultIcon", false);
if (regVersion != null)
{
regIconString = regVersion.GetValue("") as string;
regVersion.Close();
}
}
if (regIconString == null)
{
//没有读取到文件类型注册信息,指定为未知文件类型的图标
regIconString = $@"{Environment.SystemDirectory}\shell32.dll,{0}";
}
}
else
{
//直接指定为文件夹图标
regIconString = $@"{Environment.SystemDirectory}\shell32.dll,{3}";
}
var fileIcon = regIconString.Split(',');
if (fileIcon.Length != 2)
{
//系统注册表中注册的标图不能直接提取,则返回可执行文件的通用图标
fileIcon = new[] { $@"{Environment.SystemDirectory}\shell32.dll", "2" };
}
var shell32Path = fileIcon[0];
var iconIndex = int.Parse(fileIcon[1]);
return new { shell32Path, iconIndex };
}
#endregion
/// <summary>
/// 依据文件名读取图标,若指定文件不存在,则返回空值。
/// </summary>
/// <param name="fileName">文件路径</param>
/// <param name="isLarge">是否返回大图标</param>
/// <returns></returns>
public static Icon GetIconByFileName(string fileName, bool isLarge = true)
{
var intPtr = ReturnByFileName(fileName, isLarge);
return intPtr.ToString() == "0" ? null : Icon.FromHandle(intPtr);
}
/// <summary>
/// 依据文件名读取图像,若指定文件不存在,则返回空值。
/// </summary>
/// <param name="fileName">文件路径</param>
/// <param name="isLarge">是否返回大图标</param>
/// <returns></returns>
public static ImageSource GetImageByFileName(string fileName, bool isLarge = true)
{
var intPtr = ReturnByFileName(fileName, isLarge);
return intPtr.ToString() == "0" ? null : Imaging.CreateBitmapSourceFromHIcon(intPtr, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
/// <summary>
/// 根据文件扩展名(如:.*),返回与之关联的图标。
/// 若不以"."开头则返回文件夹的图标。
/// </summary>
/// <param name="fileType">文件扩展名</param>
/// <param name="isLarge">是否返回大图标</param>
/// <returns></returns>
public static Icon GetIconByFileType(string fileType, bool isLarge = true)
{
Icon resultIcon = null;
try
{
var res = ReturnByFileType(fileType);
var intPtr = ReturnByFileName(res.shell32Path, isLarge, res.iconIndex);
resultIcon = Icon.FromHandle(intPtr);
}
catch
{
// ignored
}
return resultIcon;
}
/// <summary>
/// 根据文件扩展名(如:.*),返回与之关联的图像。
/// 若不以"."开头则返回文件夹的图标。
/// </summary>
/// <param name="fileType">文件扩展名</param>
/// <param name="isLarge">是否返回大图标</param>
/// <returns></returns>
public static ImageSource GetImageByFileType(string fileType, bool isLarge = true)
{
// 处理返回结果
ImageSource resultIcon = null;
try
{
var res = ReturnByFileType(fileType);
var intPtr = ReturnByFileName(res.shell32Path, isLarge, res.iconIndex);
resultIcon = Imaging.CreateBitmapSourceFromHIcon(intPtr, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
catch
{
// ignored
}
return resultIcon;
}
}
}GetSystemIcon.cs
最新推荐文章于 2023-05-17 15:15:34 发布
本文介绍了一种从Windows系统中提取文件图标的方法,包括根据文件名或文件类型获取图标,并提供了使用C#实现的具体代码示例。
5496

被折叠的 条评论
为什么被折叠?



