メモ
- 用紙のスキャンデータ(白黒2値, 解像度600dpi)を mspaint.exe で編集し、モノクロビットマップで保存する場合を想定
- ビットマップだとファイルサイズが大きいのでpngファイルで保存
using namespace System.Drawing
using namespace System.Drawing.Imaging
using namespace System.IO
Set-StrictMode -Version Latest
Add-Type -AssemblyName System.Drawing
$VerbosePreference = "Continue"
Write-Verbose "`n$($MyInvocation.MyCommand.Name)`n`n"
if ($args.Length -eq 0) {
Write-Verbose '$args.Length -eq 0'
throw
}
Add-Type -UsingNamespace System.Management.Automation `
-Namespace MyNS -Name Array -MemberDefinition @'
public static void Sort(PSObject[] obj, string propertyName) {
System.Array.Sort(obj, (x, y) => StrCmpLogicalW(
x.Properties[propertyName].Value.ToString(),
y.Properties[propertyName].Value.ToString()
));
}
[DllImport(
"Shlwapi.dll", CharSet = CharSet.Unicode, EntryPoint = "StrCmpLogicalW"
)]
private static extern int StrCmpLogicalW(
[MarshalAs(UnmanagedType.LPWStr)] string psz1,
[MarshalAs(UnmanagedType.LPWStr)] string psz2
);
'@
$argPSObjs = [PSObject[]]@($args.ForEach({
[PSCustomObject]@{FullName = [string]$_}
}))
[MyNS.Array]::Sort($argPSObjs, "FullName")
$argsSort = [string[]]@($argPSObjs.FullName)
for ($i = 0; $i -lt $argsSort.Length; $i++) {
$pathBmpOrTiff = $argsSort[$i]
$pathPng = [Path]::ChangeExtension($pathBmpOrTiff, ".png")
try {
$bmp = [Bitmap]::new($pathBmpOrTiff)
}
catch {
throw $_
}
$str = [PSCustomObject]@{
Name = [Path]::GetFileName($pathBmpOrTiff)
ColorDepth = [Image]::GetPixelFormatSize($bmp.PixelFormat)
DpiX = $bmp.HorizontalResolution
DpiY = $bmp.VerticalResolution
Width = $bmp.Width
Height = $bmp.Height
WidthMM = $bmp.Width / $bmp.HorizontalResolution * 25.4
HeightMM = $bmp.Height / $bmp.VerticalResolution * 25.4
} | Format-List | Out-String -Width 80
Write-Verbose ("`n" +
(((($str -split "`n") -replace "^\s+$", "") -ne "") -join "`n") +
"`n`n")
if ($bmp.PixelFormat -ne [PixelFormat]::Format1bppIndexed) {
$bmp.Dispose()
Write-Verbose "`nPixelFormat が Format1bppIndexed ではありません"
throw
}
if (Test-Path -LiteralPath $pathPng) {
$bmp.Dispose()
Write-Verbose "`n既にpngファイルが存在します`n$pathPng"
throw
}
$bmp.RotateFlip([RotateFlipType]::RotateNoneFlipNone)
$bmp.SetResolution(600, 600)
$bmp.Save($pathPng, [ImageFormat]::Png)
$bmp.Dispose()
}