本文轉自:http://www.cnblogs.com/tuyile006/archive/2006/12/25/602818.html
要用到命名空间:using System.IO;
public Icon[] myicon=new Icon[1000];
public int currentIndex=0;
[System.Runtime.InteropServices.DllImport("shell32.dll")]
private static extern int ExtractIconEx(string lpszFile, int niconIndex, ref IntPtr phiconLarge, ref IntPtr phiconSmall, int nIcons);
//打开exe文件
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.Refresh();
IntPtr Large, Small;
int i, nIcons;
Large = (IntPtr)0;
Small = (IntPtr)0;
nIcons = ExtractIconEx(openFileDialog1.FileName, -1, ref Large, ref Small, 1);
Graphics g = this.CreateGraphics();
for (i = 0; i < nIcons; i++)
{
ExtractIconEx(openFileDialog1.FileName, i, ref Large, ref Small, 1);
myicon[currentIndex] = Icon.FromHandle(Large);
g.DrawIcon(myicon[currentIndex], (i / 3) * 40, (i % 3) * 40);
currentIndex++;
}
}
}
//保存icon图标
private void button2_Click(object sender, EventArgs e)
{
FileStream fs;
for (int j = 0; j < currentIndex; j++)
{
fs = new FileStream("c://"+j.ToString()+".ico",FileMode.Create,FileAccess .ReadWrite);
myicon[j].Save(fs);
fs.Close();
}
MessageBox.Show ("保存成功!");
}
注:未驗証正确性
本文介绍了一种使用C#从EXE文件中提取图标的实现方法。通过调用shell32.dll中的ExtractIconEx函数,可以获取指定EXE文件的所有图标资源,并将其显示在界面上。此外,还提供了一个保存图标的功能,能够将提取到的图标保存为单独的ICO文件。
665

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



