【问题标题】:Create a high quality .ico file from a PNG using powershell only仅使用 powershell 从 PNG 创建高质量的 .ico 文件
【发布时间】:2022-07-29 19:21:37
【问题描述】:

我想使用 powershell 从网站下载 favicon.png,然后将其转换为可用于桌面快捷方式的 favicon.ico 文件。我已经知道如何使用 Invoke-WebRequest 从网站上获取 favicon.png。我在网上找到了这段代码:

  param(
    [Parameter(Mandatory=$true)]
    $bitmapPath,
    $iconPath = "$env:temp\newicon.ico"
  )

Add-Type -AssemblyName System.Drawing

if (Test-Path $bitmapPath) {
    $b = [System.Drawing.Bitmap]::FromFile($bitmapPath)
    $icon = [System.Drawing.Icon]::FromHandle($b.GetHicon())
    $file = New-Object System.IO.FileStream($iconPath, 'OpenOrCreate') 
    $icon.Save($file)
    $file.Close()
    $icon.Dispose()

    explorer "/SELECT,$iconpath"
} else { Write-Warning "$BitmapPath does not exist" }
}

唯一的问题是生成的 .ico 文件质量非常低: ico vs png (.ico 左,.png 右)

有什么方法可以编辑这个脚本并使生成的文件质量更高?

这是通过 Intune 部署的脚本的一部分,因此它无法触及 prem 资源,否则我只需创建 .icos 并将它们托管在那里。

【问题讨论】:

  • 看起来位深度搞砸了。该图标可能使用每像素 8 位甚至 4 位,而 PNG 可能使用每像素 32 位。确保使用相同的位深度创建图标。对不起,我不能提供更多细节,必须先自己研究这个话题。如果您在c# 标签下搜索,您可能会获得更多成功。

标签: powershell icons png .ico


【解决方案1】:

试试这个:

if (Test-Path -Path $bitmapPath -PathType Leaf) {
    $img = [System.Drawing.Image]::FromFile($bitmapPath)
    $img.Save($iconPath, [System.Drawing.Imaging.ImageFormat]::Icon)
    $img.Dispose()
}

【讨论】:

    猜你喜欢
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 2020-10-12
    • 2016-04-22
    • 2012-07-10
    • 2017-11-14
    • 1970-01-01
    相关资源
    最近更新 更多