使用Powershell,我想要完成的是:
我目前正在做什么(不相关的代码除外):
Get-ChildItem | ForEach-Object {
$img = [System.Drawing.Image]::FromFile($_.FullName)
$dimensions = "$($img.Width) x $($img.Height)"
$size = $img.Length
If ($dimensions -eq "1920 x 1080" -and $size -gt 100kb)
{
Copy-Item -Path $sourceDir\$img -Destination $destDir\$img.jpg > $null
}
}
我收到的错误:
Exception calling "FromFile" with "1" argument(s): "Out of memory."
At C:\blahblah
+ $img = [System.Drawing.Image]::FromFile($_.FullName)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : OutOfMemoryException
根据我一直在研究的内容,错误来自于在内存中加载大图像文件。我已经知道流媒体是更好的方式,但我还没有找到一种方法来完成这项工作并且没有多少经验。
我尝试用第二行代替:
$img = [System.Drawing.Image]::FromStream([System.IO.MemoryStream]$_.FullName)
但它咆哮着说我无法改变" C:\ blahblah" type" System.String"的值输入" System.IO.MemoryStream"
答案 0 :(得分:1)
答案 1 :(得分:0)
我已经知道流式传输是更好的方式,但我还没有找到一种方法来完成这项工作并且没有多少经验。
try {
# create filestream from file
$Stream = New-Object System.IO.FileStream -ArgumentList $_.FullName,'Open'
$Image = [System.Drawing.Image]::FromStream($Stream)
$dimensions = "$($Image.Width) x $($Image.Height)"
if($dimensions -eq '1920 x 1080' -and $_.Length -gt 100kb)
{
# Copy-Item
}
}
finally {
$Image,$Stream |ForEach-Object {
# Clean up
$_.Dispose()
}
}