How can I import .ico into .exe in c# ? (I want to have only one file)
In resources file I have: icon.ico
When i click on that icon, in properties I have: "Embedded Resource"
What I shoud do(change)?
trayIcon.Icon = new Icon("c:\\users\\wulp\\documents\\visual studio 2013\\Projects\\WifiSwitch\\WifiSwitch\\Resources\\icon.ico");
and, how I have to use relative path ?
thanks
I landed here searching for a written solution regarding icon embedding in the output assembly/executable. But this accepted answer is not really explaining anything, unless you know how the answer relates to the code as a concept. For the code part, the link given by Jonas died in meantime as well.
Set as embedded resource
But another way is to set the icon file (which is preferably) in your solution as embedded resource by right click on the icon file in your solution. And set the field 'Build Action' to 'Embedded Resource'. Default it is set to 'Content', which makes the icon file appear as a separate file in the bin/output folder.
Then load the icon from the assembly/executable file at runtime by code. This is slightly different code than the idea Jonas probably meant to link (as he refers to resources in the project properties).
Usings for code below are
using System.Drawing;
using System.IO;
using System.Reflection;
//note: this = the current windows form I'm setting the icon for, in my case.
var assembly = Assembly.GetExecutingAssembly();//reflects the current executable
var resourceName = $"{this.GetType().Namespace}.{this.ProductName}.ico";
//note: this.ProductName reflects the executable's Product name in my solution (see current project, right click it, then click: properties - application - assembly information)
//reading the source / icon file, in this case from the assembly (as the icon is embedded in here), but the icon can be loaded from anywhere at this point.
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
this.Icon = new Icon(stream);
}
And to have a correct icon displayed on the shortcut or taskbar, make sure the icon file contains several sizes (variating from 48px by 48px to 256px by 256px). Else it shows a resized/upscaled icon.
Set icon on application property level
At last, set the icon for the project by clicking right on the project (in solution explorer) and then click: properties - application. Here at 'Resources' pick the .ico file in your solution. As it's embedded in the assembly this way, it won't need a copied version in the bin / output folder. But I assume the output assembly now includes two icons, one as embedded resource and one somewhere hidden in the assembly's bytes... as leaving out one of them breaks icon displaying functionality showing a default icon or not loading the embedded resource icon. The benefit of this answer is, as input, there is only one icon file, which is best for maintaining it.
Possible glitch on your machine is icon caching by Windows
Possibly, the machine's icon cache can be an issue. This mostly is occurring when developing the application and switching icons, displaying a former/previous icon. Search the internet for "reset icon cache windows", or this example here https://www.windowscentral.com/how-reset-icon-cache-database-windows-10.
You have to add your icon as a resource.
See: https://msdn.microsoft.com/en-us/library/7k989cfy(v=vs.80).aspx
When you've added it as a resource you can point to it.
Assuming that icon.ico is in your project folder, edit your .csproj file:
<PropertyGroup>
<ApplicationIcon>icon.ico</ApplicationIcon>
<PropertyGroup>
<ItemGroup>
<Resource Include="icon.ico" />
</ItemGroup>
That's it.
Related
I'm new to Xamarin.Forms and I am finding it impossible to change the application's icon for my app.
I used a cross-platform template in VS2017. I changed all the icon files in #mipmap-XXXX. When I run in the emulator the app icon changes but not on my device.
Does anyone have a proven method for getting this working. I'm sure it's possible but I can't find any information that helps.
If you're creating projects using the newest VS/Xamarin update, you should notice a folder in the Android project (under Resources) called mipmap-anydpi-v26, or similar. In this folder, you will find two XML files: icon.xml and icon_round.xml. If you open these files, you'll notice that they reference :
launcher_foreground - your icon image.
launcher_background - background color of your icon, mainly used for round icons.
When you are assigning the app icon and you reference icon, you are actually referencing the XML file, not the images. The XML file then retrieves the correct image size from the other mipmap folders.
Thanks both for your comments. However, neither completely solved the problem. In the end I looked in the file system and removed all icon.png images. I cleaned and rebuilt the app and nothing changed when I deployed it. I then repeated the above on all the launcher_foreground.png files and replaced them with appropriately sized ones. Now my application icon is what I wanted.
I seems that even though there is a icon.png file in each of the mipmap-xxxx folders, it is the launcher_foreground.png file that holds the application icon.
I'm trying to use a single .ico file (with multiple sizes) for both the Application executable and a form inside the application, without including the icon in the executable twice.
I noticed this because my app (without icons) is 600KB compiled, and the icon is 300KB, but when I use both the app increases to 1200KB compiled, indicating it's being embedded twice.
Here's what I've tried:
(1) Pick icon files using UI
Go to Application Properties > Application > Resources > Icon and use the "..." button to pick MyIcon.ico file.
Compiled exe is now 900KB
Go to Form Properties > Icon and use "..." button to choose MyIcon.ico file.
Compiled exe is now 1200KB
(2) Use resource
Go to Application Properties > Resources > Icons > Add existing file and pick MyIcon.ico file
In form constructor, add: this.Icon = Properties.Resources.MyIcon;
Compiled exe is now 900KB
Go to Application Properties > Application > Resources > Icon, and choose Resources\MyIcon.ico (which is listed in the drop-down)
Compiled exe is now 1200KB
Clearly, it's still including the file a second time, not referencing an embedded resource.
(3) Use Icon.ExtractAssociatedIcon()
Go to Application Properties > Application > Resources > Icon and use the "..." button to pick MyIcon.ico file.
Compiled exe is now 900KB
In form constructor, add this.Icon = Icon.ExtractAssociatedIcon(AppDomain.CurrentDomain.FriendlyName);
Compiled exe is still 900KB, but icon is the generic "exe" icon from Windows, not my application's icon
Before I go deeper into this, am I missing something obvious? Is there a standard way to do this? Am I just not using Icon.ExtractAssociatedIcon() properly?
I'm sorry, this is my oversight. Method (3) does actually work.
I was running this from the VisualStudio debugger, but didn't notice the .vshost.exe file gets a different icon -- which is what was showing up in the form.
When the compiled exe is used directly, it works fine.
After adding the necessary icon into the project you have to do 2 things:
Select Form and in the "Properties" select that icon in the "Icon" property.
Go to the Properties of the project and in the Icon field select the same icon.
When you build the project you will see necessary icon everywhere.
UPDATE:
Sorry not full answer and here we go:
You were right, it creates it twice, first one is ico file itself, the second one is base64 string in Form.resx file that generates when you add ico in the first step. So, how do fix it
First of all go to InitializeComponent() method, fine this.Icon string and change it to the following
this.Icon = new Icon(Path.GetDirectoryName(Application.ExecutablePath) + "../../../[YOUR ICO FILE NAME].ico");
, then delete autogenerated Form.resx file.
Icon object here is System.Drawing.Icon. The file name I set for example, as you see it is the ico which is next to *.sln file. In the real case, it will be without going to parent folders.
I am using VS 2005 for a windows application.
There have been code added in the UI form in past such that the form does not open in designer anymore.
I now have to add an icon in an System.Windows.Forms.ImageList.
Right now it simply adds images from the resource file in the InitilizedComponent() in the following way which I believe was originally generated by the designer.
this.MyimageListToolbar.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("MyimageListToolbar.ImageStream")));
In order to add my new icon I can simply say
Icon myIcon = new Icon(#"mypath/myicon.ico");
this.imgLstToolbar.Images.Add(myIcon);
but the problem then is that I would have to include this ico file in the setup project so it gets copied when this sofware is installed. This is not desirable. All other images added on the imagelist tool bar are not copied when the application is installed.
Is there a way I can programatically add the icon or add the icon without using the designer and still have it included in the resource file so I dont have to copy the ico file in order to run the exe.
Thanks,
You can add the icon to the "resource file" like so..
Right click on the folder "Resources" -> "Add" -> "Existing item..."
I have application that produces files. I would like to connect those files with application so that double-click on file launches my application.
Everything works properly except for annoyance that icon looks same like icon of my application. I would like to have one icon for application and one icon for it's documents.
DefaultIcon value in registry requires "app.exe,1" syntax where 1 is icon index. It seems that .NET resources are not Win32 resources.
I tried following but without success:
Selecting "Embedded Resource" as
file's build action.
Adding icon to resource file.
Only thing that worked is having icon file separated from executable and pointing to it. However, that seems to me like surrender.
Any ideas how to solve this?
Have you tried setting 2 as the icon index?
EDIT:
I found a way but you have to do it again for every new build.
Open the .exe file in Visual Studio (File -> Open File)
Right click the icon folder and select Add resource
Click the button Import
Select your .ico file
You might have to mess with the icon numbers, I think the lowest number (example 101) will be the application's icon
Remember the new icon number and set it as the index
EDIT 2:
Try this article: http://www.codeproject.com/KB/dotnet/embedmultipleiconsdotnet.aspx
If you are using the Wix toolset (http://www.wixtoolset.org) to install your application, you can get Wix to take care of the file type and document icon registration
Here is my magic Wix incantation:
<!-- Your executable -->
<File
Id="MaAppExe"
Name="MyApp.exe"
Source="MyApp.exe" KeyPath="yes" >
</File>
<!-- your document icon -->
<File
Id='IconMyAppDoc.ico'
Name='IconMyAppDoc.ico'
Source='$(var.MyApp.ProjectDir)\Resources\Images\Icons\docicon.ico' />
<-- File Extension and associated document icon -->
<ProgId
Id='MyApp.MyAppExtension'
Description='My App Data File'
Icon='IconMyAppDoc.ico'>
<Extension
Id='MyApp'
ContentType='text/plain'>
<Verb
Id='open'
Command='Open'
TargetFile="MyAppExe"
Argument='"%1"' />
</Extension>
</ProgId>
</Component>
DefaultIcon will also accept a path to a valid .ico file as an Icon.
You have to make sure that you have Win32 icons in your project not just .net icons. I HOPE that someone points out an easy way to do this, but in the mean time, here goes...
Compile your assembly, then from Visual Studio select "File -> Open -> Open File", open the compiled assembly. Add the icon you want to use for documents and set its ID to something above the one in use for your app. Save the assembly. Now you have Win32 resources available.
-- Edit --
After editing his post ZippyV appears to have a very good answer.
Had a basic WinForm question: By default a resx file is created for every form or user control (along with the designer.cs). This resx works fine for all the controls and the text added to the controls via the UI.
I was wondering if I could use the same resx to add strings which have to be used programmatically and based on conditions, attached to the controls? Will the resx get overridden in any case and this custom strings be removed?
What is the best practice to follow in this case?
There's a strange problem with the string resources in the Resources.resx file. There's no obvious way that I ever discovered how to create a new resource table for another language with the IDE. It can be done by hand though. Follow these steps:
Project + Properties, Resource tab,
add the strings you want to use in
your program
Start Windows Explorer and navigate
to your project's Properties folder
Copy and paste the Resources.resx
file
Rename it to the culture you want to
use. For example:
Resources.fr-FR.resx
Back to VS, click the Show All Files
icon in the Solution Explorer window
Expand the Properties node, the new
resource file should be visible
Right-click it and select "Include
in project"
Select it, in the Properties window
set Custom Tool =
"ResXFileCodeGenerator"
Verify that Build Action is set to
"Embedded Resource"
Build your program. You should get a new folder in your project's bin\Debug directory with the satellite assembly, named projectname.resources.dll. This satellite assembly contains both the localized strings and any localized resource from the forms. Test that it works with code like this:
public Form1() {
System.Threading.Thread.CurrentThread.CurrentUICulture =
System.Globalization.CultureInfo.GetCultureInfo("fr-FR");
InitializeComponent();
textBox1.Text = Properties.Resources.String1;
}
The auto-generated ones get overwritten (I'm using 2005), so no I would not use the same file. I would suggest creating a separate area in your project for .resx files like this. Usually I create one .resx per form, matching the name, of course.
Edit: Here is a more detailed answer I gave recently to organize the file/folder structure when localizing your app with .resx files.
Or you can try this:
Add this code in main();
using System.Resources;
ResXResourceWriter rw = new ResXResourceWriter("Resources.de-DE.resx");
rw.AddResource("String1", "de");
rw.Close();
...
repeat for more files
Go to the bin directory and move the XML (resx) file(s) to perhaps the property folder.
Go to Step 5-9 above in nobugz post.