Search code examples
c#wpfimageresources.net-assembly

Using a resource image in a library different from the main project


I have a WPF program with two libraries

enter image description here

now inside I have a gray resource background both in the main program and HelperLib. But additionally in the HelperLib I have a red background I want to use.

enter image description here

Now when I want to change the background of a window with:

 switch (bubbleType)
  {
    case eBubbleType.ERROR:
      bw.btText.Background = new ImageBrush(new BitmapImage(new Uri("pack://application:,,,/Resources/Images/gradientWallpaper_RED.jpg"))); <-----I want to use this one 
      break;

    default:
      bw.btText.Background = new ImageBrush(new BitmapImage(new Uri("pack://application:,,,/Resources/Images/gradientWallpaper.jpg")));
      break;
  }

but I get an image not found exception on the RED image but not on the other one. I suspect that, when using the gray gradient it's not using the one in the lib but the one in the main program since I see that the resource is related to the assembly not to the project. Both images have the same properties as in picture:

enter image description here

Thanks for helping


Solution

  • You would have to use a Resource File Pack Uri with the name of the referenced assembly:

    new Uri(
      "pack://application:,,,/HelperLib;component/Resources/Images/gradientWallpaper_RED.jpg")