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.


I borrowed a lot of  this example from  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 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()
            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"))
        private void Button_Click(object sender, RoutedEventArgs e)
            ButtonImage.Source = new BitmapImage(new Uri(EmbeddedResources[ButtonClicks % 2].ToString(), UriKind.Relative));
<Window x:Class="WpfApplication1.Window1"
    Title="Window1" Height="300" Width="300">
        <Button Click="Button_Click">
        <Image Name="ButtonImage" Source="Images/IMG_7680.jpg" />


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 (  For a family only feed use the following URL (

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

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s