废话不说,直接码代码:
1 static class ImageConverter 2 { 3 public static ImageSource ToImageSource(this Bitmap bitmap) 4 { 5 return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 6 } 7 8 public static BitmapSource ToBitmapSource(this Bitmap bitmap) 9 { 10 return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 11 } 12 13 public static Icon ToIcon(this Bitmap bitmap) 14 { 15 return Icon.FromHandle(bitmap.GetHicon()); 16 } 17 18 public static Image ToImage(this Bitmap bitmap) 19 { 20 return Image.FromHbitmap(bitmap.GetHbitmap()); 21 } 22 23 public static ImageSource ToImageSource(this Icon icon) 24 { 25 return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 26 } 27 }