using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Data; using System.Windows.Media.Imaging; namespace PDF_Master.DataConvert { /// /// 文件绝对路径 转ImageSource 转换器 /// public class FileToImageSourceConvert : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if(!string.IsNullOrEmpty((string)value)&&System.IO.File.Exists((string)value)) { //采用数组的形式读取,避免因bitmap占用,导致图片无法引用的问题 // Read byte[] from png file BinaryReader binReader = new BinaryReader(File.Open((string)value, FileMode.Open)); FileInfo fileInfo = new FileInfo((string)value); byte[] bytes = binReader.ReadBytes((int)fileInfo.Length); binReader.Close(); // Init bitmap BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.StreamSource = new MemoryStream(bytes); bitmap.EndInit(); return bitmap; } return null; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } }