Home of the AlmostImplementedException

WPF: How to use an image as brush

When you want to use an image as brush, you have to decide where to put it. Best and easiest way is to include it as resource to your project.
Simply add to picture to your project (maybe in an subfolder called images) and change “build action” to resource, if its not already set.

To use this image just declare an ImageBrush with the image path and name.

Here i use the image w1.png in the subfolder Images in my project.
Use an absolute path to use an image outside of your project, like:

That way you don’t have the resource compiled into your project and can change it later.
A ever better way to extract images from the main program and provide other images later, are resource-dll. Just create a dll and add the image(s) as resources. To use them, add the dll as reference to your project and call the image this way:

MyDll is the name of the Resource-Dll and Images/w1.png is the path and name of the image. This way you can just load another dll with different images at runtime to change the UI.

If you want to use the brush as resourse itself just give it a name and add it to a Resource section. This is best practice when you want to use this brush multiple times.

And here an example of how to use this brush direct or as resource by drawing an ellipse:

ImageBrush is available since .Net 3.0

Share :

, ,

Leave a Reply