I am working on a 3D article in WPF, and I wanted to use an ImageBrush
but was having some issues with it, so I decided to split this out into a small test app.
I had the following setup:
And I simply wanted to test this out by creating an ImageBrush
that I could use to say change the Background
of a Button
. So I had the following code:
1:
2:
3: System.Windows.Media.ImageBrush brush =
4: new System.Windows.Media.ImageBrush();
5: BitmapImage img = new BitmapImage(
6: new Uri(@"images/image1.jpg", UriKind.RelativeOrAbsolute));
7: brush.ImageSource = img;
8: btnImage.Background = brush;
Now I thought this should work. Yet when I ran it, I got:
So I then tried to add an image to the code, so we now had:
1: <Window x:Class="ImageBrush.Window1″
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: Title="Window1″ Height="300″ Width="300″>
5: <StackPanel x:Name="sp1″>
6: <Button x:Name="btnImage" Margin="10″ Height="60″/>
7: </StackPanel>
8: </Window>
And code behind:
1:
2:
3: System.Windows.Media.ImageBrush brush =
4: new System.Windows.Media.ImageBrush();
5: BitmapImage img = new BitmapImage(
6: new Uri(@"images/image1.jpg", UriKind.RelativeOrAbsolute));
7: brush.ImageSource = img;
8: btnImage.Background = brush;
9:
10:
11:
12: Image img2 = new Image();
13: img2.Source = img;
14: sp1.Children.Add(img2);
This seemed to work for me. But I didn’t want an Image
, so that was out. I figured this has to be some trick that Image
can do that ImageBrush
can't. For ImageBrush
, I imagined that I needed to read from the Assembly resources. So I had a hunt about and found the following code over at Tamir Khason's blog.
Which allows us to do things like:
or for framework elements:
All of which is way cool. But it still didn’t sit right with me, so I asked my mate Josh Smith about this and he simply suggested changing the Build Action of the image to “Content
”. This did the trick, the spectacular results of which are shown here:
Brilliant hey. But beware, when we set a Build Action to “Content
”, You also lose the ability to localize that image. (Images built with a Resource build action are loaded via a ResourceManager
, which means they can be substituted with images in localized satellite resource assemblies.)
From here on this blog, we will be using some information that has been the result of a chat with Ian Griffiths.
In fact, it’s perfectly possible to use a compiled in resource in an ImageBrush
, you just need a more specific URI, so we could then use a URL like the following:
Where we use a fully-qualified pack URI. I called my test application ImageBrushAsResource
by the way – obviously you’d need to put your component’s name there in its place.
We can actually use a Relative path in XAML though for an Image
such as:
It’s using ImageSourceConverter
to convert that text string into an ImageSource
, and the process that uses it isn’t quite the same as the process you’ve used in your code. The XAML parser will pass the ImageSourceConverter
a context object that offers an IUriContext
service, letting it discover the base URI of the containing XAML document, and it resolves the URI relative to that. So it corresponds to this:
This lets you use a relative URI, as in the original example. It converts it into a full URI using your Window’s base URI, which will be something like “pack://application:,,,/ImageBrush;component/window1.xaml” so it ends up resolving your relative URI to the same fully-qualified URI that my first example uses. But the advantage here is that you don’t need to hard code that into your codebehind. You just ask WPF what the base URI should be.
So by using either of these methods in code behind you are able to do without Tamir’s code.
- Use Build Action of Resource, and use full Pack Uri
- Use Build Action of Resource, and use
BaseUriHelper
All in all, a rather interesting post, I feel.