powershell 切割图片

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shrekz/article/details/39183503

#读取图片文件
$image=New-Object  System.Drawing.Bitmap("C:\Users\Administrator\Desktop\广寒阁.bmp")
$cjname="test"
$Height = $image.Height;
$Width = $image.Width;
$tileWidth=256
$tileHeight=256
$colCount = [math]::Ceiling($Width / $tileWidth)
$rowCount = [math]::Ceiling($Height / $tileHeight)
[int]$resultrow=0
[int]$resultcol=0
$null=[math]::DivRem($Width,$tileWidth,[ref]$resultcol)
$null=[math]::DivRem($Height,$tileHeight,[ref]$resultrow)

for ($row = 0; $row -lt $rowCount; $row++)
    {
        if($row -eq $rowCount-1 -and $resultrow -ne 0){$tilesHeight=$resultrow}else{$tilesHeight=$tileHeight}
        for ($col = 0; $col -lt $colCount; $col++)
        {   
            if($col -eq $colCount-1 -and $resultcol -ne 0){$tilesWidth=$resultcol}else{$tilesWidth=$tileWidth}
            #指定图片切割的坐标和大小
            $left=$col*$tileWidth
            $top=$row*$tileHeight
            $part=New-Object  System.Drawing.RectangleF($left,$top,$tilesWidth,$tilesHeight)
            #克隆图片
            $s=$image.Clone($part,"Format24bppRgb")#指定图像中每个像素的颜色数据的格式 http://msdn.microsoft.com/zh-cn/library/system.drawing.imaging.pixelformat(v=vs.110).aspx
            $ss="e:\test\$cjname`_tile_$row`_$col.Jpg"
            #保存文件
            $s.save($ss,"Jpeg")#指定图像的文件格式 http://msdn.microsoft.com/zh-cn/library/system.drawing.imaging.imageformat(v=vs.110).aspx
            $s.Dispose()
        }
}

猜你喜欢

转载自blog.csdn.net/shrekz/article/details/39183503