2012-04-09 17 views
19

Ho un'istanza di System.Drawing.Image.Mostra Drawing.Image in WPF

Come posso mostrarlo nella mia applicazione WPF?

Ho provato con img.Source ma questo non funziona.

+0

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

+3

Possibile duplicato di [WPF - Posso usare System.Drawing in wpf?] (Http://stackoverflow.com/questions/10663056/wpf-can-i-use-system-drawing-in-wpf) –

risposta

14

Per caricare un'immagine in un controllo Image WPF è necessario un System.Windows.Media.ImageSource.

è necessario convertire l'oggetto Drawing.Image a un oggetto 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; 
    } 

Dichiarazione del metodo DeleteObject.

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

Mi piace questa soluzione! Grazie amico! –

19

Ho lo stesso problema e lo risolvo combinando diverse risposte.

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 

Da this question and answers

+2

+1 Bello e ordinato – GETah

+0

Non dimenticare di disporre di 'bmp'. – Peter

+0

in che cosa viene utilizzato ** bmp ** in questo esempio? – juagicre

9

Se si utilizza un convertitore, si può effettivamente associare all'oggetto Image. Sarà sufficiente creare uno IValueConverter per convertire lo Image in uno BitmapSource.

Ho usato il codice di esempio di AlexDrenea all'interno del convertitore per fare il vero lavoro.

[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(); 
    } 
} 

Nel tuo XAML è necessario aggiungere il convertitore.

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

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

Autonomo. mi piace – Basic