title: Setting WPF image programmatically from a resource
body: <p>I've been looking for a better answer to my problem, and even when I solved it (not very elegantly) I would like to know if there's any other way.
  I've inherited a project I cannot modify too much, I mean, I can only add some other properties or override methods but not changing the base class or interface.</p>
  
  <p>This project contains a class that inherits a WPF grid to modify certain aspects of the behavior. I have to add a new column (by code) with an image. Every single image is already stored into a Resource assembly and declared through static properties, for example, an icon I need is declared like this:</p>
  
  <pre><code>MyIcon.Source="{x:Static images:Common.Size_22.icon}"
  </code></pre>
  
  <p>I need to use this icon for my column and I tried every kind of declaration through a package and it didn't work. Finally I decided to embed this Bitmap sending it through a property:</p>
  
  <pre><code>&lt;mygrid:MyGridControl Model="{Binding}" Refresh="{Binding Refresh}" 
  CommentsImageSource="{x:Static images:Common.Size_22.icon}" /&gt;
  </code></pre>
  
  <p>When I create the image cell I have to also create an image object and set source using a conversion between Bitmap and BitmapImage</p>
  
  <pre><code>image.Source = ToBitmapSource((System.Drawing.Bitmap)CommentsImageSource);
  </code></pre>
  
  <p>It works but I'm very disappointed with this resolution, is there any better way to do it? I couldn't reference the source file to avoid sending a Bitmap by property.</p>