从.NET中的本机Win32资源获取PNG图像

Elm*_*lmo 4 .net c# vb.net resources image

DLL文件包含PNG资源类型中的一些图像.

我可以在Resource Hacker,Anolis Resourcer和Resource Tuner等软件中查看PNG图像.查看Anolis Resourcer的屏幕截图以获取更多详细信息:

http://i51.tinypic.com/9pt93c.jpg

有人能告诉我如何获得PNG图像没有.从DLL文件5220并将其放在PictureBox中?我不认为像LoadImage或LoadBitmap这样的API会起作用.

Dar*_*rov 5

// get the assembly containing the image
var assembly = Assembly.GetExecutingAssembly();

// set the picturebox image to read the embedded resource
pictureBox1.Image = Image.FromStream(
    assembly.GetManifestResourceStream("AssemblyName.test.png")
);
Run Code Online (Sandbox Code Playgroud)

其中AssemblyName.test.png是程序集内嵌入资源的完全限定名称.


更新:

您似乎正在尝试从本机程序集中提取资源.您可以查看以下文章,该文章说明了如何使用P/Invoke完成此操作.


she*_*ape 5

Darin 发布的链接(因此被标记为答案)不包含功能代码。我已经评估了那里发布的代码 (http://khason.net/blog/how-to-load-unmanaged-native-resources-from-managed-c-code/) 并发现它不适用于任何位图图像作为位图资源嵌入到任何 win32 dll 中。

此外,Hans Passant 省略了无数步骤,有效地使他的职位变得毫无用处。

我能找到的唯一有点接近的解决方案来自 2004 年为 XP Theme dll 垃圾写的文章。您可以在 ThemeManager.cs 中找到“GetResourcePNG”方法http://www.codeproject.com/KB/miscctrl/XPTaskBar.aspx

但是,应该注意的是,我在使用此方法时遇到了很多困难,因为调用 bitmap.RotateFlip(RotateFlipType.Rotate180FlipX); 尝试访问系统上 authui.dll 中的 png 时导致内存问题

更新:

我发现这里列出的代码 (http://www.vbaccelerator.com/home/NET/Code/Controls/Explorer_Bar/ExplorerBar_Control_Source_Code.asp) 是迄今为止功能最强大、产生最少错误并产生最快结果的代码. 代码是用 c# 编写的,即使域名会另有说明。使用这两个类;ImageUtility 和 ResourceLibrary,您可以轻松地从标准的非 .net 资源库/dll 中提取 PNG:

    public static Bitmap GetStandardResourceBitmap(String dllName, String resourceId) {
        Bitmap result = null;

        using (ResourceLibrary library = new ResourceLibrary() { Filename = dllName }) {
            IntPtr hDib = library.GetResource(resourceId, ResourceLibrary.ImageType.IMAGE_BITMAP, ResourceLibrary.ImageLoadOptions.LR_CREATEDIBSECTION);
            if (!hDib.Equals(IntPtr.Zero)) {
                result = ImageUtility.DibToBitmap(hDib);
                ImageUtility.DeleteObject(hDib);
            }
        }

        return result;
    }
Run Code Online (Sandbox Code Playgroud)

我选择在我的方法中使用一个 String 的 resourceId,只是因为它不需要重载,并且使用编号的资源 Id 就像在前面加上一个“#”一样简单。

GetStandardResourceBitmap("shell32.dll", "#632");
Run Code Online (Sandbox Code Playgroud)

干杯