BitmapToImageSourceConverter.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Drawing;
  3. using System.Windows;
  4. using System.Windows.Data;
  5. using System.Windows.Interop;
  6. using System.Windows.Media.Imaging;
  7. namespace FWindSoft.Wpf
  8. {
  9. public class BitmapToImageSourceConverter : IValueConverter
  10. {
  11. object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  12. {
  13. BitmapSource bitmapSource = null;
  14. Bitmap bitmap = value as Bitmap;
  15. if (bitmap == null) return bitmapSource;
  16. try
  17. {
  18. bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero,
  19. Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
  20. }
  21. catch (Exception)
  22. {
  23. }
  24. return bitmapSource;
  25. }
  26. object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  27. {
  28. throw new NotImplementedException();
  29. }
  30. }
  31. }