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:
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;
}
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.
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.
The answer from user Alizer is good because the standard Microsoft class System.Drawing.Icon does not give you the icon with the best resolution. To make it easier for yourself and to get a better performance you can use the NuGet package Ico.Reader.
Example code:
var icoReader = new IcoReader();
string filePath = @"C:\myfile.exe";
IcoData icoData = icoReader.Read(filePath);
// Get the image with the best quality from exe file
int preferredIndex = icoData.PreferredImageIndex();
byte[] imageBytes = icoData.GetImage(preferredIndex);
// Convert into Bitmap and display in PictureBox
Bitmap iconBitmap = new Bitmap(new MemoryStream(imageBytes));
_pictureBox.Image = iconBitmap;
Here you would also have the option of accessing all images of the icon. GitHub Source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With