Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Kent (of course) has the right answer. <strong>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)</strong> ... as this is a common problem/question.</p> <p>This takes three steps:</p> <ol> <li>Use an image converter in your binding.</li> <li>Create the converter.</li> <li>Declare the converter in your resources.</li> </ol> <p><strong>Here is how you would use an image converter in your binding</strong>:</p> <pre><code>&lt;Setter Property="MenuItem.Icon" Value="{Binding Path=Icon, Converter={StaticResource imageConverter}}" /&gt; </code></pre> <p><strong>And, here is the code for the converter</strong> (put it into a file called ImageConverter.cs) and add it to your project:</p> <pre><code>[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); } </code></pre> <p><strong>Here is how you declare it in your resources section</strong> (note the local namespace that you will have to add):</p> <pre><code>&lt;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" &gt; &lt;Window.Resources&gt; &lt;local:ImageConverter x:Key="imageConverter"/&gt; &lt;/Window.Resources&gt; &lt;!-- some xaml snipped for clarity --&gt; &lt;/Window&gt; </code></pre> <p>And that's it!</p> <hr> <p><strong>Update</strong></p> <p>After doing a quick search for similar questions, I noticed that <a href="https://stackoverflow.com/users/1242/lars-truijens">Lars Truijens</a> pointed out <a href="https://stackoverflow.com/questions/1118496/using-image-control-in-wpf-to-display-system-drawing-bitmap/1118557#1118557">here</a> that the previous converter implementation leaks. I have updated the converter code above ... so that it doesn't leak.</p> <p>For more information on the cause of the leak, see the remarks section on this MSDN <a href="http://msdn.microsoft.com/en-us/library/1dz311e4(v=VS.100).aspx" rel="nofollow noreferrer">link</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload