Skip to content

Commit ff653a6

Browse files
committed
Added Get-ImageCodecInfo
1 parent e414823 commit ff653a6

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function Get-ImageCodecInfo {
2+
[CmdletBinding()]
3+
param (
4+
# [byte[]] or [System.IO.Stream]
5+
[Parameter(Mandatory = $true, Position = 0 )]
6+
$Image
7+
)
8+
9+
begin {
10+
Write-Verbose "Cmdlet Get-ImageCodecInfo - Begin"
11+
}
12+
13+
process {
14+
Write-Verbose "Cmdlet Get-ImageCodecInfo - Process"
15+
16+
if ($Image.PSTypeNames -contains [System.IO.Stream]) {
17+
$Image = $Image.ToArray()
18+
}
19+
20+
[System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | ? {
21+
[byte[]]$pattern = $_.SignaturePatterns | Select-Object -First 1
22+
[byte[]]$header = $Image | Select-Object -First $pattern.Length
23+
[System.Linq.Enumerable]::SequenceEqual([byte[]]$pattern, [byte[]]$header)
24+
} | Select-Object -First 1
25+
}
26+
27+
end {
28+
Write-Verbose "Cmdlet Get-ImageCodecInfo - End"
29+
}
30+
}

Tests/Photo.Shell.Tests.ps1

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Clear-Host
2+
Import-Module -Name Pester -Force
3+
Import-Module .\Photo.Shell\Photo.Shell.psm1 -Force
4+
5+
function Get-TestImage {
6+
param (
7+
[Parameter(Mandatory = $false, Position = 0 )]
8+
[System.Drawing.Imaging.ImageFormat]$Format = [System.Drawing.Imaging.ImageFormat]::Png
9+
)
10+
$bitmap = [System.Drawing.Bitmap]::new(1000, 800, [System.Drawing.Imaging.PixelFormat]::Format32bppPArgb)
11+
$graphics = [System.Drawing.Graphics]::FromImage($bitmap)
12+
$pen1 = [System.Drawing.Pen]::new([System.Drawing.Color]::FromKnownColor([System.Drawing.KnownColor]::Red), 2)
13+
$graphics.DrawEllipse($pen1, 10, 10, 900, 700)
14+
15+
$result = [System.IO.MemoryStream]::new()
16+
$bitmap.Save($result, $Format)
17+
$result.ToArray()
18+
}
19+
20+
Describe 'Photo.Shell.Tests' {
21+
Context "Get-ImageCodecInfo" {
22+
$testImage = Get-TestImage
23+
$codecInfo = Get-ImageCodecInfo $testImage
24+
25+
It "should not be null" {
26+
$codecInfo | Should -Not -BeNullOrEmpty
27+
}
28+
29+
It "should have valid type" {
30+
$codecInfo | Should -BeOfType [System.Drawing.Imaging.ImageCodecInfo]
31+
}
32+
33+
It "given img.<Format>, it returns '<Expected>'" -TestCases @(
34+
@{ Format = [System.Drawing.Imaging.ImageFormat]::Png; Expected = 'image/png' }
35+
@{ Format = [System.Drawing.Imaging.ImageFormat]::Jpeg; Expected = 'image/jpeg' }
36+
@{ Format = [System.Drawing.Imaging.ImageFormat]::Bmp; Expected = 'image/bmp' }
37+
@{ Format = [System.Drawing.Imaging.ImageFormat]::Gif; Expected = 'image/gif' }
38+
) {
39+
param ($Format, $Expected)
40+
$img = Get-TestImage -Format $Format
41+
Get-ImageCodecInfo $img | Select-Object -ExpandProperty MimeType | Should -BeExactly $Expected
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)