Click here to Skip to main content
15,882,904 members
Articles / Desktop Programming / WPF

A Moan About ImageBrush In WPF

Rate me:
Please Sign up or sign in to vote.
5.00/5 (9 votes)
17 Jun 2009CPOL3 min read 44.4K   8   1
ImageBrush in WPF

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:

37341/image-thumb.png

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:

C#
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:

37341/image-thumb1.png

So I then tried to add an image to the code, so we now had:

XML
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:

C#
 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 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.

37341/image-thumb2.png

Which allows us to do things like:

37341/image-thumb3.png

or for framework elements:

37341/image-thumb4.png

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:

37341/image-thumb5.png

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:

37341/image-thumb6.png

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:

37341/image-thumb7.png

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:

37341/image-thumb8.png

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions

 
GeneralMy vote of 5 Pin
S Schearer17-Nov-10 9:25
S Schearer17-Nov-10 9:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.