How can I load a program icon in C#

后端 未结 2 1770
渐次进展
渐次进展 2021-01-25 20:38

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?

I have something like this:

2条回答
  •  盖世英雄少女心
    2021-01-25 21:16

    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.

    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.

    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.

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

提交回复
热议问题