0

I'm looking for a way to take an icon from an executable, and create a new executable (using CSharpCodeProvider) that uses this icon.

The first part is easy, I do it using:

Icon icon = Icon.ExtractAssociatedIcon(path);

The problems come when I want to "attach" the icon. I tried using:

compilerParameters.CompilerOptions = @"/win32icon:"

But this solution requires the icon to be written in a file and I'd like to avoid that (one of the reason being it's such a mess to save an icon with more than 16 colors...).

Is it possible to use the System.Drawing.Icon object in memory directly?

EDIT: Just to be clear, I'm trying to obtain a code that can compile C# code into an executable. This executable will have an icon that is only hold in memory (as an Icon object) and not in a file (as a .ico file).

Stud
  • 521
  • 4
  • 8
  • You can c# application with roslyn ,search how build compile with roslyn and embed resources like this http://stackoverflow.com/questions/36969090/roslyn-embedd-content-into-assembly-solved – mohsen Jun 08 '16 at 10:47
  • I read about Roslyn and made some test. I still can't figure out how to put an icon to the generated application without using a file. – Stud Jun 08 '16 at 12:43
  • If you have set icon to your application so it will save in a file ***.resx** in your project .you can see it under **Properties** in **Solution Explorer** in your project in Visual studio , by double clicking this file *.resx. so when you want compile your project with roslyn you must before create ***.resources** file from *.resx file and **embed** to your compilation . Visual studio do these work too when compilation.I will answer this .iam busy and I wrote by mobile unfortunately now .. – mohsen Jun 08 '16 at 13:07
  • I don't want to compile my project with Roslyn, I want my project to compile stuffs using the icon extracted from another executable file. So the icon only exist in memory as an Icon object. I hope there is a way to embed the icon without writing a .ico file... – Stud Jun 08 '16 at 13:15
  • http://www.howtogeek.com/109347/extract-high-quality-icons-from-files-using-a-free-tool/ – mohsen Jun 08 '16 at 13:46

2 Answers2

1

Use the IconLibrary to save Icon

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");

And use that for CompilerOptions

More information

https://msdn.microsoft.com/en-us/library/2aaxe43f.aspx

mohsen
  • 1,763
  • 3
  • 17
  • 55
  • Thank you mohsen. There are still quality issues with this lib. I'm investigating it :) – Stud Jun 08 '16 at 15:46
0

Solution :

In case anyone is looking to do the same thing, here is a method that brought the expected result (icon extracting with correct graphic quality).

I use Tsuda Kageyu IconExtractor in order to get the icon I want and convert it to Bitmap with the transparency. Then I use the IconLibrary suggested by @mohsen to write in icon as a .ico file and then embed it with the CompilerOptions.

Stud
  • 521
  • 4
  • 8