Setting background image in WPF control?

I am trying to get a background image in an image control in WPF, for example, if I load a transparent PNG, I can still see the background. Is it possible, or Microsoft has completely abandoned this feature with WPF, and I have to rely on StackPanels / Grids / Whatever happens for this?

+5
source share
3 answers

Imagedoes not have the right to do this, just put Imagein Borderand install Border.Backgroundto ImageBrush.

+13
source

You do not need images. Set the Window background for the image and set the background of the root element to the image

<Window.Background>
    <ImageBrush ImageSource="BackgroundImage.png"/>
</Window.Background>

<Grid.Background>
    <ImageBrush ImageSource="ForegroundImage.png"/>    
</Grid.Background>
+4
source

As shown in the verified code, set the Background of the Window to the image brush. Notice AllowsTransparency = "True" and WindowStyle = "None" to remove the border.

<Window x:Class="khaosInstallerWPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="616" Width="773" 
    ResizeMode="NoResize" Icon="images/khaos_Installer_UI.png" 
    AllowsTransparency="True" WindowStyle="None">
    <Window.Background>
        <ImageBrush ImageSource="images\khaos_Installer_UI.png"/>
    </Window.Background>
    <Grid Margin="0,0,0,0"></Grid>
</Window>

Bonus: if you use a form to make sure that your form is being dragged

namespace khaosInstallerWPF
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            MouseDown += delegate { DragMove(); };
        }
    }
}
+2
source

All Articles