2012-04-09 21 views
19

Tengo una instancia de System.Drawing.Image.Mostrar dibujo.Imagen en WPF

¿Cómo puedo mostrar esto en mi aplicación WPF?

He intentado con img.Source pero eso no funciona.

+0

posiblemente relacionadas: http://stackoverflow.com/questions/1118496/using-image-control-in-wpf-to-display-system-drawing-bitmap – Alain

+3

duplicado posible de [WPF - ¿Puedo usar System.Drawing en wpf?] (Http://stackoverflow.com/questions/10663056/wpf-can-i-use-system-drawing-in-wpf) –

Respuesta

14

Para cargar una imagen en un control de imagen WPF necesitará un System.Windows.Media.ImageSource.

es necesario convertir el objeto Drawing.Image a un objeto ImageSource:

public static BitmapSource GetImageStream(Image myImage) 
    { 
     var bitmap = new Bitmap(myImage); 
     IntPtr bmpPt = bitmap.GetHbitmap(); 
     BitmapSource bitmapSource = 
     System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
       bmpPt, 
       IntPtr.Zero, 
       Int32Rect.Empty, 
       BitmapSizeOptions.FromEmptyOptions()); 

     //freeze bitmapSource and clear memory to avoid memory leaks 
     bitmapSource.Freeze(); 
     DeleteObject(bmpPt); 

     return bitmapSource; 
    } 

Declaración del método DeleteObject.

[DllImport("gdi32.dll")] 
[return: MarshalAs(UnmanagedType.Bool)] 
internal static extern bool DeleteObject(IntPtr value); 
+0

¡Me gusta esta solución! ¡Gracias hombre! –

19

Tengo el mismo problema y lo resuelvo combinando varias respuestas.

System.Drawing.Bitmap bmp; 
Image image; 
... 
using (var ms = new MemoryStream()) 
{ 
    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png); 
    ms.Position = 0; 

    var bi = new BitmapImage(); 
    bi.BeginInit(); 
    bi.CacheOption = BitmapCacheOption.OnLoad; 
    bi.StreamSource = ms; 
    bi.EndInit(); 
} 

image.Source = bi; 
//bmp.Dispose(); //if bmp is not used further. Thanks @Peter 

De this question and answers

+2

+1 Agradable y ordenado – GETah

+0

No se olvide de desechar 'bmp'. – Peter

+0

¿para qué se usa ** bmp ** en este ejemplo? – juagicre

9

Si utiliza un convertidor, en realidad se puede enlazar con el objeto Image. Tan solo deberá crear un IValueConverter que convertirá el Image en un BitmapSource.

Utilicé el código de muestra de AlexDrenea dentro del convertidor para hacer el trabajo real.

[ValueConversion(typeof(Image), typeof(BitmapSource))] 
public class ImageToBitmapSourceConverter : IValueConverter 
{ 
    [DllImport("gdi32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    internal static extern bool DeleteObject(IntPtr value); 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     Image myImage = (Image)value; 

     var bitmap = new Bitmap(myImage); 
     IntPtr bmpPt = bitmap.GetHbitmap(); 
     BitmapSource bitmapSource = 
     System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
       bmpPt, 
       IntPtr.Zero, 
       Int32Rect.Empty, 
       BitmapSizeOptions.FromEmptyOptions()); 

     //freeze bitmapSource and clear memory to avoid memory leaks 
     bitmapSource.Freeze(); 
     DeleteObject(bmpPt); 

     return bitmapSource; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

En su XAML, tendrá que agregar el convertidor.

<utils:ImageToBitmapSourceConverter x:Key="ImageConverter"/> 

<Image Source="{Binding ThumbSmall, Converter={StaticResource ImageConverter}}" 
        Stretch="None"/> 
+0

Autónomo. me gusta – Basic

Cuestiones relacionadas