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:
Post a Comment