我试图在代码中设置WPF图像的源代码。图像作为资源嵌入到项目中。通过查看示例,我提出了下面的代码。由于某种原因,它不工作-图像不显示。

通过调试,我可以看到流包含图像数据。怎么了?

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;

图标的定义如下:<Image x:Name="_icon" Width="16" Height="16" />


当前回答

在遇到与您相同的问题并进行了一些阅读之后,我发现了解决方案—打包uri。

我在代码中做了以下事情:

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;

或者更短,通过使用另一个BitmapImage构造函数:

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

URI被分成几个部分:

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.

application:后面的三个斜杠必须用逗号代替:

注意:包URI的授权组件 是一个嵌入式URI,指向 包装,必须符合RFC 2396。 此外,“/”字符必须 替换为“,”字符, 以及保留字符,如“%” "?"必须转义。查看OPC 获取详细信息。

当然,还要确保将映像上的构建操作设置为Resource。

其他回答

如何从嵌入的资源图标和图像(校正版本的Arcturus)加载图像:

假设您想添加一个带有图像的按钮。你该怎么办?

Add to project folder icons and put image ClickMe.png here In properties of 'ClickMe.png', set 'BuildAction' to 'Resource' Suppose your compiled assembly name is 'Company.ProductAssembly.dll'. Now it's time to load our image in XAML <Button Width="200" Height="70"> <Button.Content> <StackPanel> <Image Width="20" Height="20"> <Image.Source> <BitmapImage UriSource="/Company.ProductAssembly;component/Icons/ClickMe.png"></BitmapImage> </Image.Source> </Image> <TextBlock HorizontalAlignment="Center">Click me!</TextBlock> </StackPanel> </Button.Content> </Button>

完成了。

你只是漏了一点。

要从任何程序集获取嵌入式资源,你必须像我在这里提到的那样,用你的文件名提到程序集名称:

Assembly asm = Assembly.GetExecutingAssembly();
Stream iconStream = asm.GetManifestResourceStream(asm.GetName().Name + "." + "Desert.jpg");
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = iconStream;
bitmap.EndInit();
image1.Source = bitmap;

在遇到与您相同的问题并进行了一些阅读之后,我发现了解决方案—打包uri。

我在代码中做了以下事情:

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;

或者更短,通过使用另一个BitmapImage构造函数:

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

URI被分成几个部分:

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.

application:后面的三个斜杠必须用逗号代替:

注意:包URI的授权组件 是一个嵌入式URI,指向 包装,必须符合RFC 2396。 此外,“/”字符必须 替换为“,”字符, 以及保留字符,如“%” "?"必须转义。查看OPC 获取详细信息。

当然,还要确保将映像上的构建操作设置为Resource。

这是我的方式:

internal static class ResourceAccessor
{
    public static Uri Get(string resourcePath)
    {
        var uri = string.Format(
            "pack://application:,,,/{0};component/{1}"
            , Assembly.GetExecutingAssembly().GetName().Name
            , resourcePath
        );

        return new Uri(uri);
    }
}

用法:

new BitmapImage(ResourceAccessor.Get("Images/1.png"))

还有一种更简单的方法。如果图像在XAML中作为资源加载,并且所讨论的代码是该XAML的幕后代码:

下面是一个XAML文件的资源字典——你唯一关心的一行是ImageBrush,键为“PosterBrush”——其余的代码只是用来显示上下文

<UserControl.Resources>
        <ResourceDictionary>
            <ImageBrush x:Key="PosterBrush" ImageSource="..\Resources\Images\EmptyPoster.jpg" Stretch="UniformToFill"/>

        </ResourceDictionary>
    </UserControl.Resources>

在后面的代码中,你可以这样做

ImageBrush posterBrush = (ImageBrush)Resources["PosterBrush"];