Software Is Hardwork

ISimplicityAffinative: The endless pursuit of anti-complexity.
The technology-centric blog of D. P. Bullington.

Email D. P. Bullington View D. P. Bullington\ Follow D. P. Bullington on Twitter Get Software Is Hardwork code on CodePlex

Blog Post(s)

ResDump Code
Thursday, August 21, 2008

A long time ago, I wrote a tool which would dump all image and image list resources from an assembly, resx, or resources file:

/*
    Copyright ©2002-2008 D. P. Bullington
    Distributed under the MIT license: http://www.opensource.org/licenses/mit-license.php
*/

using System;
using System.Resources;
using System.IO;
using System.Collections;
using System.Reflection;
using System.Text;
using System.Globalization;
using System.Windows.Forms;

public class ResDump
{    
    public static void Main(String[] args)
    {
        ArrayList readers;
        string lower;
        
        Console.WriteLine(new string('-', 70));
        Console.WriteLine("Resource Dump Utillity (c)2005-2007 Daniel Bullington");
        Console.WriteLine();
        Console.WriteLine(args[0]);
        Console.WriteLine(new string('-', 70));
        Console.WriteLine();
        
        if(args.Length != 1)
        {
            Console.WriteLine("USAGE: ResDump.exe [filename]");
            return;
        }

        lower = args[0].ToLower();

        if(!lower.EndsWith(".dll") && !lower.EndsWith(".resx") && !lower.EndsWith(".resources") && !lower.EndsWith(".dll"))
        {
        {
            Console.WriteLine("File name must end with .resx, .resources, or .dll extensions:\n" + args[0]);
            return;
        }
        }

        

        try          
        {

            readers = GetResourceReaders(args[0]);

            if(readers != null)
            {
                foreach(IResourceReader rr in readers) 
                {            
                    DumpResourceReader(args[0], rr);
                    rr.Close();
                }
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine("*** ERROR ***");
            Console.WriteLine(ex.ToString());
        }
        
    }



    public static ArrayList GetResourceReaders(string name)
    {        
        Assembly a;
        CultureInfo ci;
        string lower;
        bool mainAssembly;
        IResourceReader rr;
        string[] resources;
        Stream s;
        ArrayList readers;

        readers = new ArrayList();
        lower = name.ToLower();
        
        if (lower.EndsWith(".resx"))
        {
            rr = new System.Resources.ResXResourceReader(name);
            readers.Add(rr);
        }
        else if (lower.EndsWith(".resources")) 
        {
            rr = new ResourceReader(name);        
            readers.Add(rr);
        }
        else if(lower.EndsWith(".dll"))
        {
            a = Assembly.LoadFrom(name);            
            ci = a.GetName().CultureInfo;
            mainAssembly = ci.Equals(CultureInfo.InvariantCulture);            
            resources = a.GetManifestResourceNames();
            
            foreach(String resName in resources) 
            {
                if (resName.EndsWith(".resources")) 
                {
                    s = a.GetManifestResourceStream(resName);
                    
                    rr = new ResourceReader(s);
                    readers.Add(rr);
                }                                
            }
        }


        return readers;
    }

    private static void DumpResourceReader(string outputDir, IResourceReader rr)
    {
        int count = 0;
        System.Drawing.Image image;
        System.Windows.Forms.ImageListStreamer ilStreamer;
        System.Windows.Forms.ImageList imageList;
            
        outputDir = outputDir + "-RESDUMP";

        if(!Directory.Exists(outputDir))
            Directory.CreateDirectory(outputDir);
        
        foreach (DictionaryEntry de in rr)         
        {
            if(de.Value != null)
            {            
                if(de.Value is System.Drawing.Image)
                {
                    Console.WriteLine("Dumping resource type " + de.Value.GetType().FullName + "...");
                    image = (System.Drawing.Image)de.Value;
                    image.Save(outputDir +"\\"+ (string)de.Key + ".bmp");
                    image.Dispose();
                    image = null;            
                    count++;
                }            
                else if(de.Value is System.Windows.Forms.ImageListStreamer)
                {
                    Console.WriteLine("Dumping resource type " + de.Value.GetType().FullName + "...");
                    ilStreamer = (System.Windows.Forms.ImageListStreamer)de.Value;
                    imageList = new System.Windows.Forms.ImageList();
                    imageList.ImageStream = ilStreamer;

                    foreach(System.Drawing.Image img in imageList.Images)
                    {                        
                        img.Save(outputDir +"\\"+ Guid.NewGuid().ToString() + ".bmp");                                                
                    }

                    imageList.Dispose();
                    imageList = null;
                    count++;
                }
            }
            
        }
        
        Console.WriteLine("Dumped {0} serialized resources.", count);
    }
}


0 comments:

Speaking Enagements

  • 11/18/2010 | Charlottesville .NET Users Group | Charlottesville, VA | Topic TBD
  • 09/14/2010 | Hampton Roads .NET Users Group | Cheaspeake, VA | Topic TBD
  • 07/01/2010 | Richmond .NET Users Group | Richmond, VA | Topic TBD
  • (past) 12/08/2009 | Hampton Roads .NET Users Group | Cheaspeake, VA | SharePoint Antithesis - A Case Study in Pragmatic Software Architecture and Engineering Processes
  • (past) 10/04/2009 | Richmond Code Camp 2009.2 | Richmond, VA | Soothing the Pain Points: Data Access, Validation, Rules, UI, Presentation, et. al
  • (past) 07/23/2009 | Charlottesville .NET Users Group | Charlottesville, VA | Debugging on the Windows Platform
  • (past) 05/23/2008 | NoVa CodeCamp 2009.01 | Reston, VA | Going Proxy-less - The WCF Proxy Factory
  • (past) 04/25/2009 | Richmond Code Camp 2009.1 | Richmond, VA | Software Programmer to Software Engineer: Concepts to Span the Divide
  • (past) 02/05/2009 | Richmond .NET Users Group | Richmond, VA | Debugging on the Windows Platform
  • (past) 10/04/2008 | Richmond Code Camp 2008.2 | Richmond, VA | Going Proxy-less - The WCF Proxy Factory

Blog Archive

Post Labels

.NET (64) .NETv4.0 (3) ACID (1) ActiveDirectory (1) ADF (2) Affiliate (1) Agile (6) AJAX (1) Allocator (3) Analysis (1) AOP (4) ASP.NET (6) ASP.NET MVC (1) Assembly (2) BadIdeaPile (1) BagOfBolts (5) Blogger (1) Books (2) BuildMgmt (8) C# (46) ChoDNUG (1) CLR (1) CLRv4.0 (2) CMP (1) CMS (2) CodeCamp (2) COM (1) Conversation (1) Coverage (1) CUI (1) Database (2) DDD (1) DeadFxs (1) Debugging (9) Design (4) DevAuto (3) DevCfg (1) Development (118) DI (6) DiffMerge (1) Domain (1) DTfW (2) EclipseIDE (1) ECM (1) EntityFramework (1) Estimating (1) FileShare (1) Frameworks (7) GAC (2) Google (1) Hardware (2) HRNUG (1) Humor (6) IIS (4) ILDASM (1) Impersonation (2) InstallError (1) IoC (6) KingTodd (1) LinkedIn (1) LINQtoSQL (2) MarketingHype (1) MBUnit (1) Mentoring (22) Metadata (1) Microsoft (7) MOSS2007 (5) MSBuild (2) MSIL (4) MSSCCI (2) NAnt (2) NCore (2) NCover (1) NDatabase (4) NetUse (1) NHibernate (2) NoVaCodeCamp (2) NTSD/CDB (2) NUnit (1) Observation (2) Office (2) OOD (7) OOP (6) OpenSource (14) Opinion (19) Personal (3) PMP (1) Polymorphism (1) PowerPoint (1) PowerShell (2) Presentation (3) Process (4) ProjectManagement (2) PublicKeyToken (1) QA (2) RDNUG (1) Reflection (2) Registry (2) Resharper (1) Reversing (2) RichmondCodeCamp (5) SCM (11) Scrum (5) Security (2) Series (3) Server2008 (4) ServicePack (1) SES (7) SharePoint (7) Silverlight (1) SoC (3) Software (49) SoftwareIsHardwork (17) Speaking (7) SQL (2) SSO (2) StrongName (2) Suite2008 (9) Suite2010 (1) SwEng (19) TechBlunder (1) Testing (14) Thread (3) Tools (8) Troubleshooting (10) Twitter (3) Types (2) UAC (1) UIP (1) Vault (2) VB6 (1) VC (1) Vista (3) VisualStudio (15) VSIP (2) VSTS (1) WCF (4) Web (4) WebForms (1) Win32 (3) WinDBG (3) WindowsIdentity (3) WinForms (1) WIT (1) Workhorse (1) WoW64 (1) WPF (1) WSS3 (2) x64 (2) x86 (2) xUnit (1)

Disclaimer

© D. P. Bullington, all rights reserved. Everything posted on this blog is my personal opinion and does not represent the views of my employer nor serves to infringe on the sovereignty of any nation.