Tuesday, February 8, 2011

Accessing .NET Embedded Resources

Accessing resources of any kind is usually done once inside a helper class or other mechanism that hides away the implementation details of how the resources are accessed. Or at least thats how I usually do it. Trouble is whenever its time to remember how to access the various kinds of resources I have to strain the remembering gland. Time for a post to store the information and free up some valuable brain memory kilobytes.

Here's a screen shot of my demo application showing different ways of accessing resources.


Some images are populated using Xaml and others using standard C# mechanisms.
Here's the structure of the application, note the two different images included in the Assets folder. One is set to the newer "Resource" content-type and the other to the old "EmbeddedResource".  The image in the DifferentAssembly is also a "Resource".  (Prefer Resource to the old EmbeddedResouce).



Xaml:

<Window 
    x:Class="ResourceAccessExample.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Height="350" 
    Title="MainWindow" 
    Width="525">

    <ScrollViewer>
        <StackPanel>
            <Border 
                BorderBrush="Black" 
                BorderThickness="1" 
                Margin="10" 
                Padding="10">
                <StackPanel>
                    <Image 
                        MaxHeight="75" 
                        Source="pack://application:,,,/assets/ChessMce.png" 
                        />
                    <TextBlock Text="Pack Url into same assembly" />
                </StackPanel>
            </Border>
            <Border 
                BorderBrush="Black" 
                BorderThickness="1" 
                Margin="10" 
                Padding="10">
                <StackPanel>
                    <Image 
                        MaxHeight="75" 
                        Source="pack://application:,,,/DifferentAssembly;component/UserGroup.png" 
                        />
                    <TextBlock Text="Pack Url into different assembly" />
                </StackPanel>
            </Border>
            <Border 
                BorderBrush="Black" 
                BorderThickness="1" 
                Margin="10" 
                Padding="10">
                <StackPanel>
                    <Image 
                        MaxHeight="75" 
                        Source="assets/ChessMce.png" 
                        />
                    <TextBlock Text="Relative Url into same assembly" />
                </StackPanel>
            </Border>
            <Border 
                BorderBrush="Black" 
                BorderThickness="1" 
                Margin="10" 
                Padding="10">
                <StackPanel>
                    <Image 
                        x:Name="Image2" 
                        MaxHeight="75" 
                        />
                    <TextBlock Text="Pulled from Resource Manifest using code Application static helper ('Resource')" />
                </StackPanel>
            </Border>
            <Border 
                BorderBrush="Black" 
                BorderThickness="1" 
                Margin="10" 
                Padding="10">
                <StackPanel>
                    <Image 
                        x:Name="Image3" 
                        MaxHeight="75" 
                        />
                    <TextBlock Text="Pulled from Resource Manifest using standard .NET Resource classes ('Resource')" />
                </StackPanel>
            </Border>
            <Border 
                BorderBrush="Black" 
                BorderThickness="1" 
                Margin="10" 
                Padding="10">
                <StackPanel>
                    <Image 
                        x:Name="Image4" 
                        MaxHeight="75" 
                        />
                    <TextBlock Text="Pulled from an old EmbeddedResource using standard .NET Resource classes ('Resource')" />
                </StackPanel>
            </Border>
        </StackPanel>
    </ScrollViewer>

</Window>

And the C# Code behind that populates the last three images using standard C# coding techniques:


public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += OnLoaded;
        }

        private void OnLoaded(object s, RoutedEventArgs e)
        {
            // Pull using the Application object (wpf only)
            var streamResourceInfo2 = Application.GetResourceStream(new Uri("assets/ChessMce.png", UriKind.Relative));
            if (streamResourceInfo2 != null)
            {
                var image = new BitmapImage();
                image.BeginInit();
                image.StreamSource = streamResourceInfo2.Stream;
                image.EndInit();
                this.Image2.Source = image;
            }

            // Using standard .NET resource classes.
            var assem = Assembly.GetEntryAssembly();
            var resourceManager = new ResourceManager(assem.GetName().Name + ".g", assem);
            using (ResourceSet set = resourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true))
            {
                // Can loop thru them
                foreach (DictionaryEntry pair in set)
                {
                    Debug.WriteLine(pair.Key.ToString());
                }

                // Access an item directly
                var image = new BitmapImage();
                image.BeginInit();
                image.StreamSource = (Stream)set.GetObject("assets/ChessMce.png", true);
                image.EndInit();
                this.Image3.Source = image;
            }

            // Using an old Embedded Resource
            // this line of code is useful to figure out the name Vs has given the resource! The name is case sensitive.
            // var names = Assembly.GetExecutingAssembly().GetManifestResourceNames();
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ResourceAccessExample.Assets.FreeCellMCE.png"))
            {
                var image = new BitmapImage();
                image.BeginInit();
                image.StreamSource = stream;
                image.EndInit();
                this.Image4.Source = image;
            }
        }
    }
Brain capacity recycled...

No comments:

Post a Comment