Dynamically Loading Images in SilverLight 1.0 using C#

April 18, 2008 09:40 by Onslaught

Here is an easy way to dynamically load images from a directory into your SilverLight 1.0 application using C#. I'm going to assume you already know how to set up your application to use an aspx (with codebehind) page instead of a static HTML page. If not, this article may be useful.

The aspx page (I'll call it sampleXAML.aspx) contains your XAML declaration for the SilverLight 1.0 application. For this elucidation, we will only have one main canvas. In it, we will call your loadImages() method from the codebehind.

<Canvas>
  <%= loadImages() %>
</Canvas>

In your codebehind (sampleXAML.aspx.cs), we dynamically load images that will be displayed in the SilverLight app. First will create a string variable that will house our XAML declaration for an image.

string strImages = @"<Image x:Name=""image-{0}"" Source=""{1}"" Canvas.Left=""{2}"" Canvas.Right=""{3}"" />;

I've included Canvas.Left and Canvas.Right so images will be placed in a cascading fashion to ensure they were all loaded.

Keep in mind you will need the following namespaces:

System.IO; 
System.Text; 


Next step, read in your files from a directory using the loadImages() method...

protected string loadImages()
{
  StringBuilder strXML = new StringBuilder();

  int intCounter = 0;

  DirectoryInfo dirs = new DirectoryInfo(MapPath("~/images")); //Change path here for a specific folder you want to read in from

  foreach (FileInfo fi in dirs.GetFiles("*.jpg"))
  {
    strXml.AppendFormat(strImages, intCounter, "images/" + fi.Name, intCounter*100, intCounter*100);
    intCounter++;
  }
 
  return strXML.ToString();
}

The strXML is used to build your <Image> XAML tag and is populated in the foreach loop by four parameters. You can add or remove parameters as needed, but at mininum you'll need the Source.

Note that I've filtered for JPGs. First, SilverLight 1.0 can only read JPGs and PNGs. Secondly, Windows loves to create the thumbs.db to display the cool little folder with tiny images of the pictures. While you may not get an error, it will attempt to create another <Image> XAML tag that is empty for that thumbs.db file.

When you run your application, the XAML generated should read to something similar:

<Image x:Name="image-0" Source="images/img01.jpg" Canvas.Left="0" Canvas.Right="0" />
<Image x:Name="image-1" Source="images/img02.jpg" Canvas.Left="100" Canvas.Right="100" />
<Image x:Name="image-2" Source="images/img03.jpg" Canvas.Left="200" Canvas.Right="200" />
<Image x:Name="image-3" Source="images/img04.jpg" Canvas.Left="300" Canvas.Right="300" />

The end. Post any questions you may have.


Currently rated 4.3 by 4 people

  • Currently 4.25/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Comments

April 18. 2008 10:46

trackback

Trackback from DotNetKicks.com

Dynamically Loading Images Silverlight 1.0

DotNetKicks.com

May 15. 2008 07:01

Gravatar

Hey. Thnx for the lesson. But, you know how to do this in silverlight 2.0b1? It isn´t accepting the block

&lt;Canvas>
<%= loadImages() %>
&lt/Canvas>

Thnx!

Cleyton

May 15. 2008 12:35

Gravatar

Silverlight 2.0 Beta 1... Its a completely different game. You can interact with your canvas directly from the codebehind without dynamically creating XAML. This would require a whole 'nother blog to address. But here is a quick sample that may help...

public void Page_Loaded(object o, EventArgs e)
{
// Required to initialize variables
InitializeComponent();

Canvas baseCanvas = o as Canvas;

if (baseCanvas != null)
{
//Get all image files into array and loop through them
string[] images = Directory.GetFiles("c:\\stuff");
foreach (string img in images)
{
Image myImage = new Image();
baseCanvas.Children.Add(myImage);
myImage.Width = 100;
myImage.Stretch = Stretch.Uniform;
myImage.SetValue(TopProperty, 0);
myImage.Opacity = 50;
myImage.Source = new Uri(img);
}
}

Sorry about poor formating, but hopefully that helps. The end.

onSlaught (author)

May 15. 2008 12:41

Gravatar

Yeah, already tried that. The problem is in the call to GetFiles. The SilverLight App gets all blank.

Cleyton

May 15. 2008 12:49

Gravatar

I'm sure you've already tried these troubleshooting methods, but did you make sure you have images in the directory you were looking at? Also make sure your images are valid (only JPG or PNG allowed). Post your answer when you figure it out so others can benefit.

onSlaught (author)

May 15. 2008 12:50

Gravatar

Oh yea, I forgot. Make sure your application has permission to access folder containing your images. The end.

onSlaught (author)

May 15. 2008 12:58

Gravatar

I am sure the images exists and that I have access permission because if I set the image filename static the image appear.

Image1.SetValue(Image.SourceProperty, "/imgs/testandoSilverlightPT-BR.jpg"); --> this works

Any function of System.IO.Directory that I call results in "Blank Silverlight App Error" (Good name ya?)

Cleyton

September 25. 2008 05:08

Gravatar

Hey Guys, I am trying to add pictures dynamically from my image folder using the DirectoryInfo but for some reason I think that DirectoryInfo doesnt work well with Silverlight2.0. I also tried Directory.GetFiles() but everytime i get this error

"Attempt to access the method failed: System.IO.Directory.GetFiles(System.String, System.String)"

Any idea what this about?

Danish

November 5. 2008 20:07

Gravatar

Hello,

Okay quick question. Basically I am trying to build a product store website and I have built a coverflow control like iTunes in Silverlight 2. Right now the images I have loaded are hard coded into the application. What I want to do is basically load images of the last 10 products added to the store. I am thinking of having the SQL database put the url's into an XML and have the Silverlight read from this file to load the images. What would be the best way to accomplish this.

Thanks
Chris

Christopher Kuznicki

Add comment


(Will show your Gravatar icon)  

  Country flag




Live preview

November 21. 2008 07:32

Gravatar