1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| public static ImageSource ToImageSource(this System.Drawing.Icon pIcon) {
ImageSource ret = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(pIcon.Handle, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
return ret;
}
public static System.Windows.Media.Imaging.BitmapImage ToBitmapImage(this System.Drawing.Image pImg) {
System.Windows.Media.Imaging.BitmapImage bi = new System.Windows.Media.Imaging.BitmapImage();
bi.BeginInit();
MemoryStream ms = new MemoryStream();
pImg.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);// Save to a memory stream...
ms.Seek(0, SeekOrigin.Begin);// Rewind the stream...
bi.StreamSource = ms; // Tell the WPF image to use this stream...
bi.EndInit();
return bi; // */
//var decoder = System.Windows.Media.Imaging.BitmapDecoder.Create(ms, System.Windows.Media.Imaging.BitmapCreateOptions.PreservePixelFormat, System.Windows.Media.Imaging.BitmapCacheOption.OnLoad);
//return decoder.Frames[0];
/*System.Drawing.Bitmap newBitmap = new System.Drawing.Bitmap(pImg);
ImageSource ret = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(newBitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
return ret; // */
}
public static System.Windows.Media.Imaging.BitmapImage ToBitmapImage(this System.Drawing.Bitmap bitmap) {
using (var memory = new MemoryStream()) {
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
memory.Position = 0;
var bitmapImage = new System.Windows.Media.Imaging.BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
return bitmapImage;
}
} |
Partager