Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Loading image from different assembly

Tags:

I have an image in different assembly:

  • (.NET Standard 1.4) ResourcesAssembly/Common/Images/CompanyLogo.png - mandatory requirement
  • Build Action: Content - mandatory requirement
  • Copy to output directory: Copy if newer (I checked after a compilation - the required image is presented in output directory where my exe is located - for example, Debug/Common/Images/CompanyLogo.png. So there should be no problem to get it from there.)

I want to paste it in my app's assembly (WPF) inside a window. I try 2 variants.

1.

<Image Source="pack://siteoforigin:,,,/Common/Images/CompanyLogo.png" />

Image in interface is visible at runtime. But VS's XAML designer doesn't show the image at design time and says it is an error:

Could not find a part of the path 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Common\Images\CompanyLogo.png'.

2.

<Image Source="pack://application:,,,/ResourcesAssembly;component/Common/Images/CompanyLogo.png" />

Image is not visible at runtime, but at design time all are OK.

Visual studio 2017 Community Edition 15.4.4.

So the first variant seems suitable for me, but that strange error - why it tries to find the image in Visual Studio folder? "siteoforigin" option relates to the application exe, not to the Visual Studio exe, isn't it?

UPDATE

Tried the second variant with build action as "Embedded resource" (ResourcesAssembly is a .NET Standard 1.4 project). Even cleaned and rebuilt the solution. Result is the same as in the second variant: image is not visible at runtime, but at design time it is visible.

like image 617
Alex34758 Avatar asked Feb 11 '18 11:02

Alex34758


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


1 Answers

"siteoforigin" option relates to the application exe, not to the Visual Studio exe, isn't it?

Exactly, siteoforigin points to executing assembly directory. But when you use VS's XAML designer, executing assembly is ... XDesProc.exe from the directory you specified (C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE). That's the reason why the image search path is ...\IDE\Common\Images\CompanyLogo.png.

You could ask - "Is there a way to correcly dispaly the image in both WPF Designer and application runtime?" Well, if you want to keep both requirements (1st - image in external resource assembly, 2nd - build action for the image set to 'Content'), then probably no. At least I can't figure out the solution.

Option with siteorigin authority in pack URI is not suitable because the image should be loaded in different applications with different executing directory (see above).

Option with application authority in pack URI is not suitable because it works only for resource files that are compiled into assembly (local or referenced). That's actually the reason why you don't see an image at runtime with your 2nd variant.

So if the 1st variant suits you, then OK, it's the best you could have while not violating requirements you declared. The closest solution that allows to correctly see an image both in Designer and run-time is to set Build Action to Resource for an image and to use the 2nd source path with application authority.

UPDATE

"Resource" build action is not available for .Net Standard class library. I haven't found any info whether it will be ever supported. For this moment, if your ResourcesAssembly must target .Net Standard, your best option is to use first variant you described in the question.

like image 121
CodeFuller Avatar answered Sep 19 '22 13:09

CodeFuller