How can I get a BitmapImage from a Resource?

前端 未结 1 856
鱼传尺愫
鱼传尺愫 2020-12-08 19:32

My assembly includes an image with BuildAction==Resource. I want to obtain a BitmapImage from this embedded resource.

I can load a BitmapImage from file like this:<

相关标签:
1条回答
  • 2020-12-08 20:22

    This MSDN page has all the information you might want to know about resource URIs in WPF (often called pack URIs). You're going to want to use relative URIs more often probably, so see Table 4, which should be of particular use.

    If you want a briefer overview of resource (pack) URIs, see this blog post. It shows that syntax is indeed relatively simple:

    pack://application:,,,/ResourceFile.xaml
    
    pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml
    

    However, there are a few quirks to work out (in my experience), so often finding the correct resource URI requires a bit of experimentation.

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