mardok
mardok

Reputation: 2425

Set Height and Width for images as WPF background

I need to set a image as background for WPF Window. I have set it like that:

<Window x:Class="DiagramView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="418" Width="1185" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
    <Window.Background>
        <ImageBrush ImageSource="../Images/Med.jpg" ></ImageBrush>
    </Window.Background>
</Window>

But If I have height and width for window as orginal image, the image at window isn't not sharpen as it should be. I guess it happens because part of height and width is taken by window itself. What I properties I should use to have images with orginal width/height

Upvotes: 0

Views: 2440

Answers (2)

Sheridan
Sheridan

Reputation: 69985

If you look on the ImageBrush Class page on MSDN, you'll see that there are a number of properties that you can use to position the image within the Brush. The following are of particular interest for positioning:

AlignmentX
AlignmentY
Stretch
Viewbox
ViewboxUnits
Viewport
ViewportUnits

Please see the linked page for their descriptions.

Upvotes: 0

Clemens
Clemens

Reputation: 128145

In order to retain the original image size set the Stretch property to None:

<Window.Background>
    <ImageBrush ImageSource="../Images/Med.jpg" Stretch="None" />
</Window.Background>

Upvotes: 3

Related Questions