Storing WPF Image Resources

后端 未结 10 1854
眼角桃花
眼角桃花 2020-11-22 11:17

For a WPF application which will need 10 - 20 small icons and images for illustrative purposes, is storing these in the assembly as embedded resources the right way to go?

相关标签:
10条回答
  • 2020-11-22 11:23

    Yes, it's the right way. You can use images in the Resource file using a path:

    <StackPanel Orientation="Horizontal">
        <CheckBox  Content="{Binding Nname}" IsChecked="{Binding IsChecked}"/>
        <Image Source="E:\SWorking\SharePointSecurityApps\SharePointSecurityApps\SharePointSecurityApps.WPF\Images\sitepermission.png"/>
        <TextBlock Text="{Binding Path=Title}"></TextBlock>
    </StackPanel>
    
    0 讨论(0)
  • 2020-11-22 11:27

    Some people are asking about doing this in code and not getting an answer.

    After spending many hours searching I found a very simple method, I found no example and so I share mine here which works with images. (mine was a .gif)

    Summary:

    It returns a BitmapFrame which ImageSource "destinations" seem to like.

    Use:

    doGetImageSourceFromResource ("[YourAssemblyNameHere]", "[YourResourceNameHere]");
    

    Method:

    static internal ImageSource doGetImageSourceFromResource(string psAssemblyName, string psResourceName)
    {
        Uri oUri = new Uri("pack://application:,,,/" +psAssemblyName +";component/" +psResourceName, UriKind.RelativeOrAbsolute);
        return BitmapFrame.Create(oUri);
    }
    

    Learnings:

    From my experiences the pack string is not the issue, check your streams and especially if reading it the first time has set the pointer to the end of the file and you need to re-set it to zero before reading again.

    I hope this saves you the many hours I wish this piece had for me!

    0 讨论(0)
  • 2020-11-22 11:29

    If you're using Blend, to make it extra easy and not have any trouble getting the correct path for the Source attribute, just drag and drop the image from the Project panel onto the designer.

    0 讨论(0)
  • 2020-11-22 11:34

    I found to be the best practice of using images, videos, etc. is:

    • Change your files "Build action" to "Content". Be sure to check Copy to build directory.
      • Found on the "Right-Click" menu at the Solution Explorer window.
    • Image Source in the following format:
      • "/«YourAssemblyName»;component/«YourPath»/«YourImage.png»"

    Example

    <Image Source="/WPFApplication;component/Images/Start.png" />
    

    Benefits:

    • Files are not embedded into the assembly.
      • The Resource Manager will raise some memory overflow problems with too many resources (at build time).
    • Can be called between assemblies.
    0 讨论(0)
  • 2020-11-22 11:36

    Yes, it is the right way.

    You could use the image in the resource file just using the path:

    <Image Source="..\Media\Image.png" />
    

    You must set the build action of the image file to "Resource".

    0 讨论(0)
  • 2020-11-22 11:37

    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.

    0 讨论(0)
提交回复
热议问题