|
WPF: System.Drawing.Bitmap to BitmapSource (and viceversa) |
|
|
|
Scritto da Gene
|
|
sabato 07 marzo 2009 |
|
Here is a code snipplet containing two useful functions that let you convert bitmap images from System.Drawing.Bitmap to System.Windows.Media.Imaging.BitmapSource.
Open this item to view the source code. (by clicking on "Leggi Tutto..." link) =)
private System.Drawing.Bitmap _bitmapFromSource(BitmapSource bitmapsource)
{
System.Drawing.Bitmap bitmap;
using (MemoryStream outStream = new MemoryStream())
{
// from System.Media.BitmapImage to System.Drawing.Bitmap
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapsource));
enc.Save(outStream);
bitmap = new System.Drawing.Bitmap(outStream);
}
return bitmap;
}
private BitmapSource _bitmapToSource(System.Drawing.Bitmap bitmap)
{
BitmapSource destination;
IntPtr hBitmap = bitmap.GetHbitmap();
BitmapSizeOptions sizeOptions = BitmapSizeOptions.FromEmptyOptions();
destination = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, sizeOptions);
destination.Freeze();
return destination;
}
|
|
Ultimo aggiornamento ( sabato 07 marzo 2009 )
|