WPF

2012-04-09 23 views
19

içinde Drawing.Image gösterme System.Drawing.Image bir örneğim var.WPF

Bunu WPF uygulamasında nasıl gösterebilirim?

img.Source ile denedim, ancak bu çalışmaz.

+0

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

+3

Olası yinelenen - ı kullanabilir miyim Wpf'de System.Drawing?] (Http://stackoverflow.com/questions/10663056/wpf-can-i-use-system-drawing-in-wpf) –

cevap

14

Bir Görüntüyü bir WPF Görüntü denetimine yüklemek için bir System.Windows.Media.ImageSource kaynağına ihtiyacınız olacaktır.

Bir ImageSource nesneye senin Drawing.Image nesneyi dönüştürmek gerekir: NesneSil yönteminin

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; 
    } 

Deklarasyonu.

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

Bu çözümü seviyorum! Teşekkür ederim dostum! –

19

Aynı sorunu yaşıyorum ve birkaç yanıtı birleştirerek çözüyorum. Eğer bir dönüştürücü kullanıyorsanız this question and answers

+2

+1 Güzel ve düzenli – GETah

+0

'Bmp''yi atmayı unutma. – Peter

+0

** bu örnekte kullanılan ** bmp ** nedir? – juagicre

9

itibaren

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 

, gerçekte Image nesneye bağlayabilir. Image'u BitmapSource'a dönüştürecek bir IValueConverter oluşturmanız gerekir.

Gerçek işi yapmak için AlexDrenea'nın örnek kodunu dönüştürücüde kullandım.

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

XAML'inizde dönüştürücüyü eklemeniz gerekir.

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

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

Bağımsız. bunu sevdim – Basic