StringToImageConverter.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* ==============================================================================
  2. * 功能描述:StringToImageConverter
  3. * 创 建 者:Garrett
  4. * 创建日期:2018/3/9 17:09:51
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Data;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Resources;
  15. using System.Windows;
  16. using System.Windows.Media;
  17. namespace SAGA.DotNetUtils.WPF.Converter
  18. {
  19. /// <summary>
  20. /// StringToImageConverter
  21. /// </summary>
  22. public class StringToImageConverter : IValueConverter
  23. {
  24. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  25. {
  26. BitmapImage img = new BitmapImage();
  27. if (value == null) return img;
  28. try
  29. {
  30. var bytes = File.ReadAllBytes(value.ToString());
  31. img.BeginInit();
  32. img.StreamSource = new MemoryStream(bytes);
  33. img.EndInit();
  34. }
  35. catch (Exception e)
  36. {
  37. Console.WriteLine(e);
  38. img = null;
  39. }
  40. return img;
  41. }
  42. public object ConvertBack(object value, Type targetType, object parameter,
  43. System.Globalization.CultureInfo culture)
  44. {
  45. throw new NotImplementedException();
  46. }
  47. }
  48. }