话不多说,上代码,使用这个代码提取到图标文件格式仍然是png,哪里出问题了,万能的坛友么,赐教下吧。[C#] 纯文本查看 复制代码 private void button1_Click(object sender, EventArgs e)
{
try
{
// Image img = System.Drawing.Icon.ExtractAssociatedIcon(textBox1.Text).ToBitmap();
string filePath = textBox1.Text;
var filename = System.IO.Path.GetFileName(textBox1.Text);
string savePath = Environment.GetEnvironmentVariable("USERPROFILE") + "\\Desktop\\AppIcos\\";
if (!Directory.Exists(savePath)) Directory.CreateDirectory(savePath);
// img.Save(savePath + filename +".ico");
var iconTotalCount = PrivateExtractIcons(filePath, 0, 0, 0, null, null, 0, 0);
//用于接收获取到的图标指针
IntPtr[] hIcons = new IntPtr[iconTotalCount];
//对应的图标id
int[] ids = new int[iconTotalCount];
//成功获取到的图标个数
var successCount = PrivateExtractIcons(filePath, 0, 256, 256, hIcons, ids, iconTotalCount, 0);
//遍历并保存图标
for (var i = 0; i < successCount; i++)
{
//指针为空,跳过
if (hIcons[i] == IntPtr.Zero) continue;
using (var ico = Icon.FromHandle(hIcons[i]))
{
using (var myIcon = ico.ToBitmap())
{
myIcon.Save(savePath + ids[i].ToString("000") + ".ico", ImageFormat.Icon);
}
}
//内存回收
DestroyIcon(hIcons[i]);
}
MessageBox.Show("图标已经提取到桌面的AppIcos文件中。");
}
catch(Exception ex) {
MessageBox.Show(ex.ToString());
} |