1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- 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_Office.DataConvert
- {
- /// <summary>
- /// 文件绝对路径 转ImageSource 转换器
- /// </summary>
- 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();
- }
- }
- }
|