ここで情報を見つけました。大きな 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;
};
アンマネージ コードの最終的な準備は、一般的な Shell32.dll 内にある SHGetFileInfo の署名を定義することです。
// 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);
すべての準備が整ったので、関数を呼び出して、取得したアイコンを表示します。取得されるオブジェクトは Icon タイプ (System.Drawing.Icon) ですが、PictureBox に表示したいので、ToBitmap() メソッドを使用して Icon を Bitmap に変換します。
しかし、まず、フォームに追加する必要がある 3 つのコントロールがあります。Text プロパティに "Extract Icon" を持つ Button btnExtract、PictureBox である picIconSmall、および PictureBox でもある picIconLarge です。これは、2 つのアイコン サイズを取得するためです。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();
}
}
更新:ここでさらに詳しい情報を見つけました。