Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #92 – How to Load Images from a Stream

In Tip of the Day #86 I showed you how to load external images not included in the project such as images on the web. In this Tip I will be showing you had to load images from the client’s machine through a file stream.

Due to obvious security reasons Silverlight cannot directly load files from a client box. However, in response to an event like a button Silverlight can load files through the OpenFileDialog where the client gets to choose what file to load.

The following sample shows you how to load a PNG file once a use clicks on a button. In the code below, “MyImage” is an Image control that I declared in my XAML. BitmapImage can be found in System.Windows.Media.Imaging.

private void Button_Click_Load_Image(object sender, RoutedEventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "PNG Files (*.png;*.png)|*.png;*.png | All Files (*.*)|*.*";
    ofd.FilterIndex = 1;
    
    if (true == ofd.ShowDialog())
    {
        System.IO.Stream stream = ofd.File.OpenRead();
        BitmapImage bi = new BitmapImage();
        bi.SetSource(stream);
        MyImage.Source = bi;
        stream.Close();
    }
}

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

In Tip of the Day #86 I showed you how to load external images not included in the project such as images

# February 9, 2009 7:10 PM

Silverlight Travel » How to Load Images from a Stream said:

Pingback from  Silverlight Travel » How to Load Images from a Stream

# February 10, 2009 1:02 AM

Dew Drop - February 10, 2009 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - February 10, 2009 | Alvin Ashcraft's Morning Dew

# February 10, 2009 10:17 AM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight Tips of the

# February 10, 2009 6:22 PM

Visual Web Developer Team Blog said:

Most Recent Posts: Tip #93 - Reading XML with Silverlight Tip #92 - How to Load Images from a Stream

# February 10, 2009 6:25 PM

Community Blogs said:

In this issue: Maurice de Beijer, Jesse Liberty(2), Corey Schuman, Mike Snow, Tim Heuer, and Radenko

# February 11, 2009 1:04 AM

This Week on C9: jQuery, Win Mobile, Warcraft AddOn tools, and an Automed Bartender | CHARGED's Digital Lifestyle at Work or Play said:

Pingback from  This Week on C9: jQuery, Win Mobile, Warcraft AddOn tools, and an Automed Bartender | CHARGED's Digital Lifestyle at Work or Play

# February 21, 2009 7:17 AM

NewsPeeps said:

Thank you for submitting this cool story - Trackback from NewsPeeps

# August 8, 2009 6:35 PM