powershell - 图标损坏与Powershell exe到ico转换器

标签 powershell icons executable converters ico

我正在寻找一个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而是我们可以在互联网上找到的普通.ico?

先感谢您 ! :)

最佳答案

可能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()
}

关于powershell - 图标损坏与Powershell exe到ico转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60299592/

相关文章:

.net - 如何在 Gitlab CI 中使用 Cake.net?

powershell - [string]::Format 和 -f 之间的区别,以及数组与分割运算符

Android Canvas背景透明

hyperlink - 为什么我的 Admob 帐户上的应用显示默认图标?

gcc - 使用 gcc 为 32 位体系结构编译的 C 程序的意外退出代码

windows - perl if( -e "带空格路径的窗口){}

powershell - 如何评估来自标准输入的 PowerShell 脚本输入

安卓|主屏幕上的书签图标

wix - 在 WIX 中安装期间如何静默运行另一个程序 (PostgreSQL) 的安装程序?

powershell - 使用 Microsoft Cognitive API 的 Invoke-RestMethod 收到 400 个错误请求错误