使用 Powershell exe 到 ico 转换器损坏的图标

Icon corrupted with Powershell exe to ico converter

我正在寻找一个 powershell 脚本来提取可执行文件的 .ico。

我发现了一些看起来像这样工作的工具:

[System.Reflection.Assembly]::LoadWithPartialName('System.Drawing')  | Out-Null

$folder = "C:\TMP_7423\icons"
md $folder -ea 0 | Out-Null

dir $env:windir *.exe -ea 0 -rec |
  ForEach-Object { 
    $baseName = [System.IO.Path]::GetFileNameWithoutExtension($_.FullName)
    Write-Progress "Extracting Icon" $baseName
    [System.Drawing.Icon]::ExtractAssociatedIcon($_.FullName).ToBitmap().Save("$folder$BaseName.ico")
}

但问题是,我想使用 powershell 到 exe 转换器的另一个工具,该工具具有直接在生成的 .exe 中放置图标的功能。我想将提取的 ico 用于生成的可执行文件。 问题是 ps1 到 exe 的转换器不适用于 ico 提取器生成的任何图标。 但是,问题是当我使用在互联网上找到的 .ico 时,ps1 到 exe 转换器工作。

那么,您是否有任何可执行的 .ico 提取器,它不会提取损坏的 ico,而是我们可以在 Internet 上找到的正常 .ico?

提前谢谢您! :)

我使用 Windows 表单编写了一个 Powershell 实用程序,正是出于这个目的:

此处可用:IconTool

可能 ps1 到 exe 的转换器需要合适的 .ICO 并且您的代码使用 ToBitmap().Save(),这不会以 ico 格式保存图像信息。为此,您需要使用提取图标本身的 Save() 方法。

遗憾的是,此 Save 方法的参数只能是流对象,而不能是简单的路径。

尝试:

Add-Type -AssemblyName System.Drawing

$folder = "C:\TMP_7423\icons"
if (!(Test-Path -Path $folder -PathType Container)) {
    $null = New-Item -Path $folder -ItemType Directory
}
Get-ChildItem -Path $env:windir -Filter '*.exe' -Recurse |
  ForEach-Object { 
    Write-Progress "Extracting Icon $($_.BaseName)"
    $ico = [System.Drawing.Icon]::ExtractAssociatedIcon($_.FullName)
    $target = Join-Path -Path $folder -ChildPath ('{0}.ico' -f $_.BaseName)
    # create a filestream object
    $stream = [System.IO.FileStream]::new($target, [IO.FileMode]::Create, [IO.FileAccess]::Write)
    # save as ICO format to the filestream
    $ico.Save($stream)
    # remove the stream object
    $stream.Dispose()
    $ico.Dispose()
}