0

There are two assemblies there. For example A and B. The images are situated in the project A: /Resources/images/Question.png. When I call Window of assembly A from its project, - everything is OK. The picture is there.

When I call Window of assembly A from assembly B. -Window itself is OK. The picture is missing.

Is there a way to solve this issue?

 private void SetImage(string imageName)
    {
        string uri = string.Format("/Resources/images/{0}", imageName);
        var uriSource = new Uri(uri, UriKind.RelativeOrAbsolute);
        img.Source = new BitmapImage(uriSource);
    }

thank you.

  • Where I have to put this if I use this method? See update. – DeputyOfCopyPaster Oct 17 '18 at 16:32
  • Duplicate of https://stackoverflow.com/questions/350027/setting-wpf-image-source-in-code/1651397#1651397. – Clemens Oct 17 '18 at 16:52
  • The problem is that you are loading the image from another thread. You need to freeze the image first, so it can be used across threads. Look for "how to freeze an image in WPF". – Unknown Coder Oct 17 '18 at 21:28
  • 1
    @ASilva How do you know that there is another thread? The *actual* problem is that in code behind you have to use a full Pack URI, including the referenced assembly name. – Clemens Oct 18 '18 at 07:29
  • @Clemens I did't tough about that. I guess you're right. :-) – Unknown Coder Oct 18 '18 at 07:50

1 Answers1

1

Add the name of the referenced assembly to the image's Pack URI:

private void SetImage(string imageName)
{
    var uri = "pack://application:,,,/AssemblyName;component/Resources/images/"
            + imageName;

    img.Source = new BitmapImage(new Uri(uri));
}

Replace AssemblyName by the name of your assembly that contains the image resource.

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • "AssemblyName" which assembly name? A or B. – DeputyOfCopyPaster Oct 17 '18 at 16:40
  • by the way, I always try to rate answers which were helpful. But this page always responds something like this "It's not enough reputation or so". Anyway please give me an advice, how to accept helpful answers. – DeputyOfCopyPaster Oct 18 '18 at 10:16
  • Just click the check mark on the left, below the up-/downvote buttons. – Clemens Oct 18 '18 at 10:32
  • Oh..check mark. I've got you. Could you look at this topic? https://stackoverflow.com/questions/52855899/message-cannot-find-resource-named-messageboxbuttonstyle-resource-names-are-c?noredirect=1#comment92643167_52855899 – DeputyOfCopyPaster Oct 18 '18 at 10:39