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

本文关键字:png 工作 引用 代码 获取 WPF | 更新日期: 2023-09-27 18:37:27

我读过很多例子。 而我所拥有的似乎是对的。 但是在加载时它会失败。

这是我的代码:

LargeImage=new BitmapImage(new Uri("pack://application:,,,/Images/books_48.png"))

此代码在程序集 A 中运行。 AssemblyA 的项目也有一个名为 Images 的文件夹。 该文件夹包含一个名为 books_48.png 的文件。 它被设置为编译为"资源"并且从不复制。

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

LargeImage的第一个引用是在AssemblyB中。 它将 LargeImage 绑定到 FluentRibbon Fluent:Button.LargeIcon。

当需要加载位图图像时,我收到此错误:

找不到资源"图像/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"类型的错误)。

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

我使用我的 ImageUtilities 类来获取 C# 中的图像。 我将附加下面的代码。

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

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

属性.资源.图像名称;

这给你一个位图。

我使用我的 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];
        }
    }
}

请参阅 Nuno Rodriguez 对 WPF 图像资源的回答。 它解释说路径应该是:

"/«Assembly A»;component/«YourPath»/«YourImage.png»"