获取exe文件中的图标

 要用到命名空间: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 ("保存成功!");
        }
posted @ 2006-12-25 11:24  小y  阅读(4606)  评论(5编辑  收藏  举报