I fail to access Resource image file from code in

2019-02-18 10:35发布

问题:

I have some image resources in my app i want to access programaticly.

Now i'm based on some source code so i need to acess via Uri so my code is :

new BitmapImage(new Uri("pack://application:,,,/YearBook;component/Resources/Output/" + i.ToString() + ".jpg", UriKind.RelativeOrAbsolute));

though when i try to access it i get error that resources not found ( and the name is right)

Anyone knows why?

EDIT:

Might be i needed to add some assembly in the assembly file?

回答1:

In the end with all types of ways to approach this resource, my problem was i forgot 1 folder in the sub folders.

This code eventually worked :

new BitmapImage(new Uri("pack://application:,,,/Resources/Images/Output/" 
+ i.ToString() + ".jpg", UriKind.Absolute)));


回答2:

StreamResourceInfo resource = Application.GetResourceStream(
new Uri("SilverlightApp1;component/embedded.png", UriKind.Relative));

SilverlightApp1 is assembly name,component is keyword .You will get your resource and then cast it to appropriate type, Hope this will help.



标签: c# wpf resources