Idanz

Hi,

how can I extract an icon from a file (C#)

For example: if I'll choose the EXE file which runs the "MS Office Word", I'll get the icon with the blue "W".

Thanks, Idan.



Re: Visual C# General Extract an icon

boban.s

Open Visual Studio, and go to File\Open and browse exe or dll that contains resouce icons or bitmaps. When file is open you will see a tree that is like folders, and if you see icons or bitmaps, you can open and see them. You can right click on the icon that you like and click export.




Re: Visual C# General Extract an icon

Idanz

You didn't understand me.

I would like to write a program which extracts icons from their files and save it in new folder.

Thank you.





Re: Visual C# General Extract an icon

Martin Xie - MSFT

How to: Extract the Icon Associated with a File in Windows Forms (Use the Icon.ExtractAssociatedIcon() method)

Many files have embedded icons that provide a visual representation of the associated file type.

The code example included in above article demonstrates how to extract the icon associated with a file and display the file name and its associated icon in a ListView control.

How to find the default icon for a given file extension

Loading icons from an .exe






Re: Visual C# General Extract an icon

Idanz

Thank you very much!



Re: Visual C# General Extract an icon

Idanz

I have a little problem, maybe you can help me.

My application is more or less same to the application that you gave me (Extract the icon associated...).

Code Snippet

string FileName = openFileDialog1.FileName;

listView1.Items.Add(FileName, 1);

StreamWriter SW;

SW = File.AppendText("C:\\MySoft.txt");

SW.WriteLine(FileName);

SW.Close();

This is my code.

I'm choosing an item from the "openfiledialog", putting the link to the file in a text file, and the adding the item to the listview.

When I'm runnig the program (Page_Load) I'm open the text document and adding the lines as items:

Code Snippet

StreamReader SR = new StreamReader("C:\\MySoft.txt");

string line = SR.ReadLine();

while (line != null)

{

listView1.Items.Add(line, 1);

line = SR.ReadLine();

}

SR.Close();

Now, how can I add the icon of the file I chose to the item in the listview

Thank you, Idan.





Re: Visual C# General Extract an icon

Martin Xie - MSFT

Dear Idanz,

Welcome back!Smile

The solution in this document may solve your issue.

Use the SHGetFileInfo Function to Get the Icons associated with Files and display in the ListView controlin(C# .NET).

Please follow these steps inside.

Primary Code Snippet

string fName="D:\myWord.doc";
SHFILEINFO shinfo = new SHFILEINFO();

listView1.SmallImageList = imageList1;
listView1.LargeImageList = imageList1;



//Use this to get the small Icon
IntPtr hImgSmall = Win32.SHGetFileInfo(fName, 0, ref shinfo,
(uint)Marshal.SizeOf(shinfo),
Win32.SHGFI_ICON |
Win32.SHGFI_SMALLICON);
 
  //Use this to get the large Icon
IntPtr hImgLarge = SHGetFileInfo(fName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo),Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
 
  //The icon is returned in the hIcon member of the shinfo struct
System.Drawing.Icon myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
 
  imageList1.Images.Add(myIcon);
 
  //Add file name and icon to listview
listView1.Items.Add(fName, nIndex++);

addition VB6 sample Show a File's Associated Icon in a ListView.






Re: Visual C# General Extract an icon

Idanz

Thank you very much for your help Martin!