在 WPF 图像控件上设置背景图像?

发布于 2024-12-28 16:20:39 字数 121 浏览 0 评论 0原文

我试图在 WPF 中的图像控件上添加背景图像,例如如果我加载透明 PNG,我仍然可以看到背景。有可能吗,还是微软在 WPF 中完全放弃了这个功能,而我必须依靠 StackPanels/Grids/Whatever 来实现这一点?

I'm trying to have a background image on an image control in WPF, such as if I load a transparent PNG, I would still be able to see the background. Is it possible, or did Microsoft totally drop this feature with WPF and I have to rely on StackPanels/Grids/Whatever to achieve this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神爱温柔 2025-01-04 16:20:39

Image 没有允许这样做的属性,只需将 Image 放在 Border 中并设置 Border.BackgroundImageBrush

Image has no property to allow for that, just put the Image in a Border and set the Border.Background to an ImageBrush.

零度° 2025-01-04 16:20:39

不,你需要图像。将Window背景设置为图像并将根元素背景设置为图像

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

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

No you need to images. Set the Window background to the image and set the root element background to an image

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

<Grid.Background>
    <ImageBrush ImageSource="ForegroundImage.png"/>    
</Grid.Background>
策马西风 2025-01-04 16:20:39

如此处测试的代码所示,将窗口背景设置为图像画笔。注意 AllowsTransparency="True"WindowStyle="None" 删除边框。

<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>

奖励:如果您使用形状,请确保您的表单可拖动

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

As shown in tested code here set the Window background to an image brush. Notice AllowsTransparency="True" And WindowStyle="None" to drop 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 are using a shaped for be sure to make your form draggable

namespace khaosInstallerWPF
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            MouseDown += delegate { DragMove(); };
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文