--------------------------------------------------------- //プログラムと同じフォルダに「C:\Windows\winhlp32.exe」の256x256アイコンを出力 using System; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; using System.IO; using System.Reflection;
public class Icon256 { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct SHFILEINFO { public IntPtr hIcon; public int iIcon; public uint dwAttributes; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string szDisplayName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] public string szTypeName; }
[DllImport("shell32.dll")] static extern int SHGetImageList(uint iImageList, ref Guid riid, out IntPtr ppv);
[DllImport("comctl32.dll", SetLastError=true)] static extern bool ImageList_DrawEx(IntPtr himl, int i, IntPtr hdcDst, int x, int y, int dx, int dy, uint rgbBk, uint rgbFg, int fStyle);
string iconFilePath = @"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\VS2010ImageLibrary\1041\Actions\ico_format\WinVista\burnCD_error.ico";
var size = new System.Drawing.Size(256, 256); var multiIcon = new System.Drawing.IconLib.MultiIcon(); multiIcon.Load(iconFilePath); foreach (var singleIcon in multiIcon) { var icon = singleIcon.FirstOrDefault(x => x.Size == size)?.Icon; if (icon != null) { string outName = string.Format(@"D:\{0}_256_{1}.ico", System.IO.Path.GetFileNameWithoutExtension(iconFilePath), singleIcon.Name ); using (var sm = new System.IO.FileInfo(outName).Create()) using (icon) { icon.Save(sm); } } }
■No35443に返信(inunoshiさんの記事) >> using (var sm = new System.IO.FileInfo(outName).Create()) >> using (icon) 済みません、前回答のこの部分は using (icon) using (var sm = new System.IO.FileInfo(outName).Create()) の書き間違いです。