【问题标题】:How to: Use images in a project, if they come from an embedded assembly如何:在项目中使用图像(如果它们来自嵌入式程序集)
【发布时间】:2014-05-28 09:26:05
【问题描述】:

如何:在项目中使用来自嵌入式程序集的图像

我创建了一个具有使用图像的公共方法的程序集。这些图像是嵌入的。现在,当我尝试在我的新项目中使用该程序集时,除了图像之外,一切正常。

我尝试在不使用程序集的情况下运行该应用程序(只是一个普通的 WPF 应用程序)并且它工作正常。看来,程序集没有找到嵌入的图像。

你能帮帮我吗?

编辑:我在程序集中创建图像的方式。

public void CreateBitmapImage(string uri)
    {
        BitmapImage bitmapImage = new BitmapImage();
        bitmapImage.BeginInit();
        bitmapImage.UriSource = new Uri(uri, UriKind.Absolute);
        bitmapImage.EndInit();
        image_msgIcon.Source = bitmapImage;
    }

    public void SetMsgImage(string msgImage)
    {
        if (msgImage == Error)
        {
            CreateBitmapImage("pack://application:,,,/CL.New;component/Images/CustomMessageBox/cross-circle.png");
        }
        else if (msgImage == Exclamation)
        {
            CreateBitmapImage("pack://application:,,,/CL.New;component/Images/CustomMessageBox/exclamation.png");
        }
        else if (msgImage == Information)
        {
            CreateBitmapImage("pack://application:,,,/CL.New;component/Images/CustomMessageBox/information.png");
        }
        else if (msgImage == Question)
        {
            CreateBitmapImage("pack://application:,,,/CL.New;component/Images/CustomMessageBox/question.png");
        }
        else
        {
        }
    }

现在我收到以下错误消息:

Die Ressource "images/custommessagebox/cross-circle.png" kann nicht gefunden werden.
The resource "images/custommessagebox/cross-circle.png" cannot be found.

我试过这样:

CreateBitmapImage("/CL.New;component/Images/CustomMessageBox/cross-circle.png");

...

bitmapImage.UriSource = new Uri(uri, UriKind.Relative);

...这似乎有效,但图像未显示(没有错误消息)。

【问题讨论】:

    标签: c# .net wpf uri .net-assembly


    【解决方案1】:

    您应该阅读Pack URIs in WPF 文档。

    基本上,当您引用来自不同程序集的资源时,您应该使用不同的Uris。正如文档中所述,请尝试使用以下内容:

    pack://application:,,,/ReferencedAssembly;component/Image.png 
    

    pack://application:,,,/ReferencedAssembly;component/Subfolder/Image.png
    

    【讨论】:

    • 好的,谢谢。好吧,我读了这篇文章,但我不确定“ReferencedAssembly”是什么意思——它是我的 dll 的名称,还是命名空间的名称?使用 dll 的名称,我收到资源不可用的错误。
    • 如果您为名为 SomeApplication 的 WPF 应用程序构建程序集,那么您将获得一个名为 SomeApplication.exe 的程序集。这里,ReferencedAssembly 表示应用程序的名称,而不是包含 exe 文件扩展名的程序集文件的完整名称。
    • @Exception 要添加到 Sheridan,您希望将 ReferencedAssembly 替换为正在创建资源程序集的项目的 Assembly name。您可以找到Assembly name,转到项目属性,您将在应用程序选项卡下看到它。
    • 谢谢,Sheridan 和 NETscape。好像没有exe文件。只有dll文件。我的Class Library 被称为“CL.New”,所以我的调试文件夹中唯一的文件是“CL.New.dll”。请查看我编辑的代码。
    • @Exception 你应该省略.dll 扩展:pack://application:,,,/CL.New;component/Images/img_icon.png
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多