图像相关(一) WPF Image控件动态加载图片方式

来源:互联网 发布:中山大学旅游学院 知乎 编辑:程序博客网 时间:2024/05/16 11:51
        private BitmapImage CreateBitmapImage(string imgUrl)        {            //方式1,直接通过图片路径            BitmapImage bmp = new BitmapImage();            bmp.BeginInit();            bmp.UriSource = new Uri(imgUrl);            bmp.EndInit();            //方式2,通过字节数组            //FileStream fs = new FileStream(imgUrl, FileMode.Open);            //byte[] buffer = new byte[fs.Length];            //fs.Read(buffer, 0, buffer.Length);            //fs.Close();            //BitmapImage bmp = new BitmapImage();            //bmp.BeginInit();            //bmp.StreamSource = new MemoryStream(buffer);            //bmp.EndInit();            return bmp;        }


注意当用方式1加载图片时,再用方式2加载同一种图片时,则会报错。




同时用方式1或同时用方式2 都不会报错

原创粉丝点击