我需要以编程方式生成图像,因此在xaml中使用标记不是一种选择。当窗口加载时,我也试着运行,但是
运行程序只是给我一个空白屏幕:
我的.CS
namespace WpfApplication2
{
public partial class MainWindow : Window
{
public Image anImage;
public MainWindow()
{
InitializeComponent();
anImage = new Image();
anImage.Visibility = Visibility.Visible;
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
anImage.Margin = new Thickness(500, 500, 0, 0);
BitmapImage bmImage = new BitmapImage();
bmImage.BeginInit();
bmImage.UriSource = new Uri("balloonB.png", UriKind.Relative);
bmImage.EndInit();
anImage.Source = bmImage;
anImage.Stretch = Stretch.None;
this.AddVisualChild(anImage);
this.InvalidateVisual();
}
}
}
我的XAML
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1024" Width="1280" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid Width="1280" Height="1024">
</Grid>
</Window>
答案 0 :(得分:3)
您的代码至少有两个问题。
首先,您应该确保可以在运行时加载图像文件。最简单的方法是将文件添加到Visual Studio项目中,可能位于名为&#34; Images&#34;的文件夹中。文件的构建操作必须设置为Resource
,但这仍然是图像文件的默认设置。现在,您可以通过Pack URI(或Resource Pack URI更准确地访问代码中的此图像资源文件)。给定文件夹名称&#34; Images&#34;,该URI将如下所示:
pack://application:,,,/Images/balloonB.png
其次,您不应使用AddVisualChild
将Image控件添加到MainWindow。相反,通过在XAML中设置Grid上的x:Name
属性,然后在代码隐藏中使用带有该名称的自动生成的成员变量,将其添加到顶级Grid。
<Window ...>
<Grid x:Name="rootGrid" Width="1280" Height="1024">
</Grid>
</Window>
将Image控件添加到Grid:
rootGrid.Children.Add(anImage);
将所有内容放在一起并省略不必要的属性设置,您的代码将如下所示:
var anImage = new Image();
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
anImage.Margin = new Thickness(500, 500, 0, 0);
anImage.Source = new BitmapImage(new Uri("pack://application:,,,/Images/balloonB.png"));
rootGrid.Children.Add(anImage);
或者,更短一些:
var anImage = new Image
{
Width = 120,
Height = 310,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(500, 500, 0, 0),
Source = new BitmapImage(new Uri("pack://application:,,,/Images/balloonB.png"))
};
rootGrid.Children.Add(anImage);
答案 1 :(得分:1)
我发现很多次,一个完整的包Uri有点多,简化的Uri也可以,如果不是更好,只需确保你的图像构建属性配置为MSBuild资源:
Image anImage = new Image();
anImage.Visibility = Visibility.Visible;
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
BitmapImage bmImage = new BitmapImage(new Uri(@"pack://application:,,,/yournameSpace;component/Subfolder/balloonB.png"));
bmImage.BeginInit();
bmImage.EndInit();
anImage.Source = bmImage;
anImage.Stretch = Stretch.None;
gr.Children.Add(anImage);
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1024" Width="1280" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid Name="gr" Width="1280" Height="1024">
</Grid>
</Window>
答案 2 :(得分:0)
此行可能存在问题:
anImage.Margin = new Thickness(500, 500, 0, 0);
我将我的网格重命名为gr并尝试了以下代码并且工作正常,我使用ImageBrush绘制图像:
Image anImage = new Image();
anImage.Visibility = Visibility.Visible;
anImage.Width = 120;
anImage.Height = 310;
anImage.HorizontalAlignment = HorizontalAlignment.Left;
anImage.VerticalAlignment = VerticalAlignment.Top;
//anImage.Margin = new Thickness(500, 500, 0, 0);
BitmapImage bmImage = new BitmapImage();
bmImage.BeginInit();
bmImage.UriSource = new Uri("balloonB.png", UriKind.Relative);
bmImage.EndInit();
ImageBrush ib = new ImageBrush(bmImage);
anImage.Source = ib.ImageSource;
gr.Children.Add(anImage);
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="1024" Width="1280" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid Name="gr" Width="1280" Height="1024">
</Grid>
</Window>