Managing Embedded Resources
<UserControl x:Class='SilverlightApplication3.MainPage'
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
xmlns:d='http://schemas.microsoft.com/expression/blend/2008'
xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006'
mc:Ignorable='d'
d:DesignWidth='640'
d:DesignHeight='480'>
<StackPanel>
<ListBox x:Name="ResourceNames" Background="Transparent" HorizontalAlignment="Stretch" Margin="4,4,15,4" SelectionChanged="ResourceNames_SelectionChanged">
<ListBox.Foreground>
<SolidColorBrush Color="#FFD18726"/>
</ListBox.Foreground>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Background" Value="Transparent"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<Button x:Name="RetrieveResourceNames" Content="Retrieve Resource Names" d:LayoutOverrides="VerticalAlignment, Height" Click="RetrieveResourceNames_Click"/>
<TextBlock HorizontalAlignment="Stretch" Margin="53,0,74,4" VerticalAlignment="Bottom" Text="Select a Resource to Display" TextWrapping="Wrap" Foreground="#FFFFFFFF" Height="22"/>
<Border Margin="30,-15,30,35" HorizontalAlignment="Stretch" BorderBrush="#FF000000" x:Name="ImageBorder" RenderTransformOrigin="0.5,0.5" Visibility="Collapsed" Height="310" VerticalAlignment="Stretch" d:LayoutOverrides="Height">
<Border.Background>
<SolidColorBrush Color="#FFD28826"/>
</Border.Background>
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform AngleX="5" AngleY="5"/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Border.RenderTransform>
<Image x:Name="ImageDisplay" Margin="5,5,5,5" Width="400" Height="300" OpacityMask="#FF000000" />
</Border>
</StackPanel>
</UserControl>
//File: Page.xaml.cs
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
namespace SilverlightApplication3
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void RetrieveResourceNames_Click(object sender, RoutedEventArgs e)
{
Assembly app = Assembly.GetExecutingAssembly();
string[] resources = app.GetManifestResourceNames();
ResourceNames.Items.Clear();
foreach (string s in resources)
{
ResourceNames.Items.Add(s);
}
}
private void ResourceNames_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!(ResourceNames.SelectedItem.ToString().Contains("resources")))
{
Assembly app = Assembly.GetExecutingAssembly();
using (Stream stream = app.GetManifestResourceStream(ResourceNames.SelectedItem.ToString()))
{
BitmapImage bImage = new BitmapImage();
bImage.SetSource(stream);
ImageDisplay.Source = bImage;
ImageBorder.Visibility = Visibility.Visible;
}
}
}
}
}
Related examples in the same category