我如何可以加载在C#程序图标 [英] How can I load a program icon in C#

查看:125
本文介绍了我如何可以加载在C#程序图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些程序的路径(例如资源管理器),我怎么能得到程序图标,将其转换成PNG / JPEG,然后在PictureBox中显示?

I have a path of some program (for example explorer), how can I get program icon, convert it to png/jpeg and then display in PictureBox?

我有这样的事情:

string filePath = "C:\\myfile.exe";
Icon TheIcon = IconFromFilePath(filePath);
if (TheIcon != null) {

 // But then I don't know what to do...

}

public Icon IconFromFilePath(string filePath){
 Icon programicon = null;
 try {
  programicon = Icon.ExtractAssociatedIcon(filePath);
 }
 catch { } 
 return programicon;
}



我发现了类似的此处。这里是图标。如何创建32位图标?

I found something similar here. Here is the icon. How I can create 32-bit icon?

推荐答案

中的代码是出奇的简单,如果你知道去哪里找。先从 图标,因为这是从根本上你这里经过是什么

The code is surprisingly simple if you know where to look. Start with the Icon class, since that's fundamentally what you're after here.

如果你浏览它的方法,你会遇到一个非常有趣的看的 ExtractAssociatedIcon 。接受指定,如可执行文件的路径包含图标,文件中的单个字符串参数。

If you browse its methods, you'll come across a very interesting looking ExtractAssociatedIcon. That accepts a single string parameter that specifies the path to a file containing an icon, such as an executable file.

所以,给你一个图标对象,现在你只需要在一个图片来显示它。您不必将其转换为一个PNG或JPEG,位图工作正常。并有一个内置的该成员函数: ToBitmap

So that gives you an Icon object, now you just need to display it in a PictureBox. You don't have to convert it to a PNG or JPEG, a bitmap works fine. And there's a built-in member function for that: ToBitmap.

分配新位图到的 PictureBox.Image 属性是所有你需要做的显示。

Assigning the new bitmap to the PictureBox.Image property is all you need to do to display it.

这篇关于我如何可以加载在C#程序图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆