Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 设置源后未渲染WPF图像_C#_Wpf_Image_Resources - Fatal编程技术网

C# 设置源后未渲染WPF图像

C# 设置源后未渲染WPF图像,c#,wpf,image,resources,C#,Wpf,Image,Resources,我试图设置映像源,以最终从DLL引用中提取公司标准映像 为了进行测试并确保Uri的语法正确,将映像本地加载到测试项目中,并将源代码硬编码到XAML中 <Image Name="imgTest" Source="pack://application:,,,/test2;component/Dictionaries/bricks.png"/> 接下来,在代码中设置图像的源代码 BitmapImage imageUri = new BitmapImage(); imageUri.Begi

我试图设置映像源,以最终从DLL引用中提取公司标准映像

为了进行测试并确保Uri的语法正确,将映像本地加载到测试项目中,并将源代码硬编码到XAML中

<Image Name="imgTest" Source="pack://application:,,,/test2;component/Dictionaries/bricks.png"/>
接下来,在代码中设置图像的源代码

BitmapImage imageUri = new BitmapImage();
imageUri.BeginInit();           
var imageSource = new Uri(@"pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Dictionaries/bricks.png", UriKind.Absolute);
imageUri.UriSource = imageSource;           
imageUri.EndInit();
imgCopy.Source = imageUri;
在调试模式下查看imgTest.Source的软编码值并读取:

imgTest.Source = "pack://application:,,,/test2;component/Dictionaries/bricks.png"
imgTest.Source = "pack://application:,,,/test2;component/Dictionaries/bricks.png"
硬编码值和软编码值都相同,但是图像不会使用软编码配置进行渲染

已尝试使用嵌入的资源、内容和资源以及“复制到输出目录”属性可用的三个选项中的每一个来更新映像的“生成操作”属性

在这个问题上的任何智慧都是非常感谢的

编辑#1

我将Source属性复制到软编码图像的并排比较不会显示任何图像,而硬编码图像显示相同的源值。显示XAML和C#代码

<Image Name="imgCopy_Soft" />
<Image Name="imgCopy_Hard" Source="Dictionaries/bricks.png" />

imgCopy_Soft.Source = imgCopy_Hard.Source;

首先,需要在Visual Studio中将DLL添加为
引用

我在同一个解决方案中创建了一个库项目,并将其命名为_ImageLibrary。然后,我选中了该框,以便将其添加到我的项目中,如下所示:

在DLL中,我将图像设置为:

我有一张名为
awesome cat.jpg
的图片。它位于名为
Resources
的项目目录中

然后我可以像这样访问它:

记住!!!上面的XAML部分显示的是程序集名称,而不是项目名称!!!!我的程序集是ImageLibrary.dll

现在,如果DLL是一个松散文件,只需将其添加到项目中,然后使用
Browse
功能在
References
中引用它


应该注意的是,只有当DLL随exe一起提供时,这才有效。如果你想嵌入DLL,那么这是一个完全不同的游戏,如果你问一个单独的问题,我可以告诉你怎么做。

你是将DLL与exe一起发送,还是试图将DLL嵌入exe?什么类型的DLL保存图像?它是C#资源库还是C#用户控制库?确保
bricks.png
是VS项目中项目文件夹
dictionary
中的文件,该VS项目生成了名为
test2
的程序集。文件的生成操作必须是
Resource
Copy to Output Directory
设置与此无关,因为生成操作
Resource
会生成一个“程序集资源”,即程序集文件中包含的文件。如果
test2
是一个程序集,而不是执行代码的程序集,也请确保它被引用。DLL最终将嵌入.exe中。此时的测试不依赖于DLL引用。png图像已直接加载到测试项目中。我的第一步是尝试找出相同的Uri源定义行为不相同的原因。硬代码版本显示图像,而软代码版本不显示图像。如果我能解决这个问题,基于已经完成的研究,嵌入DLL的工作应该不会带来任何额外的麻烦
<UserControl x:Class="test2.ucConfigurator"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:test2"
         mc:Ignorable="d" Height="439.5" Width="400">

            <Grid >
                <Grid.RowDefinitions>
                    <RowDefinition Height="32"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid Grid.Row="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="32"/>
                        <ColumnDefinition Width="32"/>
                    </Grid.ColumnDefinitions>
                    <Image Name="imgCopy_Soft" Grid.Column="0" />
                    <Image Name="imgCopy_Hard" Grid.Column="1" Source="pack://application:,,,/test2;component/Dictionaries/bricks.png" />
                </Grid>
            </Grid>
</UserControl>
namespace test2
{
public partial class ucConfigurator : UserControl
{
    public ucConfigurator()
    {
        InitializeComponent();
    }

    public void Initialize()
    {
        BitmapImage imageUri = new BitmapImage();
        imageUri.BeginInit();
        var imageSource = new Uri(@"pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Dictionaries/bricks.png", UriKind.Absolute);
        imageUri.UriSource = imageSource;

        imageUri.EndInit();

        imgCopy_Soft.Source = imageUri;
        imgCopy_Soft.Source = imgCopy_Hard.Source;
    }
}
}