如何从图标库中获取特定图标?

问题描述 投票:0回答:1

我有这段代码可以从

shell32.dll
中提取一个图标。

$iconPath = "$env:SystemRoot\system32\shell32.dll"  # Path to the shell32.dll library
$iconIndex = 4  # Index of the folder icon in the library
$icon = [System.Drawing.Icon]::ExtractAssociatedIcon($iconPath)
$icon = [System.Drawing.Icon]::new($icon, 16, 16)

这部分起作用,因为它从 shell32.dll 获取

an
图标,但如您所见,
$icon
没有在任何地方引用
$iconIndex
,所以我没有得到图标“4”。我尝试将
$iconIndex
作为
ExtractAssociatedIcon
的第二个参数,但这会产生错误。

如何从

$icon
库中获取
$iconIndex = 4
以引用
shell32.dll

powershell dll icons system.drawing
1个回答
0
投票

[System.Drawing.Icon]::ExtractAssociatedIcon()
不是在 shell32.dll 中提取给定数量的图标的解决方案,而是只关联一个。

但是 shell32.dll 本身提供了一种方法 (

[Shell32_Extract]::ExtractIconEx()
) 来执行此操作,此解决方案适用于 Shell32_Extract:


add-type -typeDefinition '

using System;
using System.Runtime.InteropServices;

public class Shell32_Extract {

  [DllImport(
     "Shell32.dll",
      EntryPoint        = "ExtractIconExW",
      CharSet           =  CharSet.Unicode,
      ExactSpelling     =  true,
      CallingConvention =  CallingConvention.StdCall)
  ]

   public static extern int ExtractIconEx(
      string lpszFile          , // Name of the .exe or .dll that contains the icon
      int    iconIndex         , // zero based index of first icon to extract. If iconIndex == 0 and and phiconSmall == null and phiconSmall = null, the number of icons is returnd
      out    IntPtr phiconLarge,
      out    IntPtr phiconSmall,
      int    nIcons
  );

}
';
#
$iconPath = "$env:SystemRoot\system32\shell32.dll"  # Path to the shell32.dll library
$iconIndex = 4  # Index of the folder icon in the library
#$icon = [System.Drawing.Icon]::ExtractAssociatedIcon($iconPath)
#$icon = [System.Drawing.Icon]::new($icon, 16, 16)
#$path = (Get-Location).Path + "\a1.ico"
#$icon.ToBitMap().save($path)

[System.IntPtr] $phiconSmall = 0
[System.IntPtr] $phiconLarge = 0

$nofImages = [Shell32_Extract]::ExtractIconEx($iconPath, -1, [ref] $phiconLarge, [ref] $phiconSmall, 0)

$nofIconsExtracted = [Shell32_Extract]::ExtractIconEx($iconPath, $iconIndex, [ref] $phiconLarge, [ref] $phiconSmall, 1)

if ($nofIconsExtracted -ne 2) {
    Write-Error "iconsExtracted = $nofIconsExtracted"
}

$iconSmall = [System.Drawing.Icon]::FromHandle($phiconSmall);
$iconLarge = [System.Drawing.Icon]::FromHandle($phiconLarge);

$bmpSmall = $iconSmall.ToBitmap()
$bmpLarge = $iconLarge.ToBitmap()

#
#  System.Drawing.Image.Save(), without specifying an encoder, stores
#  the bitmap in png format.
#
$bmpLarge.Save("$(get-location)\small-$iconIndex.png");
$bmpLarge.Save("$(get-location)\large-$iconIndex.png");

#
#  Use System.Drawing.Imaging.ImageFormat to specify a
#  different format:
#

$bmpSmall.Save("$(get-location)\small-$iconIndex.bmp", [System.Drawing.Imaging.ImageFormat]::Bmp );
$bmpLarge.Save("$(get-location)\large-$iconIndex.bmp", [System.Drawing.Imaging.ImageFormat]::Bmp );
   
$bmpSmall.Save("$(get-location)\small-$iconIndex.jpg", [System.Drawing.Imaging.ImageFormat]::Jpeg);
$bmpLarge.Save("$(get-location)\large-$iconIndex.jpg", [System.Drawing.Imaging.ImageFormat]::Jpeg);

这会生成这样的图像:

© www.soinside.com 2019 - 2024. All rights reserved.