Storing WPF Image Resources

后端 未结 10 1855
眼角桃花
眼角桃花 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:43

    In code to load a resource in the executing assembly where my image Freq.png was in the folder Icons and defined as Resource:

    this.Icon = new BitmapImage(new Uri(@"pack://application:,,,/" 
        + Assembly.GetExecutingAssembly().GetName().Name 
        + ";component/" 
        + "Icons/Freq.png", UriKind.Absolute)); 
    

    I also made a function:

    /// <summary>
    /// Load a resource WPF-BitmapImage (png, bmp, ...) from embedded resource defined as 'Resource' not as 'Embedded resource'.
    /// </summary>
    /// <param name="pathInApplication">Path without starting slash</param>
    /// <param name="assembly">Usually 'Assembly.GetExecutingAssembly()'. If not mentionned, I will use the calling assembly</param>
    /// <returns></returns>
    public static BitmapImage LoadBitmapFromResource(string pathInApplication, Assembly assembly = null)
    {
        if (assembly == null)
        {
            assembly = Assembly.GetCallingAssembly();
        }
    
        if (pathInApplication[0] == '/')
        {
            pathInApplication = pathInApplication.Substring(1);
        }
        return new BitmapImage(new Uri(@"pack://application:,,,/" + assembly.GetName().Name + ";component/" + pathInApplication, UriKind.Absolute)); 
    }
    

    Usage (assumption you put the function in a ResourceHelper class):

    this.Icon = ResourceHelper.LoadBitmapFromResource("Icons/Freq.png");
    

    Note: see MSDN Pack URIs in WPF:
    pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml

    0 讨论(0)
  • 2020-11-22 11:43
    1. Visual Studio 2010 Professional SP1.
    2. .NET Framework 4 Client Profile.
    3. PNG image added as resource on project properties.
    4. New file in Resources folder automatically created.
    5. Build action set to resource.

    This worked for me:

    <BitmapImage x:Key="MyImageSource" UriSource="Resources/Image.png" />
    
    0 讨论(0)
  • 2020-11-22 11:44

    The following worked and the images to be set is resources in properties:

        var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(MyProject.Properties.Resources.myImage.GetHbitmap(),
                                          IntPtr.Zero,
                                          Int32Rect.Empty,
                                          BitmapSizeOptions.FromEmptyOptions());
        MyButton.Background = new ImageBrush(bitmapSource);
    img_username.Source = bitmapSource;
    
    0 讨论(0)
  • 2020-11-22 11:46

    Full description how to use resources: WPF Application Resource, Content, and Data Files

    And how to reference them, read "Pack URIs in WPF".

    In short, there is even means to reference resources from referenced/referencing assemblies.

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