1+ enum ImageFormat { MemoryBMP; Bmp ; Emf; Wmf; Jpeg; Png; Gif; Tiff; Exif; PhotoCD; Icon }
2+
3+ function Get-ImageFormat {
4+ param (
5+ $format
6+ )
7+ switch ($format ) {
8+ " MemoryBMP" { [System.Drawing.Imaging.ImageFormat ]::MemoryBMP }
9+ " Bmp" { [System.Drawing.Imaging.ImageFormat ]::Bmp }
10+ " Emf" { [System.Drawing.Imaging.ImageFormat ]::Emf }
11+ " Wmf" { [System.Drawing.Imaging.ImageFormat ]::Wmf }
12+ " Jpeg" { [System.Drawing.Imaging.ImageFormat ]::Jpeg }
13+ " Png" { [System.Drawing.Imaging.ImageFormat ]::Png }
14+ " Gif" { [System.Drawing.Imaging.ImageFormat ]::Gif }
15+ " Tiff" { [System.Drawing.Imaging.ImageFormat ]::Tiff }
16+ " Exif" { [System.Drawing.Imaging.ImageFormat ]::Exif }
17+ " Icon" { [System.Drawing.Imaging.ImageFormat ]::Icon }
18+ Default { }
19+ }
20+ }
21+
22+ function ConvertTo-Image {
23+ <#
24+ . SYNOPSIS
25+ Converts input to an image
26+
27+ . DESCRIPTION
28+ Converts input file to an image. Supported output types: MemoryBMP, Bmp , Emf, Wmf, Jpeg, Png, Gif, Tiff, Exif, PhotoCD, Icon
29+
30+ . PARAMETER Files
31+ Input files
32+
33+ . PARAMETER Format
34+ Output Image format type
35+
36+ . EXAMPLE
37+ Get-ChildItem C:\test -Filter "*.gif" | ConvertTo-Image -Format Png
38+ Converts all gif animations from location 'C:\test' into png files
39+
40+ #>
41+
42+ [CmdletBinding ()]
43+ param (
44+ [Parameter (Mandatory = $false , Position = 0 , ValueFromPipeline = $true )]
45+ $Files ,
46+ [ImageFormat ]
47+ [Parameter (Mandatory = $true , Position = 1 )]
48+ $Format
49+ )
50+
51+ begin {
52+ Write-Verbose " Conversion start"
53+ }
54+ process {
55+ $formatType = Get-ImageFormat $Format
56+ $fullName = $input.FullName
57+ $image = [System.Drawing.Image ]::FromFile($fullName );
58+ $filePath = [IO.Path ]::ChangeExtension($fullName , " .$ ( $formatType.ToString ().ToLower()) " );
59+ $image.Save ($filePath , $formatType );
60+ $image.Dispose ();
61+ }
62+ end {
63+ Write-Verbose " Conversion done"
64+ }
65+ }
0 commit comments