I'd like to load images into a WPF project with a short pathname. Details follow.
I've got a WPF solution that looks like this:
Solution 'GB" (16 projects)
ABApp
Properties
References
... ...
ImageApp
Properties
References
images
mona.jpg
App.xaml
App.xaml.cs
Window1.xaml
Window1.xaml.cs
I want to look at the ImageApp project.
The App.xaml file looks like this:
<Application x:Class="GB.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
</Application.Resources>
</Application>
and App.xaml.cs simply defines the class App to derive from Application.
The Window1.xaml file constructs a UI, and then Window1.xaml.cs builds a drawing area in which various image stuff will be displayed.
Inside that C# file, to load up the image images/mona.jpg that I want to display, I end up writing something like this:
Image myImage;
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.UriSource =
new Uri("../../images/mona.jpg", UriKind.RelativeOrAbsolute);
myBitmapImage.EndInit();
//set image source
FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap();
newFormatedBitmapSource.BeginInit();
newFormatedBitmapSource.Source = myBitmapImage;
newFormatedBitmapSource.DestinationFormat = PixelFormats.Bgra32;
newFormatedBitmapSource.EndInit();
bmpSource = newFormatedBitmapSource;
myImage.Source = bmpSource;
BuildRenderTransform();
(I apologize for that code -- it's a kind of simplification of what's really going on, so it may not be letter perfect.)
My question is about the Uri: I have to write ../../ at the front of the Uri so that the executing program (which ends up in ImageApp/bin/Debug) knows it has to go up two levels and then down into the images folder. Is there some way that I can just write something like new Uri("images/mona.jpg", ...) instead? Can I tell WPF that the source directory should be used as the starting path for relative searches?
I confess, the whole Uri thing is mostly baffling to me, despite my having read the microsoft help articles and several StackExchange questions/answers..
I have the suspicion (and vague memory) that by adding something to the xaml for the project, I can make this happen, but I just can't seem to do it.
Any ideas?
playIcon.Source = new BitmapImage(new Uri(@"pack://application:,,,/TempApplication2;images/mona.jpp"));, but I don't really know what name to use to replaceTempApplication2; all the suggestions say "where you fill in the name of your assembly", but is thatImageApp? Or is it some other name? And if it's another name, how would I know what it is?