1. 把当前目录的矩形图片变圆形图片,"矩形变圆形.ps1.bat" | | | | | | | | | | | | | | | Add-Type -AssemblyName System.Drawing | | | | Get-ChildItem -Filter *.png | Where-Object { $_ -is [IO.FileInfo] } | ForEach-Object { | | try { | | $_.Name | | $bytes = [IO.File]::ReadAllBytes($_.FullName) | | $memstream = New-Object System.IO.MemoryStream -ArgumentList (, $bytes) | | $bmp = New-Object System.Drawing.Bitmap -ArgumentList ($memstream) | | $width = $bmp.Width | | $height = $bmp.Height | | $a = $width / 2 | | $b = $height / 2 | | $centerX = $width / 2 | | $centerY = $height / 2 | | foreach ($x in 0..($width - 1)) { | | foreach ($y in 0..($height - 1)) { | | | | $equation = [Math]::Pow($x - $centerX, 2) / [Math]::Pow($a, 2) + [Math]::Pow($y - $centerY, 2) / [Math]::Pow($b, 2) | | if ($equation -gt 1) { | | $color = $bmp.GetPixel($x, $y) | | | | $transparent_color = [System.Drawing.Color]::FromArgb(1, $color.R, $color.G, $color.B) | | $bmp.SetPixel($x, $y, $transparent_color) | | } | | } | | } | | $bmp.Save($_.FullName) | | } finally { | | if ($memstream) { $memstream.Close() } | | if ($bmp) { $bmp.Dispose() } | | } | | }COPY |
2.把当前目录的圆形图片变矩形图片,"圆形变矩形.ps1.bat" | | | | | | | | | | | | | | | Add-Type -AssemblyName System.Drawing | | | | Get-ChildItem -Filter *.png | Where-Object { $_ -is [IO.FileInfo] } | ForEach-Object { | | try { | | $_.Name | | $bytes = [IO.File]::ReadAllBytes($_.FullName) | | $memstream = New-Object System.IO.MemoryStream -ArgumentList (, $bytes) | | $bmp = New-Object System.Drawing.Bitmap -ArgumentList ($memstream) | | $width = $bmp.Width | | $height = $bmp.Height | | $a = $width / 2 | | $b = $height / 2 | | $centerX = $width / 2 | | $centerY = $height / 2 | | foreach ($x in 0..($width - 1)) { | | foreach ($y in 0..($height - 1)) { | | $equation = [Math]::Pow($x - $centerX, 2) / [Math]::Pow($a, 2) + [Math]::Pow($y - $centerY, 2) / [Math]::Pow($b, 2) | | if ($equation -gt 1) { | | $color = $bmp.GetPixel($x, $y) | | | | $transparent_color = [System.Drawing.Color]::FromArgb(255, $color.R, $color.G, $color.B) | | $bmp.SetPixel($x, $y, $transparent_color) | | } | | } | | } | | $bmp.Save($_.FullName) | | } finally { | | if ($memstream) { $memstream.Close() } | | if ($bmp) { $bmp.Dispose() } | | } | | }COPY |
|