Accessing WPF Resources on .NET

Supported on .NET only, WPF Resources can be accessed via the System.Resources.ResourceManager class, as well as via special constructors on certain system classes that represent resources and take an URI as constructor parameter:

var lResourcePath := new System.Uri('pack://application:,,,/MyImage.png');
var lBitmap := new BitmapImage(lResourcePath);
var resourcePath = new System.Uri("pack://application:,,,/MyImage.png");
var bitmap = new BitmapImage(resourcePath);
let resourcePath = new System.Uri("pack://application:,,,/MyImage.png")
let bitmap = new BitmapImage(resourcePath)
var resourcePath = new System.Uri("pack://application:,,,/MyImage.png");
var bitmap = new BitmapImage(resourcePath);

The raw bytes of a resources can be accessed by passing the URI to the Application.GetResourceStream API:

var lInfo := Application.GetResourceStream(uri);
using lMemoryStream := new MemoryStream() do begin
  lInfo.Stream.CopyTo(lMemoryStream);
  var lBbytes = lMemoryStream.ToArray();
end;
var info = Application.GetResourceStream(uri);
using (var memoryStream = new MemoryStream())
{
    info.Stream.CopyTo(memoryStream);
    var bytes = memoryStream.ToArray();
}
let info = Application.GetResourceStream(uri)
__using memoryStream = MemoryStream() {
    info.Stream.CopyTo(memoryStream)
    let bytes = memoryStream.ToArray()
}
var info = Application.GetResourceStream(uri);
try (memoryStream = new MemoryStream())
{
    info.Stream.CopyTo(memoryStream);
    var bytes = memoryStream.ToArray();
}

Resources are represented by a URI, typically of the format pack://application:,,,/X, where X is the name of the resource file as referenced by the project, possibly including a path.

The System.Resources.ResourceManager class can be used to list available resources, as shown below.

var lAssembly := typeOf(self).Assembly;
var lResourceContainerName = lAssembly.GetName().Name + '.g';
var lResourceManager = new ResourceManager(lResourceContainerName, lAssembly);
var lResourceSet = lResourceManager.GetResourceSet(Thread.CurrentThread.CurrentCulture, true, true);
foreach (var lEesource in lResourceSet)
    yield lResource.Key;
var assembly = typeof(this).Assembly;
var resourceContainerName = assembly.GetName().Name + ".g";
var resourceManager = new ResourceManager(resourceContainerName, assembly);
var resourceSet = resourceManager.GetResourceSet(Thread.CurrentThread.CurrentCulture, true, true);
foreach (var resource in resourceSet)
    yield return resource.Key;
var assembly = dynamicType(self).Assembly
var resourceContainerName = assembly.GetName().Name + ".g"
var resourceManager = ResourceManager(resourceContainerName, assembly)
var resourceSet = resourceManager.GetResourceSet(Thread.CurrentThread.CurrentCulture, true, true)
for resource in resourceSet {
    __yield return resource.Key;
}
var assembly = typeOf(this).Assembly;
var resourceContainerName = assembly.GetName().Name + ".g";
var resourceManager = new ResourceManager(resourceContainerName, assembly);
var resourceSet = resourceManager.GetResourceSet(Thread.CurrentThread.CurrentCulture, true, true);
foreach (var resource in resourceSet)
    __yield return resource.Key;

Here, the "resource container name" is the name of the lower-level Embedded Resource that contains the WPF resources for the given assembly.