Kent (of course) has the right answer. But I thought I would go ahead and post the code for the converter that converts from the System.Drawing.Bitmap (Windows Forms) to a System.Windows.Windows.Media.BitmapSource (WPF) ... as this is a common problem/question.
This takes three steps:
- Use an image converter in your binding.
- Create the converter.
- Declare the converter in your resources.
Here is how you would use an image converter in your binding:
<Setter
Property="MenuItem.Icon"
Value="{Binding Path=Icon, Converter={StaticResource imageConverter}}"
/>
And, here is the code for the converter (put it into a file called ImageConverter.cs) and add it to your project:
[ValueConversion(typeof(Image), typeof(string))]
public class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
BitmapSource bitmapSource;
IntPtr bitmap = ((Bitmap)value).GetHbitmap();
try
{
bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(bitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
finally
{
DeleteObject(bitmap);
}
return bitmapSource;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
[DllImport("gdi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
static extern int DeleteObject(IntPtr o);
}
Here is how you declare it in your resources section (note the local namespace that you will have to add):
<Window
x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication2"
>
<Window.Resources>
<local:ImageConverter x:Key="imageConverter"/>
</Window.Resources>
<!-- some xaml snipped for clarity -->
</Window>
And that's it!
Update
After doing a quick search for similar questions, I noticed that Lars Truijens pointed out here that the previous converter implementation leaks. I have updated the converter code above ... so that it doesn't leak.
For more information on the cause of the leak, see the remarks section on this MSDN link.