如何从可执行文件中获取图标,只有在 C# 中的进程实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/203456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the icon from the executable file only having an instance of it's Process in C#
提问by Haim Bender
I can get the executable location from the process, how do I get the icon from file?
我可以从进程中获取可执行位置,如何从文件中获取图标?
Maybe use windows api LoadIcon(). I wonder if there is .NET way...
也许使用 windows api LoadIcon()。我想知道是否有 .NET 方式...
采纳答案by TheSoftwareJedi
Icon ico = Icon.ExtractAssociatedIcon(theProcess.MainModule.FileName);
回答by RobS
This is a sample from a console application implementation.
这是来自控制台应用程序实现的示例。
using System;
using System.Drawing; //For Icon
using System.Reflection; //For Assembly
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
try
{
//Gets the icon associated with the currently executing assembly
//(or pass a different file path and name for a different executable)
Icon appIcon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
}
catch(ArgumentException ae)
{
//handle
}
}
}
}
回答by Bob Nadler
Use the ExtractIconEx(and here) p/invoke. You can extract small and large icons from any dll or exe. Shell32.dll itself has over 200 icons that are quite useful for a standard Windows application. You just have to first figure out what the index is for the icon(s) you want.
使用ExtractIconEx(和这里)p/invoke。您可以从任何 dll 或 exe 中提取大小图标。Shell32.dll 本身有超过 200 个对标准 Windows 应用程序非常有用的图标。您只需要首先弄清楚您想要的图标的索引是什么。
Edit: I did quick SO search and found this. The index 0 icon is the application icon.
编辑:我进行了快速搜索并找到了这个。索引 0 图标是应用程序图标。