Wpf – Loading BitmapImage in code

bitmapimagewpf

From my assembly (A) I want to call a method in another assembly (B) which passes an image. This image is then shown in a WPF Window – the window is part of B's project.

I can't seem to pass an ImageSource with a pack:// uri as this gets evaluated in the context of B, so I guess I need to cache the image using CachedBitmap (?) when still in A.

        BitmapImage img = new BitmapImage(new Uri("Images/32px-Nuvola_apps_cache.png", UriKind.Relative));
        CachedBitmap cbmp = new CachedBitmap(img, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);

I've managed to get this to work if I set the image to Embedded Resource and load it as a stream, but this isn't the WPF way.

It seems from the pack: documentation that I should be able to do this, but I've tried these below and none work;

"Images/32px-Nuvola_apps_cache.png": "Could not find part of the path"
"pack://application:,,,Images/32px-Nuvola_apps_cache.png": "The URI prefix is not recognized.".
"pack://siteoforigin:,,,Images/32px-Nuvola_apps_cache.png": "The URI prefix is not recognized."

All I want to do is load a Resource .png file into memory and pass it wholesale to a method in another assembly.

Thanks
Paul.

Best Solution

Try:

pack://application:,,,/YourAssemblyName;component/Images/32px-Nuvola_apps_cache.png
Related Question