11

I know that this must be incredibly easy - It's unbelievable how long I have searched for an answer to this question based on how simple it is in VB6. I simply want to extract an Icon from an EXE File using Icon.ExtractAssociatedIcon, and then save this icon file to my hard drive.

So, here is what I have, and I will also show you what I have tried so you don't think I'm being lazy.

OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();

string s = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\IconData.ico";

Icon ico = Icon.ExtractAssociatedIcon(ofd.FileName);
Bitmap bmp = ico.ToBitmap();

bmp.Save(s, System.Drawing.Imaging.ImageFormat.Icon);

The above code just makes a file called "IconData.ico" on my desktop which is 0 bytes in length. Again, I am sure this must be incredibly easy to do, but for the life of my I can't figure it out.

Thank you!

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • You should share the executable file or the icon you're trying to open, because here it is working without problems. – Oscar Mederos May 08 '11 at 01:33
  • I changed the original icon file's name from 9665.ico to FileIcon.ico. The new file "IconData.ico" has a blank white icon on my desktop, but when I open it in paint I can see the image. Is this normal? –  May 08 '11 at 01:44
  • [This](http://stackoverflow.com/questions/4042488/how-to-save-bitmap-as-icon) SO thread has a link to [this](http://www.codeproject.com/KB/cs/IconLib.aspx) CodeProject library, which looks promising. Might wanna check that out. – Nick Spreitzer May 09 '11 at 17:42
  • 1
    'So we don't thing you are being lazy?' A lazy programmer is actually a good thing. It's a big motivation to get easier ways to do things which is all our trade is all about. – Gichamba May 09 '11 at 21:53
  • There must be something wrong with your setup. Your code is working without a problem. – Gichamba May 09 '11 at 21:56

1 Answers1

13

You will get better results if you save the icon without first converting to a bitmap. This is because an "Icon" can contain multiple sizes whereas a bitmap is a single size chosen during the conversion.

The Icon class does not have a save to file method, but it does have a save to FileStream method, so you can save it like this:

        string s = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\IconData.ico";
        using (FileStream fs = new FileStream(s, FileMode.Create))
            ico.Save(fs);
hsmiths
  • 1,257
  • 13
  • 17
  • 1
    That was my first instinct as well, but when I tried it the saved icon didn't come out "right". [Here's the result.](http://goo.gl/P2NWC) I used excel.exe as the target assembly. – Nick Spreitzer May 08 '11 at 19:56
  • 1
    Thank you, this is what I have been using myself. I forgot to update this post as it hadn't been 8 hours since I first found the solution, so i wasn't aloud to post my answer. But, I will mark this as correct. –  May 10 '11 at 16:17
  • 3
    Using lync.exe (skype) as an example, I get very blocky and pixelated results with this. Is that to be expected, or have I gone wrong somewhere? – xandermonkey Apr 19 '17 at 14:09