Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Change Image URL's Assembly At Runtime

Change Image URL's Assembly At Runtime

Scheduled Pinned Locked Moved WPF
wpfcsharpquestionannouncement
2 Posts 2 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I have a WPF that's branded four ways, say App1, App2, App3, and App4. I change the name of the output assembly like this:

    App1
    App2
    App3
    App4

    This results in the compiled assembly being named according to the brand. I have images all over the app like this:

    In some cases, in Release builds, the images fail to appear. From what I can see, in Release builds the source can't find the assembly name, so the image fails to load. I verified this by renaming all of the output assemblies in the tags above to "App1" and it works. So I've implemented code like this:

    private BitmapImage _MyImage;
    public BitmapImage MyImage
    {
    get { return _MyImage; }
    set
    {
    if (_MyImage != value)
    {
    _MyImage = value;
    RaisePropertyChanged("MyImage");
    }
    }
    }

    and

        private void SetupBranding()
        {
            var imageFolder = "";
    

    if BRAND_APP1
    imageFolder = "App1";
    elif BRAND_APP2
    imageFolder = "App2";
    elif BRAND_APP3
    imageFolder = "App3";
    elif BRAND_APP4
    imageFolder = "App4";
    #endif

            // Get the assembly name
            string assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
    
            // Form the path to the assembly's image folder
            var path = $"/{assemblyName};component/Images/Brand/{imageFolder}/My\_Image.png";
            MyImage = new BitmapImage(new Uri(path, UriKind.Relative));
        }
    

    This seems to work fine, but I have do do this anywhere I want to use brand-specific images. I'm wondering of there's a better way? Is there a way to handle hard coded pack URI such has:

    Source="/App1;component/Images/My_Image.png"

    One other concern is that, while most of this is done in the Windows and Views, there are some URI's specified in styles. Thanks

    In theory, theory and practice are the same

    L 1 Reply Last reply
    0
    • K Kevin Marois

      I have a WPF that's branded four ways, say App1, App2, App3, and App4. I change the name of the output assembly like this:

      App1
      App2
      App3
      App4

      This results in the compiled assembly being named according to the brand. I have images all over the app like this:

      In some cases, in Release builds, the images fail to appear. From what I can see, in Release builds the source can't find the assembly name, so the image fails to load. I verified this by renaming all of the output assemblies in the tags above to "App1" and it works. So I've implemented code like this:

      private BitmapImage _MyImage;
      public BitmapImage MyImage
      {
      get { return _MyImage; }
      set
      {
      if (_MyImage != value)
      {
      _MyImage = value;
      RaisePropertyChanged("MyImage");
      }
      }
      }

      and

          private void SetupBranding()
          {
              var imageFolder = "";
      

      if BRAND_APP1
      imageFolder = "App1";
      elif BRAND_APP2
      imageFolder = "App2";
      elif BRAND_APP3
      imageFolder = "App3";
      elif BRAND_APP4
      imageFolder = "App4";
      #endif

              // Get the assembly name
              string assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
      
              // Form the path to the assembly's image folder
              var path = $"/{assemblyName};component/Images/Brand/{imageFolder}/My\_Image.png";
              MyImage = new BitmapImage(new Uri(path, UriKind.Relative));
          }
      

      This seems to work fine, but I have do do this anywhere I want to use brand-specific images. I'm wondering of there's a better way? Is there a way to handle hard coded pack URI such has:

      Source="/App1;component/Images/My_Image.png"

      One other concern is that, while most of this is done in the Windows and Views, there are some URI's specified in styles. Thanks

      In theory, theory and practice are the same

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Use relative Pack URI's. [Pack URIs - WPF .NET Framework | Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/app-development/pack-uris-in-wpf?view=netframeworkdesktop-4.8)

      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups