我有一个System.Drawing.Bitmap实例,并希望将其以system . windows . media . image . bitmapimage的形式提供给我的WPF应用程序。

最好的方法是什么?


当前回答

我在一家图像供应商工作,写了一个WPF到我们的图像格式的适配器,它类似于System.Drawing.Bitmap。

我写了这个KB来向我们的客户解释:

http://www.atalasoft.com/kb/article.aspx?id=10156

这里有代码可以做到这一点。你需要用Bitmap替换AtalaImage,并做与我们正在做的相同的事情——这应该是相当简单的。

其他回答

最简单的方法是直接从文件中生成WPF位图。

否则你将不得不使用system . windows . interop . image . createbitmapsourcefromhbitmap。

我在一家图像供应商工作,写了一个WPF到我们的图像格式的适配器,它类似于System.Drawing.Bitmap。

我写了这个KB来向我们的客户解释:

http://www.atalasoft.com/kb/article.aspx?id=10156

这里有代码可以做到这一点。你需要用Bitmap替换AtalaImage,并做与我们正在做的相同的事情——这应该是相当简单的。

感谢Hallgrim,以下是我最终得到的代码:

ScreenCapture = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
   bmp.GetHbitmap(), 
   IntPtr.Zero, 
   System.Windows.Int32Rect.Empty, 
   BitmapSizeOptions.FromWidthAndHeight(width, height));

我也最终绑定到BitmapSource,而不是在我最初的问题中BitmapImage

如何从MemoryStream加载它?

using(MemoryStream memory = new MemoryStream())
{
    bitmap.Save(memory, ImageFormat.Png);
    memory.Position = 0;
    BitmapImage bitmapImage = new BitmapImage();
    bitmapImage.BeginInit();
    bitmapImage.StreamSource = memory;
    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
    bitmapImage.EndInit();
}

我知道这个问题已经被回答了,但是这里有两个扩展方法(用于。net 3.0+)可以进行转换。:)

        /// <summary>
    /// Converts a <see cref="System.Drawing.Image"/> into a WPF <see cref="BitmapSource"/>.
    /// </summary>
    /// <param name="source">The source image.</param>
    /// <returns>A BitmapSource</returns>
    public static BitmapSource ToBitmapSource(this System.Drawing.Image source)
    {
        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(source);

        var bitSrc = bitmap.ToBitmapSource();

        bitmap.Dispose();
        bitmap = null;

        return bitSrc;
    }

    /// <summary>
    /// Converts a <see cref="System.Drawing.Bitmap"/> into a WPF <see cref="BitmapSource"/>.
    /// </summary>
    /// <remarks>Uses GDI to do the conversion. Hence the call to the marshalled DeleteObject.
    /// </remarks>
    /// <param name="source">The source bitmap.</param>
    /// <returns>A BitmapSource</returns>
    public static BitmapSource ToBitmapSource(this System.Drawing.Bitmap source)
    {
        BitmapSource bitSrc = null;

        var hBitmap = source.GetHbitmap();

        try
        {
            bitSrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                hBitmap,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
        }
        catch (Win32Exception)
        {
            bitSrc = null;
        }
        finally
        {
            NativeMethods.DeleteObject(hBitmap);
        }

        return bitSrc;
    }

和NativeMethods类(安抚FxCop)

    /// <summary>
/// FxCop requires all Marshalled functions to be in a class called NativeMethods.
/// </summary>
internal static class NativeMethods
{
    [DllImport("gdi32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    internal static extern bool DeleteObject(IntPtr hObject);
}