Bad Quality with Icon.ExtractAssociatedIcon functi

2019-08-03 16:22发布

问题:

I try to fetch the icon from an exe, where I have used the below snippet

 C#
 FileStream fs = new FileStream(icoFileNam, FileMode.OpenOrCreate);
 Icon ico = Icon.ExtractAssociatedIcon(exefilePath);
 ico.Save(fs);

The image saved lacks quality. I have saved the image as .ico file.

Can anyone knows how to retain the original quality of the icon present in the exefile?

回答1:

In the most common cases you can use the extracted icon bitmap data via the Icon.ToBitmap() method. You can save this image to different formats. However it is pretty hard to save the icon as "true" .ico file.

The problem is that there are no embedded encoders for icon images in .Net. So by default the result have been saved as low-color image. If this is unacceptable, the MS is recommended to save raw bitmap data as .ico manually. I suggest you use the IconLib library that already implement this task:

Icon icon = Icon.ExtractAssociatedIcon(@"C:\Windows\System32\notepad.exe");
MultiIcon mIcon = new MultiIcon();
SingleIcon sIcon = mIcon.Add("notepad");
sIcon.CreateFrom(icon.ToBitmap(), IconOutputFormat.Vista);
sIcon.Save(@"c:\notepad.ico");