C# 无法在WPF C代码中获取对png工作的引用

C# 无法在WPF C代码中获取对png工作的引用,c#,.net,wpf,uri,C#,.net,Wpf,Uri,我读过很多例子。我所做的似乎是对的。但当涉及到加载时,它失败了 这是我的密码: LargeImage=new BitmapImage(new Uri("pack://application:,,,/Images/books_48.png")) 此代码在AssemblyA中运行。AssemblyA的项目中还有一个名为Images的文件夹。该文件夹有一个名为books_48.png的文件。它被设置为作为资源编译,从不复制 我使用DotPeek查看图像是否在AssemblyA.dll中,它是否在那里

我读过很多例子。我所做的似乎是对的。但当涉及到加载时,它失败了

这是我的密码:

LargeImage=new BitmapImage(new Uri("pack://application:,,,/Images/books_48.png"))
此代码在AssemblyA中运行。AssemblyA的项目中还有一个名为Images的文件夹。该文件夹有一个名为books_48.png的文件。它被设置为作为资源编译,从不复制

我使用DotPeek查看图像是否在AssemblyA.dll中,它是否在那里

对LargeImage的第一个引用位于AssemblyB中。它将大图像绑定到FluentRibbon Fluent:Button.LargeIcon

加载BitmapImage时,出现以下错误:

找不到资源“images/books_48.png”

有没有关于如何加载的想法

注意:我还尝试了以下方法:

"pack://application:,,,/AssemblyA;component/Images/books_48.png"
"pack://application:,,,/AssemblyA;Images/books_48.png"
"pack://application:,,,/AssemblyA;/Images/books_48.png"
"pack://application:,,,/Images/books_48.png"
"pack://application:,,,Images/books_48.png"
"Images/books_48.png"
"/Images/books_48.png"

它们都会给我错误,要么找不到,要么是无效URI之类的错误。

参见Nuno Rodriguez的答案。它解释了路径应该是:


/«组件A»;component/«YourPath»/«yourmage.png»

我使用ImageUtilities类获取C语言中的图像。我将在下面附上代码

我总是将图像设置为“资源”“不复制”。 然后我总是将所有图像复制到我的Properties\Resources.resx文件中。这可以通过在visual studio中展开项目文件夹中的第一项“属性”文件夹来完成。然后双击“Resources.resx”文件,设计器将弹出。在此窗口的左上角,单击下拉列表并选择“图像”。然后从解决方案资源管理器高亮显示项目中的所有图像文件,并将它们复制/粘贴到Resources.resx窗口中

这样,在代码隐藏中,我可以通过键入以下内容来访问它们:

Properties.Resources.ImageName

这会给你一个位图


我使用ImageUtilities类将其转换为ImageSource,以便与WPF一起使用。因此,如果我有一个名为“MyPicture.png”的图像,我会对ImageUtilities类进行以下调用:

ImageSource source = ImageUtilities.ImageSourceFromResourceKey(
                                  Properties.Resources.MyPicture, "MyPicture");
我传递文本“MyPicture”的原因是,我可以缓存图像,而不必为每个图像创建一次以上的图像

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Helpers
{
    public static class ImageUtilities
    {
        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        public static extern bool DeleteObject(IntPtr hObject);
        public static Dictionary<String, ImageSource> _cachedImages = new Dictionary<string, ImageSource>();

        public static ImageSource ImageSourceFromBitmap(Bitmap bitmap)
        {
            if (bitmap == null)
            {
                return ImageSourceFromResourceKey(Properties.Resources.Exit, "Exit");
            }

            try
            {
                IntPtr hBitmap = bitmap.GetHbitmap();
                BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    hBitmap,
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());

                DeleteObject(hBitmap);
                return bitmapSource;
            }
            catch (Exception ex)
            {
                //Error loading image.. Just log it...
                Helpers.Debug(ex.ToString());
            }

            return null;
        }

        public static ImageSource ImageSourceFromResourceKey(Bitmap bitmap, string imageKey)
        {
            if (!_cachedImages.ContainsKey(imageKey))
            {
                _cachedImages[imageKey] = ImageSourceFromBitmap(bitmap);
            }

            return _cachedImages[imageKey];
        }
    }
}

“难道不是嵌入式资源吗?”@PaulSullivan-在对这个答案的评论中,人们似乎说嵌入式资源99%的时候是错误的。关于在应用程序中包含两次图像的内容。是的,据我所知,它应该只是一个资源。瓦卡诺-组件B是否参考组件A?或者它们都加载到另一个可执行文件中。。。或者?我试过了,但因为我是用C语言而不是用XAML,所以这种路径会返回一个无效的URI错误UriFormatException。