wpf - 使用 XAML 图像作为 WPF 窗口背景

标签 wpf image xaml background window

关于如何将 XAML 矢量图像作为窗口背景的任何建议?有很多代码用 jpg 显示了这一点,但我更喜欢基于矢量的图像。

将它作为资源也将是一种奖励,但我对最佳方法感到困惑。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Viewbox x:Key="Background2" Stretch="Fill">
        <Canvas >
            <!-- Ebene 1/<Path> -->
            <Path Fill="#ff000000" Data="F1 M 841.890,595.275 L 0.000,595.275 L 0.000,0.000 L 841.890,0.000 L 841.890,595.275 Z"/>
            <!-- Ebene 1/<Path> -->
            <Path Data="F1 M 265.910,218.277 C 265.910,169.332 223.865,129.655 172.000,129.655 C 120.135,129.655 78.090,169.332 78.090,218.277 C 78.090,267.222 120.135,306.898 172.000,306.898 C 223.865,306.898 265.910,267.222 265.910,218.277 Z">
                <Path.Fill>
                    <RadialGradientBrush MappingMode="Absolute" GradientOrigin="172.733,217.234" Center="172.733,217.234" RadiusX="81.912" RadiusY="81.912">
                        <RadialGradientBrush.GradientStops>
                            <GradientStop Offset="0.00" Color="#ff0d4976"/>
                            <GradientStop Offset="0.41" Color="#ff06243b"/>
                            <GradientStop Offset="1.00" Color="#ff000000"/>
                        </RadialGradientBrush.GradientStops>
                        <RadialGradientBrush.Transform>
                            <MatrixTransform Matrix="1.146,0.000,0.000,1.082,-26.038,-16.750" />
                        </RadialGradientBrush.Transform>
                    </RadialGradientBrush>
                </Path.Fill>
            </Path>
        </Canvas>
    </Viewbox>
</ResourceDictionary>

如果删除 Viewbox,上面的 Resource 代码可以正常工作。窗口的代码是:-
  <Window x:Class="Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window2" Height="700" Width="800">
        <Window.Resources>
            <ResourceDictionary Source="Resources/Dictionary2.xaml" />
        </Window.Resources>
        <Grid>
         <StaticResource ResourceKey="Background2"/>   
        </Grid>
    </Window>

最佳答案

尝试这个

<Window.Resources>
    <Canvas x:Key="Background2">
        <!-- Ebene 1/<Path> -->
        <Path Fill="#ff000000" Data="F1 M 841.890,595.275 L 0.000,595.275 L 0.000,0.000 L 841.890,0.000 L 841.890,595.275 Z"/>
        <!-- Ebene 1/<Path> -->
        <Path Data="F1 M 265.910,218.277 C 265.910,169.332 223.865,129.655 172.000,129.655 C 120.135,129.655 78.090,169.332 78.090,218.277 C 78.090,267.222 120.135,306.898 172.000,306.898 C 223.865,306.898 265.910,267.222 265.910,218.277 Z">
            <Path.Fill>
                <RadialGradientBrush MappingMode="Absolute"
                           GradientOrigin="172.733,217.234"
                           Center="172.733,217.234"
                           RadiusX="81.912" RadiusY="81.912">
                    <RadialGradientBrush.GradientStops>
                        <GradientStop Offset="0.00" Color="#ff0d4976"/>
                        <GradientStop Offset="0.41" Color="#ff06243b"/>
                        <GradientStop Offset="1.00" Color="#ff000000"/>
                    </RadialGradientBrush.GradientStops>
                    <RadialGradientBrush.Transform>
                        <MatrixTransform 
                             Matrix="1.146,0.000,0.000,1.082,-26.038,-16.750" />
                    </RadialGradientBrush.Transform>
                </RadialGradientBrush>
            </Path.Fill>
        </Path>
    </Canvas>
</Window.Resources>

<Grid >
    <Grid.Background>
        <VisualBrush Stretch="Fill" Visual="{StaticResource Background2}" />
    </Grid.Background>
</Grid>

如果绝对必要,您只需进行一些更改即可将资源移动到您的资源字典中。

关于wpf - 使用 XAML 图像作为 WPF 窗口背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1316208/

相关文章:

wpf - 如何获取 WPF Visual 元素的缩放大小

c# - 从给定路径加载图标以显示在 WPF 窗口中

c# - WPF 中的填充(左、上、右、下)

c# - 使用 System.Windows.MessageBox 更新 MessageBoxText 运行时?

java - 如何在 java 中获取 jpg 的像素尺寸?

javascript - 使用css动态调整图像宽度和高度

c# - C#中如何设置控件模板

jquery - 使图像成为放大和缩小按钮

c# - 如何以编程方式更改/替换模板中的 StoryBoard?

wpf - WPF 的跟踪栏/ slider 模板