您的位置:首页 > 其它

How to Load Images from a Stream

2009-09-25 17:54 507 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐