none
Background Image in WPF

    Question

  • I would like to have some standard jpg backgroundon my window. However, currently if I use Image Source="", my image gets docked to one side and my text boxes/labels get moved.

    Is there a way for me to have a background image cover the window and have text boxes, labels on the image?
    Monday, May 04, 2009 8:54 PM

Answers

  • A good way to do this would be to use an ImageBrush.  Create your imagebrush in your window's resources area, then use it as a static resource in the window's background property.

    <Window ... Background="{StaticResource MyImageBrush}" ...

    There is a good example about that here:

    http://msdn.microsoft.com/en-us/library/system.windows.media.imagebrush.aspx

    Frank
    • Proposed as answer by Frankenspank Monday, May 04, 2009 9:08 PM
    • Marked as answer by OdomaeB Thursday, May 07, 2009 2:25 PM
    Monday, May 04, 2009 9:08 PM
  • You can use the Background property with an ImageBrush, for example, adding the image as resource:

    Set background as:

    Background

     

    ="{StaticResource MyImageBrush}"

    And the resource:

    <

     

    Application.Resources>
        <ImageBrush x:Key="MyImageBrush" ImageSource="Images/curtains.png" />
    </
    Application.Resources>



    Monday, May 04, 2009 9:10 PM
  • Did you add your sample.jpg as a Resource (as a BuildAction) in the root of the project ?

    <

     

    Window.Background>
       <ImageBrush TileMode="None" ImageSource="sample.png" />
    </Window.Background>

    • Marked as answer by OdomaeB Thursday, May 07, 2009 2:25 PM
    Monday, May 04, 2009 9:56 PM

All replies

  • A good way to do this would be to use an ImageBrush.  Create your imagebrush in your window's resources area, then use it as a static resource in the window's background property.

    <Window ... Background="{StaticResource MyImageBrush}" ...

    There is a good example about that here:

    http://msdn.microsoft.com/en-us/library/system.windows.media.imagebrush.aspx

    Frank
    • Proposed as answer by Frankenspank Monday, May 04, 2009 9:08 PM
    • Marked as answer by OdomaeB Thursday, May 07, 2009 2:25 PM
    Monday, May 04, 2009 9:08 PM
  • You can use the Background property with an ImageBrush, for example, adding the image as resource:

    Set background as:

    Background

     

    ="{StaticResource MyImageBrush}"

    And the resource:

    <

     

    Application.Resources>
        <ImageBrush x:Key="MyImageBrush" ImageSource="Images/curtains.png" />
    </
    Application.Resources>



    Monday, May 04, 2009 9:10 PM
  • I did this and the background image doesn't seem to be loading.
    <Window.Background>
            <ImageBrush TileMode="None">
                <ImageBrush.ImageSource>
                    <BitmapImage UriSource="sample.jpg"/>
                </ImageBrush.ImageSource>
            </ImageBrush>
        </Window.Background>
    Monday, May 04, 2009 9:51 PM
  • Did you add your sample.jpg as a Resource (as a BuildAction) in the root of the project ?

    <

     

    Window.Background>
       <ImageBrush TileMode="None" ImageSource="sample.png" />
    </Window.Background>

    • Marked as answer by OdomaeB Thursday, May 07, 2009 2:25 PM
    Monday, May 04, 2009 9:56 PM
  • I added

    Background= "{ StaticResource MyImageBrush }" to the window1.xaml and

    Application.Resources >
        < ImageBrush x : Key ="MyImageBrush" ImageSource ="Images/curtains.png" />
    </
    Application.Resources > to App.xaml.

    I still don't see the image.


    Monday, May 04, 2009 10:47 PM
  • yes, I added the .jpg to resources and set built action to resource.
    Monday, May 04, 2009 10:51 PM
  • That is weird Phoebe.
    unless your curtains.png not on Images folder.

    It should work.  you could post your window xaml here so that we could evaluate
    Tuesday, May 05, 2009 5:14 AM
  • It's works

    Developing is part of being a developer.

    Tuesday, April 10, 2012 1:11 PM