Setting WPF image source in code

I'm trying to set a WPF image's source in code. The image is embedded as a resource in the project. By looking at examples I've come up with the below code. For some reason it doesn't work - the image does not show up.

By debugging I can see that the stream contains the image data. So what's wrong?

Assembly asm = Assembly.GetExecutingAssembly();
Stream iconStream = asm.GetManifestResourceStream("SomeImage.png");
PngBitmapDecoder iconDecoder = new PngBitmapDecoder(iconStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
ImageSource iconSource = iconDecoder.Frames[0];
_icon.Source = iconSource;

The icon is defined something like this: <Image x:Name="_icon" Width="16" Height="16" />


After having the same problem as you and doing some reading, I discovered the solution - Pack URIs.

I did the following in code:

Image finalImage = new Image();
finalImage.Width = 80;
...
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri("pack://application:,,,/AssemblyName;component/Resources/logo.png");
logo.EndInit();
...
finalImage.Source = logo;

Or shorter, by using another BitmapImage constructor:

finalImage.Source = new BitmapImage(
    new Uri("pack://application:,,,/AssemblyName;component/Resources/logo.png"));

The URI is broken out into parts:

  • Authority: application:///
  • Path: The name of a resource file that is compiled into a referenced assembly. The path must conform to the following format: AssemblyShortName[;Version][;PublicKey];component/Path

  • AssemblyShortName: the short name for the referenced assembly.
  • ;Version [optional]: the version of the referenced assembly that contains the resource file. This is used when two or more referenced assemblies with the same short name are loaded.
  • ;PublicKey [optional]: the public key that was used to sign the referenced assembly. This is used when two or more referenced assemblies with the same short name are loaded.
  • ;component: specifies that the assembly being referred to is referenced from the local assembly.
  • /Path: the name of the resource file, including its path, relative to the root of the referenced assembly's project folder.
  • The three slashes after application: have to be replaced with commas:

    Note: The authority component of a pack URI is an embedded URI that points to a package and must conform to RFC 2396. Additionally, the "/" character must be replaced with the "," character, and reserved characters such as "%" and "?" must be escaped. See the OPC for details.

    And of course, make sure you set the build action on your image to Resource .


    var uriSource = new Uri(@"/WpfApplication1;component/Images/Untitled.png", UriKind.Relative);
    foo.Source = new BitmapImage(uriSource);
    

    这会在名为“WpfApplication1”的程序集中将其“Build Action”设置为“Resource”的名为“Images”的文件夹中加载名为“Untitled.png”的图像。


    这是少一点的代码,可以在一行中完成。

    string packUri = "pack://application:,,,/AssemblyName;component/Images/icon.png";
    _image.Source = new ImageSourceConverter().ConvertFromString(packUri) as ImageSource;
    
    链接地址: http://www.djcxy.com/p/50428.html

    上一篇: 使用非WPF XAML资源

    下一篇: 在代码中设置WPF图像源