Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smoothdeveloper/f16daf0e0c661d0ec881 to your computer and use it in GitHub Desktop.
Save smoothdeveloper/f16daf0e0c661d0ec881 to your computer and use it in GitHub Desktop.
Extract all bitmap resources from all assemblies in folder
// annoyed with resources embedded in resources in assemblies?
// extract all images from all assemblies in a folder
// see call commented at the end
#r "System.Drawing"
#r "System.Windows.Forms"
open System.Collections
open System.Drawing
open System.Linq
open System.IO
open System.Resources
open System.Reflection
open System.Drawing.Imaging
open System.Windows.Forms
let getFilenameExtension (format: ImageFormat) =
let format =
let format = ImageCodecInfo.GetImageEncoders().FirstOrDefault(fun e -> e.FormatID = format.Guid)
if format = null then
ImageCodecInfo.GetImageEncoders().FirstOrDefault(fun e -> e.FilenameExtension.ToLower().Contains("png"))
else
format
(format.FilenameExtension.Split(';').[0]).ToLower().Replace("*","")
let extractResourcesImages rootFolder (a:Assembly) =
let directory = Path.Combine(rootFolder, "extractedimages", a.GetName().Name)
ignore <| Directory.CreateDirectory(directory)
let resourceNames = a.GetManifestResourceNames()
for n in resourceNames do
printfn "resource name: %s" n
match n with
| x when x.EndsWith("resources") ->
use stream = a.GetManifestResourceStream(n)
use rm = new ResourceReader(stream)
for entry in rm.Cast<DictionaryEntry>() do
let makeFileName suffix (image: Image) =
Path.Combine(directory, n, entry.Key.ToString()) + suffix + (getFilenameExtension (image.RawFormat))
let images = [
match entry.Value with
| :? Bitmap as bitmap ->
let filename = makeFileName "" bitmap
let image = new Bitmap(bitmap)
yield filename, image
| :? ImageListStreamer as ils ->
let imageList = new ImageList()
imageList.TransparentColor <- Color.Transparent
imageList.ImageStream <- ils
for image,ii in Seq.zip (imageList.Images.Cast<Bitmap>()) ([0..imageList.Images.Count]) do
let suffix = ("." + ii.ToString() + ".")
let filename = makeFileName suffix image
let image = new Bitmap(image)
yield filename, image
| _ -> ()
]
images |> Seq.iter(fun (filename, image) ->
try
let file = new FileInfo(filename)
file.Directory.Create()
image.Save(filename, image.RawFormat)
printfn "saved %s" filename
with
| e -> printfn "failed to save resource %s %s" filename (e.ToString())
)
printfn "%O %O" entry.Key (entry.Value.GetType().Name)
| _ -> ()
let loadAllAssemblies folder =
let folder = new DirectoryInfo(folder)
let files = seq {
yield! folder.GetFiles("*.exe")
yield! folder.GetFiles("*.dll")
}
let tryLoadAssembly f =
try
Some <| Assembly.LoadFile(f)
with
| e -> None
seq {
for f in files do
let a = tryLoadAssembly f.FullName
if a.IsSome then
yield a.Value
}
(*
loadAllAssemblies (Path.Combine(__SOURCE_DIRECTORY__ , "./"))
|> Seq.iter (extractResourcesImages __SOURCE_DIRECTORY__)
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment