51

I have an C# app. I need to add an icon to that app so i added an icon resource. Adding resource went fine, but is there any way to use my (resource) icon as form icon WITHOUT adding additional code? When i try to use design-time "icon" property of form it seems i have to choose a file, but i want to use embedded icon...

Any help?

guest86
  • 2,894
  • 8
  • 49
  • 72
  • 2
    Answered here: http://stackoverflow.com/questions/90697/how-to-create-and-use-resources-in-net – OSH Jan 28 '13 at 09:18

8 Answers8

140
  1. Add the icon to the project Resources.resx.

  2. Open the designer of the form you want to add the icon to.

  3. Append the InitializeComponent function.

  4. Add this line in the top:

    this.Icon = {EntryPointClassName}.Properties.Resources.{IconResoureceName};
    

    repeat step 4 for any forms in your project you want to update

carlin.scott
  • 6,214
  • 3
  • 30
  • 35
Moudi
  • 1,401
  • 2
  • 9
  • 2
  • 2
    Thanks @Moudi for this answer - this is the way to go! If you use a resource (i.e. icon, image) more than once in your application, it will reduce file size of your application if embedded the way you describe here! – Sebastian Mar 06 '14 at 12:56
  • This feels like the right solution, but it also breaks the designer. Once you add code to the InitializeComponent function, trying to open designer shows the error "The code within the method 'InitializedComponent' is generated by the designer and should not be manually modified. Any suggestions to get around this? – Aerankas Aug 27 '15 at 18:02
  • @Aerankas You can add the same line in the Load event of the form rather than putting in the InitializeComponent and it has the same effect. – PabloInNZ Sep 20 '15 at 23:43
  • 3
    You can also just include this in the form constructor just after `InitializeComponent` is called – Robert Rouhani Nov 29 '15 at 20:18
  • 1
    Sometimes, you can't set picture directly, for API example. I need to set an absolute fullpath. Is there a way to get the resource path (without write any string to avoid loosing the build completion, and missing detection) ? – Julian50 Mar 23 '16 at 08:27
13

How I load Icons: Using Visual Studio 2010: Go to the project properties, click Add Resource > Existing File, select your Icon.

You'll see that a Resources folder appeared. This was my problem, I had to click the loaded icon (in Resources directory), and set "Copy to Output Directory" to "Copy always". (was set "Do not copy").

Now simply do:

Icon myIcon = new Icon("Resources/myIcon.ico");
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
user1481088
  • 165
  • 1
  • 3
  • Alas I am trying to do this to fix a problem opening my Design view, and I still get ; Could not find a part of the path 'C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\DocuityConfig\MyApp.ico'. – AnneTheAgile Nov 28 '12 at 02:08
  • 2
    If you load the icon from file it is pointless to add as a resource, only increases you executable/assembly size. Another problem with loading from file is that you'll have to deploy all files individually. Note that end users will be able to easily replace external resources by overwriting the files. – user3285954 Nov 30 '14 at 16:28
  • You shouldn't do this. GDI+ cache will be increased for every calling new Icon(..). Better is used one instance and reference it, especially if you need binding for a certain amount of your items. – Ondrej Rozinek Oct 05 '15 at 14:03
  • This doesn't work and doesn't do anything. you never assign the icon to this.Icon and there are other issues with this as mentioned above. – Fractal Oct 24 '17 at 19:47
  • If the Icon isn't there, is your app gonna crash? – TheDudeWithHat Aug 25 '20 at 08:39
13

choosing that file, will embed the icon in the executable.

The Lazy Coder
  • 11,560
  • 4
  • 51
  • 69
3

in visual studio for vb.net, go to the project properties, click Add Resource > Existing File, select your Icon.

in your code: Me.Icon = My.Resources.IconResourceName

Rob
  • 3,488
  • 3
  • 32
  • 27
3

Forms maintain separate resource files (SomeForm.Designer.resx) added via the designer. To use icons embedded in another resource file requires codes. (this.Icone = Project.Resources.SomeIcon;)

bricklayer137
  • 374
  • 2
  • 10
2

On Form_Load:

this.Icon = YourProjectNameSpace.Resources.YourResourceName.YouAppIconName;

Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
Lucas Ponzo
  • 104
  • 1
  • 6
  • 2
    How is this answer better than Moudi's answer? – Cody G Jan 05 '17 at 20:02
  • I find it's `Properties.Resources.AppIcon` or `YourProjectNamespace.Properties.Resources.AppIcon` and like `NotifyIcon1.Icon = My.Resources.Logo_square` Though personally I am not setting the icon like that I just drag notifyicon on there and click the little arrow and set the icon.. There is a good video System Tray Icon, NotifyIcon Control example in windows form c# by winforms https://www.youtube.com/watch?v=tCHOdDzUFIY – barlop Feb 28 '19 at 03:59
2

After adding the ICO file to your apps resources, you can use references it using My.Resources.YourIconNameWithoutExtension

For example if I had a file called Logo-square.ico added to my apps resources, I can set it to an icon with:

NotifyIcon1.Icon = My.Resources.Logo_square
A.Badger
  • 4,279
  • 1
  • 26
  • 18
0

You can use this without extra size also

this.Icon = Icon.ExtractAssociatedIcon(AppDomain.CurrentDomain.FriendlyName);