【问题标题】:Cannot get reference to png work in WPF C# code无法在 WPF C# 代码中获取对 png 工作的引用
【发布时间】:2012-06-15 20:39:24
【问题描述】:

我读过很多例子。我所拥有的似乎是正确的。但是加载失败。

这是我的代码:

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

此代码在 AssemblyA 中运行。 AssemblyA 的项目中还有一个名为 Images 的文件夹。该文件夹有一个名为 books_48.png 的文件。它被设置为编译为“资源”并且从不复制。

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

对LargeImage 的第一个引用在AssemblyB 中。它将 LargeImage 绑定到 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”之类的错误)。

【问题讨论】:

  • 不应该是嵌入资源吗?
  • @PaulSullivan - 在这个答案的 cmets (stackoverflow.com/a/483891/16241) 中,人们似乎在说嵌入式资源在 99% 的情况下都是错误的。关于在应用中包含两次图像的一些事情。
  • 是的,据我所知,它应该只是资源。 Vaccano - 程序集 B 是否引用程序集 A?还是它们都加载到另一个可执行文件中......或者?

标签: c# .net wpf uri


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    请参阅 Nuno Rodriguez 对WPF image resources 的回答。它解释了路径应该是:

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

    【讨论】:

    • 我试过了,但由于我是在 C# 中(不是在 XAML 中)执行此操作,因此这种路径会返回“无效 URI”错误 (UriFormatException)。
    猜你喜欢
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    • 2021-10-18
    • 1970-01-01
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多