Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I have a .exe file with some icon (which represents the file). Could you please tell me how to extract that icon and display in a picturebox?
 
Your help would be highly appreciated! Thanks!
Posted 2-Oct-12 14:51pm
supernorb2.5K
Comments
Sergey Alexandrovich Kryukov at 2-Oct-12 21:07pm
   
I'm curious why?
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It the icon is there, and if you have Visual Studio, you can load .exe using [Main menu] => Files => Open… => File. If will open a kind of resource viewer to show the resources embedded in the executable module.
 
However, what you want to do is somewhat suspicious. Why? Just want to remind you that a resource could be a subject of the author's intellectual property protected by the law. Also, even though an icon can be shown in a PictureBox, it usually makes no sense, as the icons are designed for different purposes; an icon contains several images of standard sizes in one file, so the OS uses different images in different situations. With picture boxes, you don't use it. Icons are only uses as application icon and system window icons. For picture boxes, you might need to convert an icon into a bitmap. Besides, there are many situations where a picture box is used but actually useless — you don't need this control to show an image in any control.
 
[EDIT]
 
I'm sorry if it was not obvious how to retrieve the resource. Here is just the example.
 
I located the file "c:\Program Files\Common Files\Microsoft Shared\OFFICE12\MSOXMLED.EXE" and loaded it in Visual Studio using main menu as shown above. In the window created as a result, I can see a tree view with the first node "Icon" and its child node "IDI_APP_ICON". Having this icon selected, I activate a context menu and choose "Export". That's it.
 
—SA
  Permalink  
v4
Comments
supernorb at 3-Oct-12 11:20am
   
Anyway I still want to use PictureBox to show any image in my Temporary projects, just for convenience. After asking this question, I tried searching somewhere else in the Internet and I found some solutions, some of them was posted in CodeProject. There are at least 3 ways, the first is very simple with using the static method ExtractAssociatedIcon of the class Icon, the other two ones use Win32 API (ExtractIconEx and SHGetFileInfo, both in Shell32.dll). Converting from Icon to Bitmap is so simple with ToBitmap() method. About the intellectual property, I don't think they want to protect their application icons from people because if the 'stealers' want they can search for those in the internet easily with high quality images not only icons of 32x32. And the most suitable case you may want to extract application icons is for example when you create an application managing a list of your favorite applications and you want to place a representative icon before the application name in each entry. Thanks!
Sergey Alexandrovich Kryukov at 3-Oct-12 12:16pm
   
OK, this is up to you, after all.
Did I answer your question on how to extract your icon? Please try it and then accept this answer (green button), -- thanks.
--SA
supernorb at 8-Oct-12 13:27pm
   
Sorry for the late reply (my network had some trouble). You didn't really point out how to extract icon in an .exe file. And in fact I've found the solutions myself as I said in the last comment. I wish there were 2 accept buttons, a one is for those who deserve it with their correct and helpful answers and the other is for those who want it. Then I would click on the second for you. If you update the answer so that I see how helpful it is, I'll click on the accept button! Thanks
Sergey Alexandrovich Kryukov at 8-Oct-12 13:53pm
   
I though it was more then enough. OK, if you still need to "point out" this simple thing, please see the updated answer, after [EDIT].
--SA
supernorb at 8-Oct-12 14:00pm
   
I don't see any code to do, are you extracting icon at design time? That's not I want. What I want is simply a Browse button to select a .exe file and then show the extracted icon of that file on the form! Thanks!
Sergey Alexandrovich Kryukov at 8-Oct-12 15:57pm
   
Sorry for my misunderstanding of the question, but you really did not explain that you need a programmatical solution. That is also solvable.
--SA
supernorb at 8-Oct-12 16:09pm
   
It seems that my question may be ambiguous but I found some words that make it clear "display in a picturebox", that meant I wanted some code here not the way you suggested, anyway you gave me a solution which may be helpful to me some time in the future. If you want I will click the accept solution button, don't you?
Sergey Alexandrovich Kryukov at 8-Oct-12 16:22pm
   
Sorry again -- it did not look clarifying to me, because I though you just wanted to get the icon, add to project or to data and then use the file programmatically -- these are two different phases. As you need to do programmatically in both phases, Richard's answers works for you. All you need is Windows API and P/Invoke.
Alternative is using mixed-mode C++/CLI project which can include unmanaged code.
 
Just in case, I also want to warn you against PictureBox, in case you wanted to do anything dynamic with it. I have a number of past answers related to the questions, based on very usual abuse of this control.
 
You don't have to accept the solution, as now you explained to me what was missing, and Richard already answered the way you need.
--SA
supernorb at 8-Oct-12 16:29pm
   
About PictureBox, I really can't find any other control which can replace it, I like the SizeMode property of it, could you please suggest me with any other control which has the same property or I have to customize a picturebox myself? Thanks!
Sergey Alexandrovich Kryukov at 8-Oct-12 16:37pm
   
Replacement? I guess you miss something. This control does not anything, practically, Everything you can do with PictureBox, you can do with ANY CONTROL, but not everything you can do with your control you can do with PictureBox. This control is a kind of simplification often used by the beginners, a mere shortcut. For anything dynamic (and not only), you need to override OnPaint and do your custom rendering.
--SA
supernorb at 8-Oct-12 17:45pm
   
As I said, the only reason I use PictureBox is it has SizeMode property, with other controls, I have to customize to use the same property. Thanks!
Sergey Alexandrovich Kryukov at 8-Oct-12 18:09pm
   
Right, this is something already made for you. If your application and use of bitmap/icon graphics is simple enough, this control helps you. I don't say you should not use it, just wanted to warn you against going too far with it, where direct rendering in a control is simpler.
--SA
supernorb at 8-Oct-12 13:29pm
   
No accept but my vote of 5! It's not really bad!
Sergey Alexandrovich Kryukov at 8-Oct-12 13:46pm
   
???
Whatever...
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Just use the LoadIcon()[^] function; although as this is C# you may need to use P/Invoke[^] to get it. Some other suggestions here[^].
  Permalink  
Comments
supernorb at 8-Oct-12 15:13pm
   
Thanks for the another approach using LoadIcon, it's really helpful! My vote of 5 and also a click on Accept Solution.
Sergey Alexandrovich Kryukov at 8-Oct-12 15:58pm
   
My 5. Somehow I did not understand that OP needs do to it programmatically. It also involves loading the executable image to get an instance.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 278
1 Nguyen.H.H.Dang 230
2 Maciej Los 205
3 Peter Leow 195
4 arvind mepani 187


Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid