2 people like it.

Image extraction from assemblies

Extract images from all resources's ressources of all assmeblies in a folder

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
// 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"
open System.Collections
open System.Drawing
open System.Linq
open System.IO
open System.Resources
open System.Reflection
open System.Drawing.Imaging
 
let getFilenameExtension (format: ImageFormat) =
  let extensions = ImageCodecInfo.GetImageEncoders().FirstOrDefault(fun e -> e.FormatID = format.Guid).FilenameExtension
  (extensions.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
        match entry.Value with
        | :? Bitmap -> 
          let bitmap = (entry.Value :?> Bitmap)
          
          let filename = Path.Combine(directory, n, entry.Key.ToString()) + (getFilenameExtension (bitmap.RawFormat))
          let file = new FileInfo(filename)
          file.Directory.Create()
          let image = new Bitmap(bitmap)
          try
            image.Save(filename, bitmap.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__ , "../../../bin/debug/"))
|> Seq.iter (extractResourcesImages __SOURCE_DIRECTORY__)
*)
namespace System
namespace System.Collections
namespace System.Drawing
namespace System.Linq
namespace System.IO
namespace System.Resources
namespace System.Reflection
namespace System.Drawing.Imaging
val getFilenameExtension : format:ImageFormat -> string

Full name: Script.getFilenameExtension
val format : ImageFormat
Multiple items
type ImageFormat =
  new : guid:Guid -> ImageFormat
  member Equals : o:obj -> bool
  member GetHashCode : unit -> int
  member Guid : Guid
  member ToString : unit -> string
  static member Bmp : ImageFormat
  static member Emf : ImageFormat
  static member Exif : ImageFormat
  static member Gif : ImageFormat
  static member Icon : ImageFormat
  ...

Full name: System.Drawing.Imaging.ImageFormat

--------------------
ImageFormat(guid: System.Guid) : unit
val extensions : string
type ImageCodecInfo =
  member Clsid : Guid with get, set
  member CodecName : string with get, set
  member DllName : string with get, set
  member FilenameExtension : string with get, set
  member Flags : ImageCodecFlags with get, set
  member FormatDescription : string with get, set
  member FormatID : Guid with get, set
  member MimeType : string with get, set
  member SignatureMasks : byte[][] with get, set
  member SignaturePatterns : byte[][] with get, set
  ...

Full name: System.Drawing.Imaging.ImageCodecInfo
ImageCodecInfo.GetImageEncoders() : ImageCodecInfo []
val e : ImageCodecInfo
property ImageCodecInfo.FormatID: System.Guid
property ImageFormat.Guid: System.Guid
System.String.Split([<System.ParamArray>] separator: char []) : string []
System.String.Split(separator: string [], options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], count: int) : string []
System.String.Split(separator: string [], count: int, options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], count: int, options: System.StringSplitOptions) : string []
val extractResourcesImages : rootFolder:string -> a:Assembly -> unit

Full name: Script.extractResourcesImages
val rootFolder : string
val a : Assembly
type Assembly =
  member CodeBase : string
  member CreateInstance : typeName:string -> obj + 2 overloads
  member EntryPoint : MethodInfo
  member Equals : o:obj -> bool
  member EscapedCodeBase : string
  member Evidence : Evidence
  member FullName : string
  member GetCustomAttributes : inherit:bool -> obj[] + 1 overload
  member GetCustomAttributesData : unit -> IList<CustomAttributeData>
  member GetExportedTypes : unit -> Type[]
  ...

Full name: System.Reflection.Assembly
val directory : string
type Path =
  static val DirectorySeparatorChar : char
  static val AltDirectorySeparatorChar : char
  static val VolumeSeparatorChar : char
  static val InvalidPathChars : char[]
  static val PathSeparator : char
  static member ChangeExtension : path:string * extension:string -> string
  static member Combine : [<ParamArray>] paths:string[] -> string + 3 overloads
  static member GetDirectoryName : path:string -> string
  static member GetExtension : path:string -> string
  static member GetFileName : path:string -> string
  ...

Full name: System.IO.Path
Path.Combine([<System.ParamArray>] paths: string []) : string
Path.Combine(path1: string, path2: string) : string
Path.Combine(path1: string, path2: string, path3: string) : string
Path.Combine(path1: string, path2: string, path3: string, path4: string) : string
Assembly.GetName() : AssemblyName
Assembly.GetName(copiedName: bool) : AssemblyName
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
type Directory =
  static member CreateDirectory : path:string -> DirectoryInfo + 1 overload
  static member Delete : path:string -> unit + 1 overload
  static member EnumerateDirectories : path:string -> IEnumerable<string> + 2 overloads
  static member EnumerateFileSystemEntries : path:string -> IEnumerable<string> + 2 overloads
  static member EnumerateFiles : path:string -> IEnumerable<string> + 2 overloads
  static member Exists : path:string -> bool
  static member GetAccessControl : path:string -> DirectorySecurity + 1 overload
  static member GetCreationTime : path:string -> DateTime
  static member GetCreationTimeUtc : path:string -> DateTime
  static member GetCurrentDirectory : unit -> string
  ...

Full name: System.IO.Directory
Directory.CreateDirectory(path: string) : DirectoryInfo
Directory.CreateDirectory(path: string, directorySecurity: System.Security.AccessControl.DirectorySecurity) : DirectoryInfo
val resourceNames : string []
Assembly.GetManifestResourceNames() : string []
val n : string
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val x : string
System.String.EndsWith(value: string) : bool
System.String.EndsWith(value: string, comparisonType: System.StringComparison) : bool
System.String.EndsWith(value: string, ignoreCase: bool, culture: System.Globalization.CultureInfo) : bool
val stream : Stream
Assembly.GetManifestResourceStream(name: string) : Stream
Assembly.GetManifestResourceStream(type: System.Type, name: string) : Stream
val rm : ResourceReader
Multiple items
type ResourceReader =
  new : fileName:string -> ResourceReader + 1 overload
  member Close : unit -> unit
  member Dispose : unit -> unit
  member GetEnumerator : unit -> IDictionaryEnumerator
  member GetResourceData : resourceName:string * resourceType:string * resourceData:byte[] -> unit

Full name: System.Resources.ResourceReader

--------------------
ResourceReader(fileName: string) : unit
ResourceReader(stream: Stream) : unit
val entry : DictionaryEntry
(extension) IEnumerable.Cast<'TResult>() : Generic.IEnumerable<'TResult>
Multiple items
type DictionaryEntry =
  struct
    new : key:obj * value:obj -> DictionaryEntry
    member Key : obj with get, set
    member Value : obj with get, set
  end

Full name: System.Collections.DictionaryEntry

--------------------
DictionaryEntry()
DictionaryEntry(key: obj, value: obj) : unit
property DictionaryEntry.Value: obj
Multiple items
type Bitmap =
  inherit Image
  new : filename:string -> Bitmap + 11 overloads
  member Clone : rect:Rectangle * format:PixelFormat -> Bitmap + 1 overload
  member GetHbitmap : unit -> nativeint + 1 overload
  member GetHicon : unit -> nativeint
  member GetPixel : x:int * y:int -> Color
  member LockBits : rect:Rectangle * flags:ImageLockMode * format:PixelFormat -> BitmapData + 1 overload
  member MakeTransparent : unit -> unit + 1 overload
  member SetPixel : x:int * y:int * color:Color -> unit
  member SetResolution : xDpi:float32 * yDpi:float32 -> unit
  member UnlockBits : bitmapdata:BitmapData -> unit
  ...

Full name: System.Drawing.Bitmap

--------------------
Bitmap(filename: string) : unit
   (+0 other overloads)
Bitmap(stream: Stream) : unit
   (+0 other overloads)
Bitmap(original: Image) : unit
   (+0 other overloads)
Bitmap(filename: string, useIcm: bool) : unit
   (+0 other overloads)
Bitmap(type: System.Type, resource: string) : unit
   (+0 other overloads)
Bitmap(stream: Stream, useIcm: bool) : unit
   (+0 other overloads)
Bitmap(width: int, height: int) : unit
   (+0 other overloads)
Bitmap(original: Image, newSize: Size) : unit
   (+0 other overloads)
Bitmap(width: int, height: int, format: PixelFormat) : unit
   (+0 other overloads)
Bitmap(width: int, height: int, g: Graphics) : unit
   (+0 other overloads)
val bitmap : Bitmap
val filename : string
property DictionaryEntry.Key: obj
System.Object.ToString() : string
property Image.RawFormat: ImageFormat
val file : FileInfo
Multiple items
type FileInfo =
  inherit FileSystemInfo
  new : fileName:string -> FileInfo
  member AppendText : unit -> StreamWriter
  member CopyTo : destFileName:string -> FileInfo + 1 overload
  member Create : unit -> FileStream
  member CreateText : unit -> StreamWriter
  member Decrypt : unit -> unit
  member Delete : unit -> unit
  member Directory : DirectoryInfo
  member DirectoryName : string
  member Encrypt : unit -> unit
  ...

Full name: System.IO.FileInfo

--------------------
FileInfo(fileName: string) : unit
property FileInfo.Directory: DirectoryInfo
DirectoryInfo.Create() : unit
DirectoryInfo.Create(directorySecurity: System.Security.AccessControl.DirectorySecurity) : unit
val image : Bitmap
Image.Save(filename: string) : unit
Image.Save(stream: Stream, format: ImageFormat) : unit
Image.Save(filename: string, format: ImageFormat) : unit
Image.Save(stream: Stream, encoder: ImageCodecInfo, encoderParams: EncoderParameters) : unit
Image.Save(filename: string, encoder: ImageCodecInfo, encoderParams: EncoderParameters) : unit
val e : exn
System.Exception.ToString() : string
System.Object.GetType() : System.Type
val loadAllAssemblies : folder:string -> seq<Assembly>

Full name: Script.loadAllAssemblies
val folder : string
val folder : DirectoryInfo
Multiple items
type DirectoryInfo =
  inherit FileSystemInfo
  new : path:string -> DirectoryInfo
  member Create : unit -> unit + 1 overload
  member CreateSubdirectory : path:string -> DirectoryInfo + 1 overload
  member Delete : unit -> unit + 1 overload
  member EnumerateDirectories : unit -> IEnumerable<DirectoryInfo> + 2 overloads
  member EnumerateFileSystemInfos : unit -> IEnumerable<FileSystemInfo> + 2 overloads
  member EnumerateFiles : unit -> IEnumerable<FileInfo> + 2 overloads
  member Exists : bool
  member GetAccessControl : unit -> DirectorySecurity + 1 overload
  member GetDirectories : unit -> DirectoryInfo[] + 2 overloads
  ...

Full name: System.IO.DirectoryInfo

--------------------
DirectoryInfo(path: string) : unit
val files : seq<FileInfo>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
DirectoryInfo.GetFiles() : FileInfo []
DirectoryInfo.GetFiles(searchPattern: string) : FileInfo []
DirectoryInfo.GetFiles(searchPattern: string, searchOption: SearchOption) : FileInfo []
val tryLoadAssembly : (string -> Assembly option)
val f : string
union case Option.Some: Value: 'T -> Option<'T>
Assembly.LoadFile(path: string) : Assembly
union case Option.None: Option<'T>
val f : FileInfo
val a : Assembly option
property FileSystemInfo.FullName: string
property Option.IsSome: bool
property Option.Value: Assembly
Raw view New version

More information

Link:http://fssnip.net/pT
Posted:2 years ago
Author:Gauthier Segay
Tags: system.resources