Using Resources with WPF and Winforms

I had a developer ask me how to use Resources the other day.  Honestly I had never used them for any production system and so I didn’t know.  So I decided to find out.  Below is the code on how to do it in WPF and then after that how you would do it in Winforms.

WPF

I borrowed a lot of  this example from http://mostlytech.blogspot.com/2007/09/enumerating-xaml-baml-files-in-assembly.html.  The article I link to showed how to iterate through Resources files that are in your solution with the Build Action set to Resource.  I then use that to put an image on a button that alternates every time it is clicked.  At http://forums.msdn.microsoft.com/en-US/wpf/thread/1bb025e8-a20a-43c4-a760-8666c63ff624/ it explains how to work with Resources that you define in the Resource tab in the Project Properties.  You can also set an image directly in XAML as shown below

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Media.Imaging;
 
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private int ButtonClicks = 0;
        List<object> EmbeddedResources = new List<object>();
        public Window1()
        {
            InitializeComponent();
            Assembly asm = Assembly.GetExecutingAssembly();
            Stream stream = asm.GetManifestResourceStream(asm.GetName().Name + ".g.resources");
 
            using (ResourceReader reader = new ResourceReader(stream))
            {
                foreach (DictionaryEntry entry in reader)
                {
                    if (entry.Key.ToString().Contains(".jpg"))
                    EmbeddedResources.Add(entry.Key);
                }
            }
 
        }
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
 
            ButtonClicks++;
            ButtonImage.Source = new BitmapImage(new Uri(EmbeddedResources[ButtonClicks % 2].ToString(), UriKind.Relative));
        }
    }
}
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Button Click="Button_Click">
        <Image Name="ButtonImage" Source="Images/IMG_7680.jpg" />
        </Button>
    </Grid>
</Window>

Winforms

I didn’t spend as much time looking at the Winforms side of things, but here is a snippet of code that can be used.  Note that I set the Build Action for the Image file to EmbeddedResource because that is what all the examples said to do.  Bitmap has an overload that allows it to resolve Resource References.  The name of the resource becomes <Namespace>.<Path>.<Filename>.

pictureBox1.Image = new Bitmap(typeof(Form1), "Images.IMG_7680.jpg");

Note: I use this blog to post both Personal and Technical articles.  For a technical only feed use the following URL (http://bryanandnoel.spaces.live.com/category/technology/feed.rss).  For a family only feed use the following URL (http://bryanandnoel.spaces.live.com/category/family/feed.rss)

Technorati Tags: ,,,
Advertisements
This entry was posted in Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s