【问题标题】:How to extract icons from an executable?如何从可执行文件中提取图标?
【发布时间】:2018-02-17 22:47:49
【问题描述】:

我正在从事一个个人项目,我希望在该项目中让用户有机会浏览和使用注册表(HKCU 密钥),就像他使用 regedit.exe 一样。

一切正常,但我现在想以某种方式提取与注册表值关联的图标。
有人知道我如何实现这样的目标吗?

我想获得的图标示例:

【问题讨论】:

  • 澄清一下,您是要我们告诉您如何从 Windows 操作系统中刷出受版权保护的资源吗?

标签: c# .net winapi icons registry


【解决方案1】:

您可以使用ExtractIconEx() 从regedit.exe 中提取这些图标。

请注意,regedit.exe 可以在 Windows 和 Windows\System32 目录。在这里,我假设它在Windows 之下。

Win API 声明:

  [DllImport("shell32.dll", CharSet = CharSet.Auto)]
  public static extern uint ExtractIconEx(string lpszFile, int nIconIndex, [Out] IntPtr[] phiconLarge, [Out] IntPtr[] phiconSmall, [In] uint nIcons);

  [DllImport("user32.dll")]
  public static extern int DestroyIcon(IntPtr hIcon);

ExtractIconEx()首先用空参数调用,以获取文件包含的图标数量。
使用此信息,我们检索 2 个数组中的小图标和大图标句柄,并使用 Icon.FromHandle() 从这些句柄创建一个 GDI+ 图标。

string icnSource = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "regedit.exe");

uint icnNumber = ExtractIconEx(icnSource, -1, null, null, 1);

if (icnNumber > 0)
{
    IntPtr[] phiconLarge = new IntPtr[icnNumber];
    IntPtr[] phiconSmall = new IntPtr[icnNumber];
    ExtractIconEx(icnSource, 0, phiconLarge, phiconSmall, icnNumber);

    Icon[] iconsmall = new Icon[icnNumber];
    Icon[] iconlarge = new Icon[icnNumber];

    for (int x = 0; x < icnNumber; x++)
    {
        if (phiconLarge != null)
        {
            iconlarge[x] = (Icon)Icon.FromHandle(phiconLarge[x]).Clone();
            DestroyIcon(phiconLarge[x]);
        }
        if (phiconSmall != null)
        {
            iconsmall[x] = (Icon)Icon.FromHandle(phiconSmall[x]).Clone();
            DestroyIcon(phiconSmall[x]);
        }
    }
}

如有必要,您可以使用Icon.ToBitmap() 将图标转换为 GDI+ 位图。

这些,数字 3 和 4,是您正在寻找的图标:

pictureBox1.Image = iconsmall[3].ToBitmap();
pictureBox2.Image = iconsmall[4].ToBitmap();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 2012-05-25
    相关资源
    最近更新 更多