WPF Window Background ImageBrush not tiling

末鹿安然 提交于 2020-01-01 07:38:11

问题


I have a window with a background image. The image may change at runtime which really should not matter for this.

I want the image to be fixed to the top left (which it is) and not scale (which is also correct. But I need the image to repeat (tile) when the window is made larger than the image. I am doing ...

What am i missing?

TIA


回答1:


You need to set the TileMode property as well as the Viewport and ViewportUnits:

For example:

<Window.Background>
    <ImageBrush ImageSource="myImage.png"
        Viewport="0,0,300,300"
        ViewportUnits="Absolute"
        TileMode="Tile"
        Stretch="None"
        AlignmentX="Left"
        AlignmentY="Top"  />
</Window.Background>

Note: the second 2 segments of the Viewport attribute indicate the desired size of each repetition. If you want to display the entire image, these should be the width and height of the image.

Example output:

Edit in response to comments

If you don't know the size of the image to specify in the Viewport property, you can use a Binding with an IValueConverter to calculate it from the image. I am convinced there must be a better way of doing this, but I haven't found one yet!

XAML:

<Window.Resources>
    <local:Converter x:Key="Converter" />
</Window.Resources>
<Window.Background>

    <ImageBrush ImageSource="myImage.png"
    ViewportUnits="Absolute"
    TileMode="Tile"
    Stretch="None"
    AlignmentX="Left"
    AlignmentY="Top" 
    Viewport="{Binding ImageSource, RelativeSource={RelativeSource Self}, Converter={StaticResource Converter}}"/>
</Window.Background>

Value converter:

public class Converter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var source = (ImageSource)value;
        return new Rect(0,0,source.Width, source.Height);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}



回答2:


If you would like the entire solution in c#

ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(@"c:\your\image\source.gif"));
brush.TileMode = TileMode.Tile;
brush.ViewportUnits = BrushMappingMode.Absolute;
brush.Viewport = new Rect(0, 0, brush.ImageSource.Width, brush.ImageSource.Height);

MainWindow1.Background = brush;


来源:https://stackoverflow.com/questions/4097330/wpf-window-background-imagebrush-not-tiling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!