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.
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.
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);
Bu çözümü seviyorum! Teşekkür ederim dostum! –
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
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"/>
Bağımsız. bunu sevdim – Basic
Benzeyen: http://stackoverflow.com/questions/1118496/using-image-control-in-wpf-to-display-system-drawing-bitmap [wpf ait – Alain
Olası yinelenen - ı kullanabilir miyim Wpf'de System.Drawing?] (Http://stackoverflow.com/questions/10663056/wpf-can-i-use-system-drawing-in-wpf) –