Posted on

Retrieve resources in c# Windows Forms .Net Application

Scenario

You have a library (dll) with one or more image and you want to use those image in your application.

Load Resource from application

Consider having a library called : myLibrary.dll with some class inside with Namespace mylibrary.

In your bapplication you can get to the resources by referencing the assembly :

Assembly assem = Assembly.LoadFrom("myLibrary.dll");

You can enumerate the resource files

string[] resNames = assem.GetManifestResourceNames();
if (resNames.Length == 0)
Console.WriteLine(" No resources found.");

foreach (var resName in resNames)
Console.WriteLine(" Resource 1: {0}", resName.Replace(".resources", ""));
foreach (var resName in resNames)
Console.WriteLine(" Resource 2: {0}", resName);

And this will be the output :

Resource 1: myLibrary.Properties.Resources
Resource 2: myLibrary.Properties.Resources.resources

The replace is necessary because the real resource name is the first one (without .resources)

Now, If you have an image called myBitmap.png in your resources, you can load it, using ResourceManager

ResourceManager rm = new ResourceManager("myLibrary.Properties.Resources",
assem);

and finally

Object myres = rm.GetObject(myBitmap);

Load Resource inside Library

To get resource from a class inside your resource library, defined a new class, for example myClass and reference own assembly :

rm = new ResourceManager("myLibrary.Properties.Resources",
typeof(myClass).Assembly);

At this point you can withdraw the resource as seen above.

Posted on

Visual Studio 2022 – programming on Framework 4.5

.NET 4.5 has long been out of support

To use .NET Framework via Visual Studio 2022 you have to download the nunget package :

https://www.nuget.org/packages/microsoft.netframework.referenceassemblies.net45

Rename the extension of the package in .zip to decomprime it.

Put the content in this Folder :

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5

You have to put at the first level the content of the folder

build\\.NETFramework\v4.5

that you can find inside the nunget