12,064,656 members (23,115 online)
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: //THIS DOESNT WORK UNLESS I UNCOMMENT THE CODE BELOW, 2: //WHICH IS NOT EVEN RELATED 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: //THIS DOESNT WORK UNLESS I UNCOMMENT THE CODE BELOW, 2: //WHICH IS NOT EVEN RELATED 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: //BUT IF I UNCOMMENT THESE THE ABOVE CODE WORKS JUST FINE 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 cant. 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 Khasons blog.
Which allows us to do things like
or for framework elements
All of which is way coo. 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 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 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.
All in all a rather interesting post, I feel.