Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK, here is an improved GetImageSource (name changed to reflect its real purpose).</p> <pre><code> /// &lt;summary&gt; /// Get an ImageSource from the ResourceDictionary referred to by the /// &lt;paramref name="uriDictionary"/&gt;. /// &lt;/summary&gt; /// &lt;param name="keyName"&gt;The ResourceDictionary key of the ImageSource /// to retrieve.&lt;/param&gt; /// &lt;param name="uriDictionary"&gt;The Uri to the XAML file that holds /// the ResourceDictionary.&lt;/param&gt; /// &lt;returns&gt;&lt;c&gt;null&lt;/c&gt; on failure, the requested &lt;c&gt;ImageSource&lt;/c&gt; /// on success.&lt;/returns&gt; /// &lt;remarks&gt;If the requested resource is an &lt;c&gt;Image&lt;/c&gt; instead of /// an &lt;c&gt;ImageSource&lt;/c&gt;, /// then the &lt;c&gt;image.Source&lt;/c&gt; is returned.&lt;/remarks&gt; public static ImageSource GetImageSource(String keyName, Uri uriDictionary) { if (String.IsNullOrEmpty(keyName)) throw new ArgumentNullException("keyName"); if (null == uriDictionary) throw new ArgumentNullException("uriDictionary"); ResourceDictionary imagesDictionary = new ResourceDictionary(); imagesDictionary.Source = uriDictionary; var var = imagesDictionary[keyName]; Object blob = imagesDictionary[keyName]; Debug.WriteLine(String.Format( "error: GetImageSource( '{0}', '{1}' )" + " value is: {2}", keyName, uriDictionary, (null == blob) ? "null" : blob.GetType().FullName)); if (null != blob) { if (blob is ImageSource) { return blob as ImageSource; } if (blob is Image) { Image image = blob as Image; return image.Source; } if (blob is System.Drawing.Image) { System.Drawing.Image dImage = blob as System.Drawing.Image; MemoryStream mem = new MemoryStream(); dImage.Save(mem, System.Drawing.Imaging.ImageFormat.MemoryBmp); mem.Position = 0; BitmapDecoder decode = new BmpBitmapDecoder( mem, BitmapCreateOptions.None, BitmapCacheOption.None); return decode.Frames[0]; } Debug.WriteLine(String.Format( "error: GetImageSource( '{0}', '{1}' )" + " can't handle type: {2}", keyName, uriDictionary, blob.GetType().FullName)); } return null; } </code></pre> <p>And to use it you would do this:</p> <pre><code>String packText = String.Format("pack://application:,,,/{0};component/{1}", Assembly.GetEntryAssembly().FullName, "ImageDictionary.xaml"); Uri imageUri = new Uri(packText); // or if you prefer: // Uri imageUri = new Uri("file:///.../ImageDictionary.xaml"); // ImageSource source = GetImageSource(imageKey, imageUri); if (null != source) { this.Image.Source = source; } else { // bail ... } </code></pre> <p>The <code>Drawing.Image</code> case will handle BMP, PNG, JPG, GIF, etc., even though I used <code>BmpBitmapDecoder</code>. But be aware that XAML images stretch very prettily but <code>Drawing.Image</code> does not.</p> <p>-Jesse</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. 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