WPF中Image 后台代码动态付给路径 (转)

打开文件对话框选择图片 

//浏览图片方法
        private void Browse(Image img)
        {
            try
            {
                string strImage = "";
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "所有文本文件(*.jpg;*.gif;*.png)|*.jpg;*.gif;*.png";
                if (dlg.ShowDialog() == true)
                {
                    strImage = dlg.FileName;
                }
                if (strImage != "")
                {
                    BitmapImage image = new BitmapImage(new Uri(strImage, UriKind.Absolute));
                    img.Source = image;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

红色代码说明:直接付给Image的Source 的路径不好用,必须先转换一下

本文来自忘了忘不了的博客,原文地址:http://blog.sina.com.cn/s/blog_6f99d8ea0100v0b7.html

 

posted @ 2014-06-11 14:42  ck16  阅读(331)  评论(0)    收藏  举报