locked
Load Images from Directory in Silverlight RRS feed

  • Question

  • Hi,

    I have a Quick Question: I would like to load images from a Directory in Silverlight. I have 100 images in a folder and i dont want to specify each image name in the code or xml or in xaml. So, i would like to loop through the directory read image files path and bind it to my control.

    Any Ideas/Suggestion are greatly appreciated.

     

    Thanks,

     

    San

     

    Sunday, May 1, 2011 11:23 PM

Answers

  • Hi StarterKit,

    In WPF, we could do as follow:

    public static List<BitmapImage> LoadImages()

    {

        List<BitmapImage> robotImages = new List<BitmapImage>();

        DirectoryInfo robotImageDir = new DirectoryInfo(@"..\..\Robots");

        foreach (FileInfo robotImageFile in robotImageDir.GetFiles("*.jpg"))

        {

            Uri uri = new Uri(robotImageFile.FullName);

            robotImages.Add(new BitmapImage(uri));

        }

        return robotImages;

    }

    And then, we could bind image control:

    <Image Grid.Column="0" Source="{Binding UriSource}" />

    you could download the complete sample(WPF) form:

    http://cid-41e44c402aaada87.office.live.com/self.aspx/MenuItemTemplate/MenuItemTemplate.zip

    If you want to get Silverlight answer, I think you could post your issue on Silverlight forum:

    http://forums.silverlight.net/forums/

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Sheldon _Xiao Thursday, May 12, 2011 6:04 AM
    Wednesday, May 4, 2011 6:28 AM