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.
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.
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);
¡Me gusta esta solución! ¡Gracias hombre! –
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
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"/>
Autónomo. me gusta – Basic
posiblemente relacionadas: http://stackoverflow.com/questions/1118496/using-image-control-in-wpf-to-display-system-drawing-bitmap – Alain
duplicado posible de [WPF - ¿Puedo usar System.Drawing en wpf?] (Http://stackoverflow.com/questions/10663056/wpf-can-i-use-system-drawing-in-wpf) –