首页 > 代码库 > WPF绑定BitMapImage

WPF绑定BitMapImage

先说下图片文件存在服务器。wpf常用绑定图片地址没办法用。忽然想到,convert能否转字节数据?实验了下可以。

图片绑定字节数组。

convert代码

  public class PictureConvert : IValueConverter
    {
        /// <summary>
        /// 转换图片
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            BitmapImage musicPicture = null;
            byte[] image =(byte[]) value;
            if (image != null)
            {
              MemoryStream ms = new MemoryStream(image);
            musicPicture = new BitmapImage();
            musicPicture.BeginInit();
           
            
            musicPicture.StreamSource = ms;
            musicPicture.EndInit();
             
            }
            return musicPicture;
            
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

 

WPF绑定BitMapImage