2010-02-06 65 views
-1

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

+0

好吧...但我必须创建10个图像控件,然后我必须将它们放在单个应用程序....现在我能做什么: – Suryakavitha 2010-02-06 09:05:00

+1

请不要再问同样的问题。如果您想添加更多详细信息,可以使用这些注释上方的“编辑”链接编辑您的问题。 – 2010-02-06 12:49:16

回答

1

这是一个简单的例子,我已经做了哪些加载在标志堆栈溢出。

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> 

干杯,

安德鲁

0

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

+0

我想创建作为Image控件动态地设置属性s的控制 – Suryakavitha 2010-02-06 08:47:23

0

here,MSDN上

// 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; 
+0

好的...但我必须创建10个图像控件,然后我必须将它们放在单个应用程序中....现在,我可以做什么 – Suryakavitha 2010-02-06 09:53:57