0

I have a library to which I have added images. I want to get these images in order to display them in the WPF Image(System.Windows.Control.Image). I have used several methods.

<Image Source=/MyLibrary;Component/Resource/image.png"/>

With this method, I get the following error: Failed to upload a file or assembly MyLibrary If I use

<Image Source="pack://application:,,,/Resource/image.png"

Then I get an image that is displayed in the UserControl, but not on the form. The structure of the project is as follows: The project contains several libraries. 2 of them are not used and I use only 3. In the third library I have a Resource folder with images in it. Also in this library is the WindowsForm project to which I add my UserControl in which I use WPF and all the methods for working are written in it. This library is a plugin. In it, through a third-party application for which a plugin is being written, I launch a Form in which UserControl.

How to request an image from project resources in xaml. Images have been added to the resources of the project itself.

I tried to add images via C# code when initializing the project. But I can't assign the value of system.drawing.bitmap to a variable of type system.windows.controls.image

This is my first appeal, so it may be a bad word. I really hope for a solution. Thank you in advance for reading the problem.

  • _"...Also in this library is the WindowsForm..."_ - that's part of the problem: WPF has its own (unique, and _uniquely annoying..._) resources system called _XAML Resources_ which are unrelated and incompatible with .NET _RESX Resources_, .NET Assembly Manifest Resources, and "native" Win32 _PE Resources_. – Dai Jun 21 '23 at 11:15
  • If you want to use .resx image resources in a WPF application you should store them as streams instead of `System.Drawing` instances. [This](https://stackoverflow.com/a/57099811/5114784) answer may help. And [here](https://github.com/koszeggy/KGySoft.ComponentModelDemo) is a live example for using the same .resx resources both in WinForms and WPF. – György Kőszeg Jun 21 '23 at 11:33
  • Or, you could just set your image as BuildAction=None and CopyToOutputDirectory=CopyIfNewer, and use a relative path when referencing the image in xaml. One thing I like with this method is that you can check if the image is copied correctly. You do not need to mess about with embedded resources if you do not want to. – JonasH Jun 21 '23 at 14:42
  • The image files in the library should have their Build Action set to Resource. Then you would load them by fully qualified Resource File Pack URIs that include the assembly name of the referenced library, as shown in the answer to the duplicate question. Btw, this has nothing to do with "*XAML Resources*". – Clemens Jun 21 '23 at 14:51

0 Answers0