我有一个 System.Drawing.Image 的实例。
如何在我的 WPF 应用程序中显示这个?
我试过了,img.Source
但这不起作用。
我有同样的问题并通过结合几个答案来解决它。
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
要将图像加载到 WPF 图像控件中,您将需要 System.Windows.Media.ImageSource。
您需要将您的 Drawing.Image 对象转换为 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;
}
DeleteObject 方法的声明。
[DllImport("gdi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeleteObject(IntPtr value);
如果您使用转换器,您实际上可以绑定到Image
对象。您只需要创建一个IValueConverter
将 转换Image
为BitmapSource
.
我在转换器中使用了 AlexDrenea 的示例代码来完成实际工作。
[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)
{
if (value is null || !(value is Image myImage))
{//ensure provided value is valid image.
return null;
}
if (myImage.Height > Int16.MaxValue || myImage.Width > Int16.MaxValue)
{//GetHbitmap will fail if either dimension is larger than max short value.
//Throwing here to reduce cpu and resource usage when error can be detected early.
throw new ArgumentException($"Cannot convert System.Drawing.Image with either dimension greater than {Int16.MaxValue} to BitmapImage.\nProvided image's dimensions: {myImage.Width}x{myImage.Height}", nameof(value));
}
using Bitmap bitmap = new Bitmap(myImage); //ensure Bitmap is disposed of after usefulness is fulfilled.
IntPtr bmpPt = bitmap.GetHbitmap();
try
{
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();
return bitmapSource;
}
finally
{ //done in a finally block to ensure this memory is not leaked regardless of exceptions.
DeleteObject(bmpPt);
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
在您的 XAML 中,您需要添加转换器。
<utils:ImageToBitmapSourceConverter x:Key="ImageConverter"/>
<Image Source="{Binding ThumbSmall, Converter={StaticResource ImageConverter}}"
Stretch="None"/>