Get PNG image from Win32 native resource in .NET.

The DLL file contains images inside the PNG resource type.

I can view PNG images in programs like Resource Hacker, Anolis Resourcer & Resource Tuner. Check out this screenshot of Anolis Resourcer for more details:

Can someone tell me how can I get a PNG image of no. 5220 from a dll file and put it inside a picturebox? I don't think APIs like LoadImage or LoadBitmap will work.

+4
source share
3 answers
// 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") ); 

where AssemblyName.test.png is the fully qualified name of the embedded resource within the assembly.


UPDATE:

It seems you are trying to extract resources from a native assembly. You can take a look at the following article , which illustrates how this can be done with P / Invoke.

+5
source

A PNG image is not one of the standard Win32 resource types. It is usually embedded as a binary binary with the named resource type "PNG", although this is not guaranteed. Of course, the easiest way to understand this is to open the file with the Visual Studio File + Open + File command. You will see the built-in resources organized in the form of a tree, hopefully with a descriptive name, right-click the candidate and select "Export" to save it to disk.

Doing this programmatically requires a lot of little things. This is complicated because the resource type and resource identifier can be either a string or IntPtr, so you will need 4 overloads for FindResource. In order to load a file without executing any of its code, you will need LoadLibraryEx (). FindResource to get the resource descriptor. SizeOfResource to find out how big it is. LoadResource + LockResource to get a pointer to resource data. Marshal.Copy () to copy resource data to byte []. Clean up using FreeResource and FreeLibrary.

+4
source

The link that Darin posted (which, therefore, was marked as an answer) does not contain functional code. I appreciated the code posted there (http://khason.net/blog/how-to-load-unmanaged-native-resources-from-managed-c-code/) and found that it does not work properly for any A bitmap embedded in any win32 dll as a bitmap resource.

In addition, Hans Passant leaves many steps, actually making it useless.

The only somewhat close solution I could find was an article written in 2004 for the XP Theme dll junk file. You can find the GetResourcePNG method in ThemeManager.cs here http://www.codeproject.com/KB/miscctrl/XPTaskBar.aspx

However, it should be noted that I had a lot of difficulties with this method, like calling bitmap.RotateFlip (RotateFlipType.Rotate180FlipX); causes memory problems when trying to access png in authui.dll file on my system

Update:

I found the code given here (http://www.vbaccelerator.com/home/NET/Code/Controls/Explorer_Bar/ExplorerBar_Control_Source_Code.asp) to be the most functional, produce the smallest errors and produce the fastest results. The code is written in C #, although the domain name points to something else. The use of two classes; ImageUtility and ResourceLibrary, you can easily pull PNG from the standard resource library DLL:

  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; } 

I decided to have resourceId in my String method, just because it does not require overloading and using numbered resource identifiers is as simple as adding "#".

 GetStandardResourceBitmap("shell32.dll", "#632"); 

Greetings

+4
source

All Articles