I'm trying to embed multiple .ico files (with multiple sizes) for an Application executable in Visual Studio 2019.
Until now I have added a single .ico file to the project root and set it as the application icon to use via Project -> Properties -> Application referenced it under Resources, Icon and manifest.
I read somewhere that it would be possible to embed multiple icons by adding them to a resource file so I created a new resource file from Project -> Properties -> Resources and added two existing .ico files. I then selected this resource file (.resx) instead of a single icon under Project -> Properties -> Application.
However, when trying to bild the application I get the following error:
Error CS1583 Error reading Win32 resources -- Image is too small.
Even if I only add a single icon (the same that was used to begin with) to the resource file I get the same error. It is curious since that icon worked when setting it directly in the Project -> Properties -> Application dialog.
Is this the wrong approach for embedding multiple icons or might there be something else going on?
Related
I have a Visual Studio 2017 solution, which is split in several C#-WinForm applications.
To have a similar user interface I put all the icons/pictures into one project (ResourceProject) and marked the resource file public.
By directly editing the .Designer-file of a Form I can now explicitly assign the Image to something from the resource file of the ResourceProject by stating
button1.Image = ResourceProject.Properties.Resources.DemoPic
However, from time to time, the designer overwrites this with
button1.Image = ((System.Drawing.Image)resources.GetObject("button1.Image")));
and creates a local resource file, which has the image embedded.
This is exactly what I wanted to avoid in the first place, since I might want to alter easily some icons later on and have those changes directly spread over all projects.
The only way to avoid it seems to be to assign the images not in the Designer file but in some other method in the actual form file.
Is there any other way around this problem?
You should not change designer.cs file, it's auto-generated and any change which you make in the file will be replaced with auto-generated code the next time which you change something in the form.
But to share image resources between multiple projects in a solution with design-time support, you can follow these steps:
Create a class library project, let's call it ResourceLibrary.
Add a Resx resource file to the root folder of the project with this name Resources.Resx.
Open the resource designer and change its Access Modifier to Public. (It will set its Custom Tool to PublicResXFileCodeGenerator)
Add a few images to the resource designer and save it.
Then in the Windows forms project do the following settings:
Add a reference to ResourceLibrary.
Right click on windows forms project and choose Add → Existing item...
Browse to the ResourceLibrary folder and choose Resources.Resx file.
Click on drop-down arrow of the Open button, and choose Add As Link.
Select Resource.Resx which has added to windows forms project and choose properties.
Set its Build Action to None
Set its Custom Tool to a text like None
Set its Custom Tool Namespace to the namespace of the resource in the other assembly: ResourceLibrary.
Rebuild the project.
Then for all the image properties you can choose the other resource file from drop-down in the Select Resource dialog. The code generation will generate correct code for the property and you have a correct build and it works as expected at design-time as well as run-time.
You can clone or download a working example here:
Repository
Download
I have two assemblies in my application one is where all the code is, the other holds all the images and extra resource files. It was working just fine until I added new images to the resource dll. Now it keeps coming up with MissingManifestResourceException and saying it can't find certain images. All my images are being compiled as Resources.
Fix this problem as right-click the Resources.resx file in the Solution Explorer and click Run Custom Tool. This re-generates the auto-generated Resources.Designer.cs file.
If the .resx file was added to the project manually, the Custom Tool property of the file must be set to "ResXFileCodeGenerator".
Rebuilt that project and then run it
I'm trying to add images to my tree nodes (ImageList.Add()), but just can't figure out a nice way of doing it.
I've read from MSDN help I should use System.Drawing.Image.FromFile(path). But cannot just get a file somewhere.
I'm building a DLL, and want it to be a single file, no bitmaps being copied together with it.
So I've read I should add Image files to the project and mark them with Build Action as "Resource".
Ok, but where do I get them??? I saw people using it in XAML files, but I don't have that.
Saw people using Resources.SomeName, but can't find those Resources class.
So....How do I do it?? I've got the files marked as resources, just need to add them to the ImageList.
By the way, I'd love to use the path relative to the Code File that is adding the images to the ImageList. But if not possible, just relative to the assembly root.
If you want to use file paths, for items that are in your project, you must set the "Copy to Output Directory" property to "Copy Always" or "Copy if newer", otherwise it won't be in the bin folder, and then you'll be trying to pass a path to a file that doesn't exist. Build action isn't all that important in this scenario.
If you want to use compiled resources, and reference them via the Resources object, see the rest of my answer. I assume you are using Visual Studio, 2005 or later.
To add an image as a compiled resource to a clean Windows Forms project, so that you can access it via Resources.SomeName do the following:
In Solution Explorer, under the windows forms project (mine is called WinFormsApplication1), expand the "Properties" folder. By default this folder should contain: AssemblyInfo.cs, Resources.resx, Settings.settings.
Double-click on the Resources.resx file. This will open an editor for it. You'll probably see a table of strings, with columns "Name", "Value", "Comment".
Click the drop-down arrow on the "Add Resource" button, and select "Existing File", which will allow you to browse to the image you want to add.
You should now see the image appear in a gallery of sorts. Mine has the name TestImage
Now when you edit the code (mine is Fom1.cs), I can access the image as a System.Drawing.Bitmap as Properties.Resources.TestImage.
To my mind, this is the best way to do images that you want compiled into the application. If you want user-added images, you'll need to use OpenFileDialog, or something like that to get your file path. Then the Image.FormFile() will be what you want.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to embed a text file in a .NET assembly?
I have a c# winforms application that use several text files.
I would like to somehow embed those files either into the exe or dlls, so that they cannot be seen by the user when the application is deployed.
Can visual studio do something like that?
With text files you can also proceed as following:
Double click your Properties -> Resources.resx file to open the resource designer.
From the first small drop down on the top, select "Files" as resource type.
Click on Add Resource -> Add Existing File... and select your text files.
Let's say that the TXT you added was called Foo.txt... you can just access it's content like so:
String foo = Properties.Resources.Foo;
Then you can also change te file names inside the resource designer and they will be automatically refactored in your code. From the Resources directory, where they are now located in your project, you can also edit them.
I just recommend you to keep their access level as internal.
Here's how I do it:
In the solution Explorer, right-click on Properies/Resources.resx.
Choose "View Designer".
You can use the "Add Resource" button, but I often just drag the file right onto the designer window.
Visual Studio will perform code generation, such that you can access your file using Properties.Resources.YourFileName.
Be wary: Visual Studio will copy your chosen file into your project's Resources folder. It will not create a link to wherever the file was originally stored.
I have five projects in my solution. Each of them uses some icons (many of these icons are the same in the different projects) and I want to store them in one place.
I tried to use separate project for storing icons, but I got fiasco..
When I use icon from other project (project which contains all icons) Visual Studio automatically copied this icons to the form.resx file.
Explanation:
By the first I can't attach icon (from other project) to my button from designer. Therefore I must to go to the form.designer.cs and manually attach the icon to the button. After these operations VS rebuilt the auto-generated part in form.designer.cs replaced my code with own and copied the icon to the form.resx file.
//my code
this.btnCompile.Image = SharedProject.Properties.Resources.compile;
//code replaced with VS
this.btnCompile.Image = ( (System.Drawing.Image)( resources.GetObject( "compile.Image" ) ) );
You can see that image was copied to local project to the resources. In this case to change button's image is insufficient to replace only my image (compilation.png) in shared project. I need to replace also it in each form where it used.
Question: How to manage icons in solution to avoid duplication and this big inconvenience?
I've solved exactly the same problem but with one limitation. Let's see two possibilities:
1) You want to have ability to update all images by overriding one folder with images in app folder.
You can use Service as #Farzin Zaker suggested. The drawback of that solution is you have to dynamically set up images to controls and in design mode you will not see control with correct images.
2) You want to use Designer.
In that case you have to add those images as resources. The trick is to make VS use relative path to images from the specified folder. By default VS copies all resource files to project Resources folder. But if VS sees that in Resources folder "registered" other file with the same name VS stores in .resx relative path to image. So If you want to add the same image in two project you have to:
- Create folder Resources
- Add image as existing item (as link): in VS you will see the link to image, set action to None
- Add image to project resources section.
You can design a Service project witch will serve Icons or other resources for your other projects. you should just implement an interface like this in your application :
public interface IResourceProvider
{
Image RetrieveIcon(string key);
//you can add other resource types here.
}
then implement this interface in a new class and return related Icon to each key passed to its methods. here you need to browse resx file of this project for requested Icon (Resource)
then in you application you should just call your ResourceProvider class to priovide required resource Items.