How do I get rid of the MissingManifestResourceException in WPF? - c#

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

Related

Embed Multiple Icons in .Net Application Executable

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?

Use solution-wide resources with WinForms

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

Windows forms ImageList - Add images with Relative path - No file copy

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.

Place Visual Studio resource files in a folder other than 'Resources'?

I have a resource file named rs.resx. In the Visual Studio designer, I can add an image to my resource file by clicking "Add resource" and specifying the path to my image file.
After adding the image, the image file itself is also copied to a folder in my Visual Studio solution named Resources. I would like all of my image files to be placed in a folder named Images instead. Is this possible?
This is a little tricky, but it is possible.
VS checks if the file added to a resource is already defined somewhere within your project. If it can't find it, it creates the folder Resources, puts a copy to the file there, adds this file to the project and puts a reference into the resource designer to this fresh copy of your file.
To avoid this behaviour you should add the file to your project before you add it into the resource file. If the file isn't somewhere within your project structure you can just create a folder, right click it, select Add file and before you click on the Add button of the OpenFileDialog, push on the little arrow next to the button and select Add as link.
Now the file resides on the place on your hdd wherever you like and the resource designer doesn't create a copy within your project file if you now add the file within the resource designer.
Maybe this little picture helps to find the Add as link button:
(source: modbusdriver.com)
That's just a subdirectory of your project directory. Your program doesn't use it at runtime, it should use the embedded resources. Anything you add to the .resx file gets copied there, not just images. But you can rename the folder if you really want to, right-click it and click Rename.
Instead of adding a .resx file to your project, I'd recommend you use the existing one. Project + Properties, Resources tab. Makes it very easy to retrieve the resource at runtime, just use Properties.Resources.Something in your code.

Loading Content images from Blend from separate project

I have a VS2010 solution with 5 projects. Two of these projects are called:
MyResources
Application
The Application project contains all of the views and viewmodels for the application to run, and the Resources project contains all the images, resource dictionaries, etc for theming and skinning.
The way I have images set up in Resources, I have their build action set to Content, and Copy set to Copy Always. In my Application project, I reference these files using a pack URI. Example:
"pack://siteoforigin:,,,/Themes/DefaultTheme/BackgroundImage.png"
This works fine for me. The themes folder and all its subfolders are properly copied to the proper folder since I'm using siteoforigin://.
However, this breaks Blendability. When I load the project in Blend 4, I get errors.
If I use siteoforigin, Blend tries to load the images from the executing folder of blend.exe (In this case, C:\Program Files\Microsoft Expression\Blend 4 Beta\Themes\Default_Theme\BackgroundImage.png")
If I change the pack URI to the following:
pack://application:,,,/IQ.IQKiosk.Resources;component/Themes/DefaultTheme/BackgroundImage.png
It tries to look for the image in "themes/default_theme/backgroundimage.png"
If I try:
/MyResources;component/Themes/DefaultTheme/BackgroundImage.png
It tries to look for the image in "C:\MyResources;component\Themes\DefaultTheme\BackgroundImage.png"
If I try:
/Themes/DefaultTheme/BackgroundImage.png
It tries to look for the image in "C:\Themes\DefaultTheme\BackgroundImage.png".
So now I'm stuck. I'm not sure how to reference my images without having to embed them into the resource project, and I can't hard code the directory because my other coworkers have the project in different folders as well.
I tried to think of a way to get the proper URI to the proper locations of the files (relative to the .sln file would be awesome) or have the files get copied over to where blend compiles and executes the project. Alas, I am unable to figure out how.
Does anyone know the proper way to get these images to load?
I figured it out. Instead of using a pack URI, I just used
new Uri(Environment.CurrentDirectory + "\\Themes\\DefaultTheme\\BackgroundImage.png")
and it found the proper folder, since Evironment.CurrentDirectory gave me the proper bin folder where my themes were copied to.
Huzzah!

Categories