shell32.dllからアイコンを取得するにはどうすればよいですか?
質問
自作アプリで使用するツリー アイコンを取得したいと考えています。画像を .icon ファイルとして抽出する方法を知っている人はいますか?16x16 と 32x32 の両方が必要です。または、画面キャプチャを行うだけです。
解決
Visual Studio で、[ファイルを開く...] を選択し、次に [ファイル...] を選択します。次に、Shell32.dll を選択します。フォルダーツリーが開き、「アイコン」フォルダーにアイコンが表示されます。
アイコンを保存するには、フォルダー ツリーでアイコンを右クリックし、[エクスポート] を選択します。
他のヒント
簡単な方法を探している人は、7zip を使用してshell32.dll を解凍し、.src/ICON/ フォルダーを探してください。
別のオプションは、次のようなツールを使用することです。 リソースハッカー. 。アイコンだけでなく、それ以上のことも処理します。乾杯!
shell32.dll からアイコン #238 を抽出する必要があり、Visual Studio や Resourcehacker をダウンロードしたくなかったので、Technet からいくつかの PowerShell スクリプトを見つけました (John Grenfell と # に感謝)https://social.technet.microsoft.com/Forums/windowsserver/en-US/16444c7a-ad61-44a7-8c6f-b8d619381a27/using-icons-in-powershell-scripts?forum=winserverpowershell)同様のことを行い、私のニーズに合わせて新しいスクリプト(以下)を作成しました。
入力したパラメータは次のとおりです (ソース DLL パス、ターゲット アイコン ファイル名、DLL ファイル内のアイコン インデックス)。
C:\Windows\System32\shell32.dll
C: emp estart.ico
238
試行錯誤の結果、一時的に新しいショートカットを作成して、必要なアイコン インデックスが #238 であることがわかりました (デスクトップを右クリックして [新規] --> [ショートカット] を選択し、「calc」と入力して Enter を 2 回押します)。次に、新しいショートカットを右クリックして「プロパティ」を選択し、「ショートカット」タブの「アイコンの変更」ボタンをクリックします。パス C:\Windows\System32\shell32.dll に貼り付けて、[OK] をクリックします。使用したいアイコンを見つけて、そのインデックスを計算します。注意:インデックス #2 は #1 の下にあり、その右側ではありません。私の Windows 7 x64 マシンでは、アイコン インデックス #5 が 2 列目の先頭にありました。
誰かが同様に機能し、より高品質のアイコンを取得するより良い方法を持っている場合は、それについて聞くことに興味があります。ありがとう、ショーン。
#Windows PowerShell Code###########################################################################
# http://gallery.technet.microsoft.com/scriptcenter/Icon-Exporter-e372fe70
#
# AUTHOR: John Grenfell
#
###########################################################################
<#
.SYNOPSIS
Exports an ico and bmp file from a given source to a given destination
.Description
You need to set the Source and Destination locations. First version of a script, I found other examples but all I wanted to do as grab and ico file from an exe but found getting a bmp useful. Others might find useful
No error checking I'm afraid so make sure your source and destination locations exist!
.EXAMPLE
.\Icon_Exporter.ps1
.Notes
Version HISTORY:
1.1 2012.03.8
#>
Param ( [parameter(Mandatory = $true)][string] $SourceEXEFilePath,
[parameter(Mandatory = $true)][string] $TargetIconFilePath
)
CLS
#"shell32.dll" 238
If ($SourceEXEFilePath.ToLower().Contains(".dll")) {
$IconIndexNo = Read-Host "Enter the icon index: "
$Icon = [System.IconExtractor]::Extract($SourceEXEFilePath, $IconIndexNo, $true)
} Else {
[void][Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$image = [System.Drawing.Icon]::ExtractAssociatedIcon("$($SourceEXEFilePath)").ToBitmap()
$bitmap = new-object System.Drawing.Bitmap $image
$bitmap.SetResolution(72,72)
$icon = [System.Drawing.Icon]::FromHandle($bitmap.GetHicon())
}
$stream = [System.IO.File]::OpenWrite("$($TargetIconFilePath)")
$icon.save($stream)
$stream.close()
Write-Host "Icon file can be found at $TargetIconFilePath"
リソースの抽出 は、多くの DLL からアイコンを再帰的に検索する別のツールで、非常に便利な IMO です。
上記のソリューションの更新バージョンは次のとおりです。リンクに埋もれていた不足しているアセンブリを追加しました。初心者にはそれが理解できないでしょう。これは変更せずに実行できるサンプルです。
<#
.SYNOPSIS
Exports an ico and bmp file from a given source to a given destination
.Description
You need to set the Source and Destination locations. First version of a script, I found other examples
but all I wanted to do as grab and ico file from an exe but found getting a bmp useful. Others might find useful
.EXAMPLE
This will run but will nag you for input
.\Icon_Exporter.ps1
.EXAMPLE
this will default to shell32.dll automatically for -SourceEXEFilePath
.\Icon_Exporter.ps1 -TargetIconFilePath 'C:\temp\Myicon.ico' -IconIndexNo 238
.EXAMPLE
This will give you a green tree icon (press F5 for windows to refresh Windows explorer)
.\Icon_Exporter.ps1 -SourceEXEFilePath 'C:/Windows/system32/shell32.dll' -TargetIconFilePath 'C:\temp\Myicon.ico' -IconIndexNo 41
.Notes
Based on http://stackoverflow.com/questions/8435/how-do-you-get-the-icons-out-of-shell32-dll Version 1.1 2012.03.8
New version: Version 1.2 2015.11.20 (Added missing custom assembly and some error checking for novices)
#>
Param (
[parameter(Mandatory = $true)]
[string] $SourceEXEFilePath = 'C:/Windows/system32/shell32.dll',
[parameter(Mandatory = $true)]
[string] $TargetIconFilePath,
[parameter(Mandatory = $False)]
[Int32]$IconIndexNo = 0
)
#https://social.technet.microsoft.com/Forums/windowsserver/en-US/16444c7a-ad61-44a7-8c6f-b8d619381a27/using-icons-in-powershell-scripts?forum=winserverpowershell
$code = @"
using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace System
{
public class IconExtractor
{
public static Icon Extract(string file, int number, bool largeIcon)
{
IntPtr large;
IntPtr small;
ExtractIconEx(file, number, out large, out small, 1);
try
{
return Icon.FromHandle(largeIcon ? large : small);
}
catch
{
return null;
}
}
[DllImport("Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int ExtractIconEx(string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons);
}
}
"@
If (-not (Test-path -Path $SourceEXEFilePath -ErrorAction SilentlyContinue ) ) {
Throw "Source file [$SourceEXEFilePath] does not exist!"
}
[String]$TargetIconFilefolder = [System.IO.Path]::GetDirectoryName($TargetIconFilePath)
If (-not (Test-path -Path $TargetIconFilefolder -ErrorAction SilentlyContinue ) ) {
Throw "Target folder [$TargetIconFilefolder] does not exist!"
}
Try {
If ($SourceEXEFilePath.ToLower().Contains(".dll")) {
Add-Type -TypeDefinition $code -ReferencedAssemblies System.Drawing
$form = New-Object System.Windows.Forms.Form
$Icon = [System.IconExtractor]::Extract($SourceEXEFilePath, $IconIndexNo, $true)
} Else {
[void][Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$image = [System.Drawing.Icon]::ExtractAssociatedIcon("$($SourceEXEFilePath)").ToBitmap()
$bitmap = new-object System.Drawing.Bitmap $image
$bitmap.SetResolution(72,72)
$icon = [System.Drawing.Icon]::FromHandle($bitmap.GetHicon())
}
} Catch {
Throw "Error extracting ICO file"
}
Try {
$stream = [System.IO.File]::OpenWrite("$($TargetIconFilePath)")
$icon.save($stream)
$stream.close()
} Catch {
Throw "Error saving ICO file [$TargetIconFilePath]"
}
Write-Host "Icon file can be found at [$TargetIconFilePath]"
IrfanView で DLL を開き、結果を .gif または .jpg として保存するだけです。
この質問が古いことは知っていますが、「dll からアイコンを抽出」で 2 番目の Google ヒットです。ワークステーションに何もインストールしたくないので、IrfanView を使用していることを思い出しました。
フリーウェアをダウンロードできます リソースハッカー そして、以下の指示に従います。
- アイコンを検索したい DLL ファイルを開きます。
- フォルダーを参照して特定のアイコンを見つけます。
- メニューバーから「アクション」を選択し、「保存」を選択します。
- .ico ファイルの保存先を選択します。
参照 : http://techsultan.com/how-to-extract-icons-from-windows-7/
Linux を使用している場合は、次のコマンドを使用して Windows DLL からアイコンを抽出できます。 gExtractWinIcons。Ubuntu と Debian で利用できます。 gextractwinicons
パッケージ。
このブログ記事には、 スクリーンショットと簡単な説明.
また、利用可能なリソースである Visual Studio Image Library もあり、これは「Microsoft ソフトウェアと視覚的に一貫しているように見えるアプリケーションを作成するために使用できます」が、おそらく下部に記載されているライセンスの対象となります。https://www.microsoft.com/en-ca/download/details.aspx?id=35825