There are examples how to set image to the Image object in Silverlight (both in 1.0 and 1.1); however I haven’t found one how to set it to any object that can be filled. The beauty of XAML based applications is that you can put whatever media you want inside objects, so if you want to fill in Ellipse or a Path with an image you can. The easiest way to do it is just declaratively, in XAML:
<Rectangle x:Name="mainPic" Width="448" Height="386" Canvas.Left="288">
<Rectangle.Fill>
<ImageBrush ImageSource="pic.jpg" />
</Rectangle.Fill>
</Rectangle>
This is great for static content, but what if you want to set it programmatically using Downloader object.
Below is the solution for 1.0 (I tested in on Feb CTP release)
var wpfe;
function root_Loaded(sender, args) {
wpfe = document.getElementById("wpfeControl1");
mainDownloader = wpfe.createObject("Downloader");
mainDownloader.completed = "javascript:mainDownloadCompleted";
mainDownloader.DownloadProgressChanged = "javascript:mainDownloadProgressChanged";
mainDownloader.open("GET", "SomePic.JPG", true);
mainDownloader.send();
}
function mainDownloadCompleted(sender, args) {
sender.findName("mainPic").Fill.ImageSource = sender.Uri;
}
Apart from setting the downloader, the most important code line is this:
sender.findName("mainPic").Fill.ImageSource = sender.Uri;
When the image gets downloaded we set the Rectangle’s ImageBrush to its URL.
Now solution for 1.1 in C#:
public void Page_Loaded(object o, EventArgs e)
{
// Required to initialize variables
InitializeComponent();
Downloader downloader = new Downloader();
downloader.Completed += new EventHandler(downloader_Completed);
downloader.Open("GET", new Uri("SomePic.JPG", UriKind.Relative), true);
downloader.Send();
}
void downloader_Completed(object sender, EventArgs e)
{
(mainPic.Fill as ImageBrush).ImageSource = (sender as Downloader).Uri;
}
Once again, the most important code line is this
(mainPic.Fill as ImageBrush).ImageSource = (sender as Downloader).Uri;
Notice, how much casting we have to do, because C #is a strongly typed language.
Hope this helps.