Skip to content

Commit 499d03c

Browse files
committed
Added ConvertTo-Image
Added ConvertTo-Png
1 parent 12e272e commit 499d03c

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

media/ConvertTo-Image.ps1

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
}

media/ConvertTo-Png.ps1

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function ConvertTo-Png {
2+
<#
3+
.SYNOPSIS
4+
Converts input to an PNG image
5+
6+
.DESCRIPTION
7+
Converts input file to an PNG image
8+
9+
.EXAMPLE
10+
Get-ChildItem C:\test -Filter "*.gif" | ConvertTo-Png
11+
Converts all gif animations from location 'C:\test' into png files
12+
13+
#>
14+
begin {
15+
Write-Verbose "Conversion start"
16+
}
17+
process {
18+
$input | ConvertTo-Image -Format Png
19+
}
20+
end {
21+
Write-Verbose "Conversion done"
22+
}
23+
}

0 commit comments

Comments
 (0)