WinForm C# programatically embedding an image in resx - c#

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..."

Related

Win forms icon of exe

I'd like to set icon of exe file, after publishing windows forms application on C# in Visual Studio.
What i do: Project->properties->icon->browse and setting the file.ico
rebuilding a project, publishing but nothing happends. published app still has deafult icon. What im doing wrong?
UPD: yes, i have read guide by microsoft and tried to add icon as resource. It takes no effect. (icon is valid and i also tried to use default generated icon from resource)
UPD2: I did everything correctly, but i tought that setup.exe file will have the icon. This where i was wrong. Programm will crieate icon, in folder with other programms.
Your .exe can't find the directory of .ico file. To fix this you can add .ico file in Resources -> Add Resource. Select the icon in Application -> Resources -> Icon and manifest -> select your icon.

How do I include an icon once that can be used for both the executable and a form?

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.

Adding an image to a project in Visual Studio

I added a folder to my project by right clicking on the project and adding a new folder. Now I added the image to the folder (using copy paste in Windows File Explorer), but the solution explorer is not showing my added image. I did refresh the solution as well.
Also, in that folder, there is no option of adding an image, only Visual Studio files (new items etc)..
Why isn't Solution Explorer showing my image?
You just need to have an existing file, open the context menu on your folder , and then choose Add => Existing item...
If you have the file already placed within your project structure, but it is not yet included, you can do so by making them visible in the solution explorer
and then include them via the file context menu
You need to turn on Show All Files option on solution pane toolbar and include this file manually.
Click on the Project in Visual Studio and then click on the button titled "Show all files" on the Solution Explorer toolbar. That will show files that aren't in the project. Now you'll see that image, right click in it, and select "Include in project" and that will add the image to the project!
If you're having an issue where the Resources added are images and are not getting copied to your build folder on compiling. You need to change the "Build Action" to None from Resource ( which is the default) and change the Copy to "If Newer" or "Always" as shown below :

Cannot load project by opening .csproj

Sorry, i am new to VS... :'(
I am currently using VS2010. When i am trying to double-click a .csproj to load a project, it just load a XML instaed of all the associated .cs / properties / reference files. I did previously can load other projects with .csproj. So i am wondering if it's related to the project itself.
Any way I can spot/check the reason/root-cause of it? (from the XML file loaded?)
Is it because the project is previously developed using other older VS, like VS2008 / VS2005?
Thanks.
Maybe you have accidentally changed the default file association of your .csproj files. Go into the Control Panel -> Default Programs -> Set Associations to see what application is set to open the file and change it to Visual Studio.
Click Start, and then click Control Panel.
Click Folder Options.
Click File Types tab in Folder Options window and the full file
types and their association will be listed.
Click New and type the File Extension in the box and then click
Advanced.
Choose an association for the file type.
Click OK and then Click Close.
Open the file again

Images folder in WinForms application

What's the way to add custom images/icons in WinForms application.
I added a folder (right-click project and add new folder) named it then as my images folder and I would like to use this folder as my main images folder.
I don't see any option to use my 'images' folder in Visual Studio after dropping the Picture Box control.
Add the image as an embedded resource. You can the set the PictureBox's Image property to the resource via the property editor or at runtime by accessing the Properties.Resources object. The images will be compiled directly into your executable, you just need to add them to your project.

Categories

Resources