C# 在 WPF 图像控件上设置背景图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8994827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting a background image on a WPF image control?
提问by pikzen
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 ?
我正在尝试在 WPF 中的图像控件上添加背景图像,例如如果我加载透明的 PNG,我仍然可以看到背景。是否有可能,或者微软是否完全放弃了 WPF 的这个功能,我必须依靠 StackPanels/Grids/Whatever 来实现这一点?
采纳答案by H.B.
Imagehas no property to allow for that, just put the Imagein a Borderand set the Border.Backgroundto an ImageBrush.
Image没有允许这样做的属性,只需将Imagea放入并将 theBorder设置Border.Background为 an ImageBrush。
回答by MyKuLLSKI
No you need to images. Set the Window background to the image and set the root element background to an image
不,你需要图像。将Window背景设置为图像,将根元素背景设置为图像
<Window.Background>
<ImageBrush ImageSource="BackgroundImage.png"/>
</Window.Background>
<Grid.Background>
<ImageBrush ImageSource="ForegroundImage.png"/>
</Grid.Background>
回答by Joe Johnston
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 背景设置为图像画笔。注意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>
Bonus: If you are using a shaped for be sure to make your form draggable
奖励:如果您使用的是 shape 确保让您的表单可拖动
namespace khaosInstallerWPF
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MouseDown += delegate { DragMove(); };
}
}
}

