PathToImageConverter.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Data;
  8. using System.Windows.Media.Imaging;
  9. namespace LogoForceTestApp.Modules.MainModule.ViewModels
  10. {
  11. public sealed class PathToImageConverter : IValueConverter
  12. {
  13. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  14. {
  15. if (value == null)
  16. {
  17. return null;
  18. }
  19. string uriStr = (string)value;
  20. Uri uri = new Uri(uriStr, UriKind.Absolute);
  21. BitmapImage bmp = new BitmapImage();
  22. bmp.DecodePixelHeight = 200; // 确定解码高度,宽度不同时设置
  23. bmp.BeginInit();
  24. // 延迟,必要时创建
  25. bmp.CreateOptions = BitmapCreateOptions.DelayCreation;
  26. bmp.CacheOption = BitmapCacheOption.OnLoad;
  27. bmp.UriSource = uri;
  28. bmp.EndInit(); //结束初始化
  29. return bmp;
  30. }
  31. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  32. {
  33. return null;
  34. }
  35. }
  36. }