StringToImageConverter.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* ==============================================================================
  2. * 功能描述:StringToImageConverter
  3. * 创 建 者:Garrett
  4. * 创建日期:2018/3/9 17:09:51
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Data;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Resources;
  14. using System.Windows;
  15. namespace SAGA.DotNetUtils.WPF.Converter
  16. {
  17. /// <summary>
  18. /// StringToImageConverter
  19. /// </summary>
  20. public class StringToImageConverter:IValueConverter
  21. {
  22. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  23. {
  24. BitmapImage img = new BitmapImage();
  25. if (value == null) return img;
  26. //若要原始文件的站点,可以调用 Application 类的 GetRemoteStream 方法,同时传递标识原始文件的所需站点的 pack URI。 GetRemoteStream 将返回一个 StreamResourceInfo 对象,该对象将原始文件的该站点作为 Stream 公开,并描述其内容类型。
  27. var uri = new Uri(value.ToString().Replace("%20", " "), UriKind.Relative);
  28. StreamResourceInfo info = Application.GetRemoteStream(uri);
  29. img.BeginInit();
  30. //img.UriSource = new Uri(value.ToString(), UriKind.Relative);
  31. img.StreamSource = info.Stream;
  32. img.EndInit();
  33. return img;
  34. }
  35. public object ConvertBack(object value, Type targetType, object parameter,
  36. System.Globalization.CultureInfo culture)
  37. {
  38. throw new NotImplementedException();
  39. }
  40. }
  41. }