0

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?

3
  • 3
    You should use resources and pack URIs. This embeds the file into the assembly instead of loading it from disk. Commented Oct 29, 2013 at 21:13
  • That seems to go against the advice in stackoverflow.com/questions/3442000/…. Also, I tried following that advice, using a line similar to playIcon.Source = new BitmapImage(new Uri(@"pack://application:,,,/TempApplication2;images/mona.jpp"));, but I don't really know what name to use to replace TempApplication2; all the suggestions say "where you fill in the name of your assembly", but is that ImageApp? Or is it some other name? And if it's another name, how would I know what it is? Commented Oct 29, 2013 at 21:36
  • That advice is exactly what I'm saying. The assembly name can be found in Project Properties, and is usually the same as the project name. Commented Oct 29, 2013 at 21:47

1 Answer 1

1

I'm not sure (can not check right now), but try to specify something like this

new Uri("/ImageApp;component/images/mona.jpg");

And ensure you have ImageApp in references

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.