メモ
Clipboard.GetImage
メソッドで取得したImage
から新しいBitmap
を作成
- 正の整数を8で割ったときの商を求めるため
-shr 3
を使用
- 普通に手作業でクリップボードの画像をペイントに貼り付けてモノクロビットマップとして保存したほうが早い
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
$VerbosePreference = [System.Management.Automation.ActionPreference]::Continue
Set-StrictMode -Version Latest
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
. {
$bmpClip = [System.Drawing.Bitmap][System.Windows.Forms.Clipboard]::GetImage()
if ($null -eq $bmpClip) {
Write-Verbose '$null -eq $bmpClip'
return
}
$bmpNew = [System.Drawing.Bitmap]::new($bmpClip)
if ($bmpNew.PixelFormat -ne [System.Drawing.Imaging.PixelFormat]::Format32bppArgb) {
Write-Verbose "PixelFormat: $($bmpNew.PixelFormat)"
Write-Verbose "PixelFormat が Format32bppArgb ではありません"
$bmpClip.Dispose()
$bmpNew.Dispose()
return
}
$bmpBW = [System.Drawing.Bitmap]::new(
$bmpNew.Width, $bmpNew.Height,
[System.Drawing.Imaging.PixelFormat]::Format1bppIndexed
)
$bmpBW.SetResolution($bmpNew.HorizontalResolution, $bmpNew.VerticalResolution)
$dataNew = [System.Drawing.Imaging.BitmapData]$bmpNew.LockBits(
[System.Drawing.Rectangle]::new(0, 0, $bmpNew.Width, $bmpNew.Height),
[System.Drawing.Imaging.ImageLockMode]::ReadOnly, $bmpNew.PixelFormat
)
$dataBW = [System.Drawing.Imaging.BitmapData]$bmpBW.LockBits(
[System.Drawing.Rectangle]::new(0, 0, $bmpBW.Width, $bmpBW.Height),
[System.Drawing.Imaging.ImageLockMode]::WriteOnly, $bmpBW.PixelFormat
)
$arrNewByte = [byte[]]::new($dataNew.Stride * $dataNew.Height)
[System.Runtime.InteropServices.Marshal]::Copy($dataNew.Scan0, $arrNewByte, 0, $arrNewByte.Length)
$arrBWBit = [System.Collections.BitArray]::new(8 * $dataBW.Stride * $dataBW.Height)
for ($y = 0; $y -lt $dataNew.Height; $y++) {
$numBytesNewY = $dataNew.Stride * $y
$numBitsBWY = 8 * $dataBW.Stride * $y
for ($x = 0; $x -lt $dataNew.Width; $x++) {
$numBytesNew = $numBytesNewY + 4 * $x
$b = $arrNewByte[$numBytesNew + 0]
$g = $arrNewByte[$numBytesNew + 1]
$r = $arrNewByte[$numBytesNew + 2]
$a = $arrNewByte[$numBytesNew + 3]
if (($x -eq 0) -and ($y -eq 0)) {
Write-Host "$("{0:000}" -f $x), $("{0:000}" -f $y) : " -ForegroundColor Yellow -NoNewline
Write-Host "$a, " -ForegroundColor Gray -NoNewline
Write-Host "$r, " -ForegroundColor Red -NoNewline
Write-Host "$g, " -ForegroundColor Green -NoNewline
Write-Host "$b" -ForegroundColor Blue
}
$numBitsBW = $numBitsBWY + 8 * ($x -shr 3) + (7 - ($x % 8))
$arrBWBit[$numBitsBW] = ($r -eq 255) -and ($g -eq 255) -and ($b -eq 255)
}
}
$arrBWByte = [byte[]]::new($dataBW.Stride * $dataBW.Height)
$arrBWBit.CopyTo($arrBWByte, 0)
[System.Runtime.InteropServices.Marshal]::Copy($arrBWByte, 0, $dataBW.Scan0, $arrBWByte.Length)
$arrBWByte = $null
$bmpNew.UnlockBits($dataNew)
$bmpBW.UnlockBits($dataBW)
$bmps = [System.Drawing.Bitmap[]]@($bmpClip, $bmpNew, $bmpBW)
$bmps | Select-Object -Property @(
@{Name = "W"; Expr = {$_.Width}}
@{Name = "H"; Expr = {$_.Height}}
@{Name = "DpiX"; Expr = {$_.HorizontalResolution}}
@{Name = "DpiY"; Expr = {$_.VerticalResolution}}
"PixelFormat"
@{Name = "ColorPaletteEntries"; Expr = {$_.Palette.Entries}}
) | Format-Table -AutoSize
$bmpBW.Save("${env:USERPROFILE}\Desktop\test.png", [System.Drawing.Imaging.ImageFormat]::Png)
$bmps.ForEach({$_.Dispose()})
}