C# WPF应用程序中动态生成的图像控件

C# WPF应用程序中动态生成的图像控件,c#,wpf,C#,Wpf,我想在WPF应用程序中动态创建一个图像控件,并设置该控件的属性…如大小、位置、颜色、sizemode 我怎么做?给我任何样本代码。你想显示图像文件或流吗?或者您要创建一个图像控件并将其添加到代码中的窗口?下面是一个简单的示例,我在徽标中加载了堆栈溢出 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Loaded += ne

我想在WPF应用程序中动态创建一个图像控件,并设置该控件的属性…如大小、位置、颜色、sizemode
我怎么做?给我任何样本代码。

你想显示图像文件或流吗?或者您要创建一个图像控件并将其添加到代码中的窗口?

下面是一个简单的示例,我在徽标中加载了堆栈溢出

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        var webImage = new BitmapImage(new Uri("http://sstatic.net/so/img/logo.png"));
        var imageControl = new Image();
        imageControl.Source = webImage;
        ContentRoot.Children.Add(imageControl);
    }
}
还有xaml

<Window x:Class="WpfExamples.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="ContentRoot">

    </Grid>
</Window>

干杯

Andrew

来自MSDN


我想动态创建一个图像控件,并设置该控件的属性。。。但我必须创建10个图像控件,然后我必须将它们放在一个应用程序中。。。。现在我能做什么:请停止重复问同样的问题。如果您想添加更多详细信息,可以使用这些评论上方的
edit
链接编辑您的问题。好的。。。但我必须创建10个图像控件,然后我必须将它们放在一个应用程序中。。。。现在我该怎么办
// Create Image Element
Image myImage = new Image();
myImage.Width = 200;

// Create source
BitmapImage myBitmapImage = new BitmapImage();

// BitmapImage.UriSource must be in a BeginInit/EndInit block
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg");

// To save significant application memory, set the DecodePixelWidth or  
// DecodePixelHeight of the BitmapImage value of the image source to the desired 
// height or width of the rendered image. If you don't do this, the application will 
// cache the image as though it were rendered as its normal size rather then just 
// the size that is displayed.
// Note: In order to preserve aspect ratio, set DecodePixelWidth
// or DecodePixelHeight but not both.
myBitmapImage.DecodePixelWidth = 200;
myBitmapImage.EndInit();
//set image source
myImage.Source = myBitmapImage;