tags:

views:

59

answers:

3

i want to create as an Image control dynamically in WPF application and set the properties of that controls ...like Size,location,color,sizemode how can i do it? Give me any Samplecode for that..

A: 

You'd like to display an image file or stream? Or you're going to create an image control and add it to the window in code?

Roland
i want to create as an Image control dynamically and set the properties of that controls
Suryakavitha
A: 

Here is a simple example which I have done which loads in the logo for stack overflow.

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);
    }
}

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

Cheers,

Andrew

REA_ANDREW
A: 

From here, on 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;
Tony
ok ... but i have to create 10 image controls and then i have to place them in single Application.... now what can i do
Suryakavitha