C# 在WPF中显示绘图.图像
我有一个System.Drawing.Image的实例 如何在我的WPF应用程序中显示这一点C# 在WPF中显示绘图.图像,c#,wpf,image,C#,Wpf,Image,我有一个System.Drawing.Image的实例 如何在我的WPF应用程序中显示这一点 我尝试了img.Source,但没有成功 要将图像加载到WPF图像控件,您需要System.Windows.Media.ImageSource 您需要将图形。图像对象转换为ImageSource对象: public static BitmapSource GetImageStream(Image myImage) { var bitmap = new Bitmap(myImag
我尝试了
img.Source
,但没有成功 要将图像加载到WPF图像控件,您需要System.Windows.Media.ImageSource
您需要将图形。图像对象转换为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);
我有同样的问题,并通过结合几个答案来解决它
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
从如果使用转换器,您实际上可以绑定到
图像
对象。您只需创建一个IValueConverter
,将图像
转换为位图源
我在转换器中使用了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"/>
可能相关:自包含的可能重复。我喜欢它不要忘记处理bmp
。bmp在本例中用于什么?@juagicre要在WPF window.bmp位图的System.Drawing.Image中显示的任何格式正确的System.Drawing.Bitmap都应该初始化,不是吗?嗯,我猜这是在代码的“…”部分完成的:)@juagicre是的,它应该。在“…”中,确切地说,或者它应该来自函数外部等。