我有一个System.Drawing.Bitmap实例,并希望将其以system . windows . media . image . bitmapimage的形式提供给我的WPF应用程序。
最好的方法是什么?
我有一个System.Drawing.Bitmap实例,并希望将其以system . windows . media . image . bitmapimage的形式提供给我的WPF应用程序。
最好的方法是什么?
当前回答
我来到这个问题,因为我试图做同样的事情,但在我的情况下,位图是来自资源/文件。我发现最好的解决方案如下所示:
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.aspx
// Create the image element.
Image simpleImage = new Image();
simpleImage.Width = 200;
simpleImage.Margin = new Thickness(5);
// Create source.
BitmapImage bi = new BitmapImage();
// BitmapImage.UriSource must be in a BeginInit/EndInit block.
bi.BeginInit();
bi.UriSource = new Uri(@"/sampleImages/cherries_larger.jpg",UriKind.RelativeOrAbsolute);
bi.EndInit();
// Set the image source.
simpleImage.Source = bi;
其他回答
感谢Hallgrim,以下是我最终得到的代码:
ScreenCapture = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bmp.GetHbitmap(),
IntPtr.Zero,
System.Windows.Int32Rect.Empty,
BitmapSizeOptions.FromWidthAndHeight(width, height));
我也最终绑定到BitmapSource,而不是在我最初的问题中BitmapImage
我在一家图像供应商工作,写了一个WPF到我们的图像格式的适配器,它类似于System.Drawing.Bitmap。
我写了这个KB来向我们的客户解释:
http://www.atalasoft.com/kb/article.aspx?id=10156
这里有代码可以做到这一点。你需要用Bitmap替换AtalaImage,并做与我们正在做的相同的事情——这应该是相当简单的。
我的观点来自于一些资源。https://stackoverflow.com/a/7035036 https://stackoverflow.com/a/1470182/360211
using System;
using System.Drawing;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Security;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using Microsoft.Win32.SafeHandles;
namespace WpfHelpers
{
public static class BitmapToBitmapSource
{
public static BitmapSource ToBitmapSource(this Bitmap source)
{
using (var handle = new SafeHBitmapHandle(source))
{
return Imaging.CreateBitmapSourceFromHBitmap(handle.DangerousGetHandle(),
IntPtr.Zero, Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
}
[DllImport("gdi32")]
private static extern int DeleteObject(IntPtr o);
private sealed class SafeHBitmapHandle : SafeHandleZeroOrMinusOneIsInvalid
{
[SecurityCritical]
public SafeHBitmapHandle(Bitmap bitmap)
: base(true)
{
SetHandle(bitmap.GetHbitmap());
}
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
protected override bool ReleaseHandle()
{
return DeleteObject(handle) > 0;
}
}
}
}
最简单的方法是直接从文件中生成WPF位图。
否则你将不得不使用system . windows . interop . image . createbitmapsourcefromhbitmap。
我花了一些时间让转换双向工作,所以这里是我提出的两个扩展方法:
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Media.Imaging;
public static class BitmapConversion {
public static Bitmap ToWinFormsBitmap(this BitmapSource bitmapsource) {
using (MemoryStream stream = new MemoryStream()) {
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapsource));
enc.Save(stream);
using (var tempBitmap = new Bitmap(stream)) {
// According to MSDN, one "must keep the stream open for the lifetime of the Bitmap."
// So we return a copy of the new bitmap, allowing us to dispose both the bitmap and the stream.
return new Bitmap(tempBitmap);
}
}
}
public static BitmapSource ToWpfBitmap(this Bitmap bitmap) {
using (MemoryStream stream = new MemoryStream()) {
bitmap.Save(stream, ImageFormat.Bmp);
stream.Position = 0;
BitmapImage result = new BitmapImage();
result.BeginInit();
// According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed."
// Force the bitmap to load right now so we can dispose the stream.
result.CacheOption = BitmapCacheOption.OnLoad;
result.StreamSource = stream;
result.EndInit();
result.Freeze();
return result;
}
}
}