WPF - 将Bitmap转换为ImageSource

时间:2014-10-08 15:24:49

标签: c# wpf bitmap type-conversion imagesource

我需要将System.Drawing.Bitmap转换为System.Windows.Media.ImageSource类,以便将其绑定到WizardPage(扩展WPF工具包)的HeaderImage控件中。 位图设置为我编写的程序集的资源。 它被引用如下:

     public Bitmap GetBitmap
     {
         get
         {
             Bitmap bitmap = new Bitmap(Resources.my_banner);
             return bitmap;
         }
     }

     public ImageSource HeaderBitmap
     {
         get
         {
             ImageSourceConverter c = new ImageSourceConverter();
             return (ImageSource) c.ConvertFrom(GetBitmap);
         }
     }

转发器是我在这里找到的:http://www.codeproject.com/Questions/621920/How-to-convert-Bitmap-to-ImageSource 我在

得到一个NullReferenceException

return (ImageSource) c.ConvertFrom(Resources.my_banner); 如何初始化ImageSource以避免此异常?或者还有另一种方式吗? 我想在之后使用它:

        <xctk:WizardPage x:Name="StartPage" Height="500" Width="700"
                     HeaderImage="{Binding HeaderBitmap}" Enter="StartPage_OnEnter"

提前感谢您的任何答案。

6 个答案:

答案 0 :(得分:19)

对于其他人,这有效:

    //If you get 'dllimport unknown'-, then add 'using System.Runtime.InteropServices;'
    [DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool DeleteObject([In] IntPtr hObject);

    public ImageSource ImageSourceForBitmap(Bitmap bmp)
    {
        var handle = bmp.GetHbitmap();
        try
        {
            return Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
        }
        finally { DeleteObject(handle); }               
    }

答案 1 :(得分:11)

为了搜索者的利益,我基于更多detailed solution创建了一个快速转换器。

到目前为止没有问题。

using System;
using System.Drawing;
using System.IO;
using System.Windows.Media.Imaging;

namespace XYZ.Helpers
{
    public class ConvertBitmapToBitmapImage
    {
        /// <summary>
        /// Takes a bitmap and converts it to an image that can be handled by WPF ImageBrush
        /// </summary>
        /// <param name="src">A bitmap image</param>
        /// <returns>The image as a BitmapImage for WPF</returns>
        public BitmapImage Convert(Bitmap src)
        {
            MemoryStream ms = new MemoryStream();
            ((System.Drawing.Bitmap)src).Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            BitmapImage image = new BitmapImage();
            image.BeginInit();
            ms.Seek(0, SeekOrigin.Begin);
            image.StreamSource = ms;
            image.EndInit();
            return image;
        }
    }
}

答案 2 :(得分:8)

我不相信ImageSourceConverter会转换为System.Drawing.Bitmap。但是,您可以使用以下内容:

public static BitmapSource CreateBitmapSourceFromGdiBitmap(Bitmap bitmap)
{
    if (bitmap == null)
        throw new ArgumentNullException("bitmap");

    var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

    var bitmapData = bitmap.LockBits(
        rect,
        ImageLockMode.ReadWrite,
        PixelFormat.Format32bppArgb);

    try
    {
        var size = (rect.Width * rect.Height) * 4;

        return BitmapSource.Create(
            bitmap.Width,
            bitmap.Height,
            bitmap.HorizontalResolution,
            bitmap.VerticalResolution,
            PixelFormats.Bgra32,
            null,
            bitmapData.Scan0,
            size,
            bitmapData.Stride);
    }
    finally
    {
        bitmap.UnlockBits(bitmapData);
    }
}

此解决方案要求源图像采用Bgra32格式;如果您正在处理其他格式,则可能需要添加转换。

答案 3 :(得分:5)

dethSwatch-感谢您在上面的回答!它极大地帮助了!实现它之后,我得到了想要的行为,但是我发现程序的另一部分出现了内存/句柄问题。我对代码进行了如下更改,使其更加冗长,问题消失了。再次谢谢你!

    [DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool DeleteObject([In] IntPtr hObject);

    public ImageSource ImageSourceForBitmap(Bitmap bmp)
    {
        var handle = bmp.GetHbitmap();
        try
        {
            ImageSource newSource = Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

            DeleteObject(handle);
            return newSource;
        }
        catch (Exception ex)
        {
            DeleteObject(handle);
            return null;
        }
    }

答案 4 :(得分:0)

对我来说最简单的解决方案:

ImageBrush myBrush = new ImageBrush();
var bitmap = System.Drawing.Image.FromFile("pic1.bmp");
Bitmap bitmap = new System.Drawing.Bitmap(image);//it is in the memory now
var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(),IntPtr.Zero,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions());
myBrush.ImageSource = bitmapSource;
cover.MainGrid.Background = myBrush;
cover.Show();
bitmap.Dispose();

答案 5 :(得分:0)

void Draw()
{
    System.Drawing.Bitmap bmp = new Bitmap();
    ...
    Image img = new Image();
    img.Source = BitmapToImageSource(bmp)
 }


private BitmapImage BitmapToImageSource(System.Drawing.Bitmap bitmap)
{
    using (MemoryStream memory = new MemoryStream())
    {
        bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
        memory.Position = 0;
        BitmapImage bitmapimage = new BitmapImage();
        bitmapimage.BeginInit();
        bitmapimage.StreamSource = memory;
        bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapimage.EndInit();
        return bitmapimage;
    }
}