Storing Wpf Image Resources

Storing WPF Image Resources

If you will use the image in multiple places, then it's worth loading the image data only once into memory and then sharing it between all Image elements.

To do this, create a BitmapSource as a resource somewhere:

<BitmapImage x:Key="MyImageSource" UriSource="../Media/Image.png" />

Then, in your code, use something like:

<Image Source="{StaticResource MyImageSource}" />

In my case, I found that I had to set the Image.png file to have a build action of Resource rather than just Content. This causes the image to be carried within your compiled assembly.

Set Image source from Resources in WPF application

First you will have to include the image in your project with BuildAction Resource

Then you can use it directly in your Image

 <Image Source="Img100.jpg"/>

Or make a reusable resource of the image

App.xaml (or other resource dictionary)

<Application.Resources>
<BitmapImage x:Key="myImage" UriSource="Img100.jpg" />
</Application.Resources>

Element:

<Image Source="{StaticResource myImage}" />

Display image from resources by binding -WPF

First you must add your Image into a Resource File in the Solution Explorer. Next you must set the Build Action of your Image to Resource and then you can use it in the XAML Code like this:

<UserControl>
<UserControl.Resources>
<ResourceDictionary>
<BitmapImage x:Key="name" UriSource="Resources/yourimage.bmp" />
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Image Source="{StaticResource name}"/>
</Grid>
</UserControl>

How to reference image resources in XAML?

If the image is in your resources folder and its build action is set to Resource. You can reference the image in XAML as follows:

"pack://application:,,,/Resources/Search.png"

Assuming you do not have any folder structure under the Resources folder and it is an application. For example I use:

ImageSource="pack://application:,,,/Resources/RibbonImages/CloseButton.png"

when I have a folder named RibbonImages under Resources folder.

wpf image resources and changing image in wpf control at runtime

First, make sure you've defined your image resources like this:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ImageSource x:Key="image1">images/image1.jpg</ImageSource>
<ImageSource x:Key="image2">images/image2.jpg</ImageSource>
</ResourceDictionary>

Secondly, I'm assuming that your WPF dictionary is in its own file. Now you have to make sure you've merged your dictionary into your main window's XAML (skip this step if your resource dictionary is defined inside of the window's XAML). In your window's XAML file, make sure you have something like this:

<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="myDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>

Now, in your code-behind, you can use the FindResource() method to locate your image resource by it's key name (the value of the x:Key attribute on the ImageSource in the resource dictionary) like so:

imageControl.Source = (ImageSource)FindResource("image1");

Hope this helps!

XAML: How to use Image type resources declared in the Resources node?

Use BitmapImage instead of Image as resource type:

<Window.Resources>
<BitmapImage x:Key="MyIcon"
UriSource="pack://application:,,,/Resources/Images/myicon.png"/>
</Window.Resources>
...
<WrapPanel Height="50">
<Image Source="{StaticResource MyIcon}"/>
</WrapPanel>

You also don't have to write a full Resource File Pack URI in XAML, so you could write the resource declaration like this:

<BitmapImage x:Key="MyIcon" UriSource="/Resources/Images/myicon.png"/>


Related Topics



Leave a reply



Submit