找到信息here。要获得大的 Vista 图标,您需要使用 Shell32 的 SHGetFileInfo 方法。我已经复制了下面的相关文本,当然您需要将文件名变量替换为“Assembly.GetExecutingAssembly().Location”。
using System.Runtime.InteropServices;
我们将在调用 SHGetFileInfo() 时使用一组常量来指定我们希望检索的图标的大小:
// Constants that we need in the function call
private const int SHGFI_ICON = 0x100;
private const int SHGFI_SMALLICON = 0x1;
private const int SHGFI_LARGEICON = 0x0;
SHFILEINFO 结构非常重要,因为它将是我们处理各种文件信息的句柄,其中包括图形图标。
// This structure will contain information about the file
public struct SHFILEINFO
{
// Handle to the icon representing the file
public IntPtr hIcon;
// Index of the icon within the image list
public int iIcon;
// Various attributes of the file
public uint dwAttributes;
// Path to the file
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string szDisplayName;
// File type
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
非托管代码的最后准备是定义SHGetFileInfo的签名,它位于流行的Shell32.dll中:
// The signature of SHGetFileInfo (located in Shell32.dll)
[DllImport("Shell32.dll")]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, int cbFileInfo, uint uFlags);
现在我们已经准备好了一切,是时候调用函数并显示我们检索到的图标了。将被检索的对象是一个图标类型 (System.Drawing.Icon),但我们希望在 PictureBox 中显示它,因此我们将使用 ToBitmap() 方法将图标转换为位图。
但首先,您需要将 3 个控件添加到表单中,一个按钮 btnExtract 的 Text 属性具有“提取图标”,picIconSmall 是一个 PictureBox,一个 picIconLarge 也是一个 PictureBox。那是因为我们会得到两个图标大小。现在在 Visual Studio 的设计视图中双击 btnExtract,您将看到它的 Click 事件。里面是剩下的代码:
private void btnExtract_Click(object sender, EventArgs e)
{
// Will store a handle to the small icon
IntPtr hImgSmall;
// Will store a handle to the large icon
IntPtr hImgLarge;
SHFILEINFO shinfo = new SHFILEINFO();
// Open the file that we wish to extract the icon from
if(openFile.ShowDialog() == DialogResult.OK)
{
// Store the file name
string FileName = openFile.FileName;
// Sore the icon in this myIcon object
System.Drawing.Icon myIcon;
// Get a handle to the small icon
hImgSmall = SHGetFileInfo(FileName, 0, ref shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON);
// Get the small icon from the handle
myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
// Display the small icon
picIconSmall.Image = myIcon.ToBitmap();
// Get a handle to the large icon
hImgLarge = SHGetFileInfo(FileName, 0, ref shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_LARGEICON);
// Get the large icon from the handle
myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
// Display the large icon
picIconLarge.Image = myIcon.ToBitmap();
}
}
更新:找到更多信息here。