新建对应大小的birmap,填充颜色后将原图贴上去,比如下面的- Add-Type -AssemblyName System.Drawing
- function Cover-Image {
- param(
- [string]$ImagePath,
- # [int]$TopCover,
- # [int]$BottomCover,
- [int]$leftAdd,
- [int]$rightAdd,
- [Drawing.Color]$colorAdd
- )
- $originalImage = [System.Drawing.Image]::FromFile($ImagePath)
- $originalHeight = $originalImage.Height
- $originalWidth = $originalImage.Width
- # $graphics = [System.Drawing.Graphics]::FromImage($originalImage)
- # $whiteBrush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::White)
- # $graphics.FillRectangle($whiteBrush, 0, 0, $originalWidth, $TopCover)
- # $graphics.FillRectangle($whiteBrush, 0, $originalHeight - $BottomCover, $originalWidth, $BottomCover)
- # $coveredImagePath = [System.IO.Path]::ChangeExtension($ImagePath, "covered.jpg")
- # $originalImage.Save($coveredImagePath, [System.Drawing.Imaging.ImageFormat]::Jpeg)
-
- $new_image=[System.Drawing.bitmap]::new($originalWidth+$leftAdd+$rightAdd,$originalHeight,[drawing.imaging.PixelFormat]::Format32bppArgb)
- $img_brush=New-Object System.Drawing.SolidBrush($colorAdd)
- $img_canvas=[System.Drawing.Graphics]::FromImage($new_image)
- $img_canvas.FillRectangle($img_brush,0,0,$new_image.width,$new_image.height)
- $img_canvas.DrawImage($originalImage,$leftAdd,0)
- $new_path = [System.IO.Path]::ChangeExtension($ImagePath, "covered.jpg")
- $new_image.Save($new_path, [System.Drawing.Imaging.ImageFormat]::Jpeg)
- }
-
- # 白色覆盖顶部100像素,底部150像素
- #$topCover = 100
- #$bottomCover = 150
-
- # 用MidnightBlue色,左边添加100像素,右边150像素
- $left_add = 100
- $right_add=150
- $color_add=[System.Drawing.Color]::MidnightBlue
-
- dir *.jpg | ForEach-Object {
- # Cover-Image -ImagePath $_.FullName -TopCover $topCover -BottomCover $bottomCover
- Cover-Image -ImagePath $_.FullName -leftAdd $left_add -rightAdd $right_add -colorAdd $color_add
- }
复制代码
|