2014-10-08 54 views
12

我需要將System.Drawing.Bitmap轉換爲System.Windows.Media.ImageSource類,以便將其綁定到WizardPage(擴展WPF工具包)的HeaderImage控件中。 位圖被設置爲我編寫的程序集的資源。 它被引用的那樣:WPF - 將位圖轉換爲ImageSource

 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 我得到

return (ImageSource) c.ConvertFrom(Resources.my_banner); 我怎麼能初始化的ImageSource爲了避免此異常一個NullReferenceException?還是有另一種方式? 我想以後使用它像:提前

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

感謝您的任何答案。

+0

在這裏看到:什麼是一個NullReferenceException,我如何修復它(HTTP ://stackoverflow.com/q/4660142/1136211) – Clemens 2014-10-08 15:51:12

回答

6

我不認爲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格式;如果您正在處理其他格式,則可能需要添加轉換。

9

爲了搜索者的利益,我創建了一個基於這個更多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; 
     } 
    } 
} 
14

對於其他人來說,這個工程:

//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); }    
    } 
+3

你是我的英雄。用它來爲Bitmap類做一個擴展,它工作得非常好。工作很好。 – ARidder101 2016-09-23 20:07:52

+1

@ ARidder101我相信我從別人那裏得到了這個。 – dethSwatch 2016-09-24 16:24:30

0

對我來說,simpliest解決方案:

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();