Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

Trouble with multipage TIFF using powershell - SaveAdd “A Generic error occurred in GDI+.”

$
0
0
I can't seem to get powershell to utilize the SaveAdd method for a multipage tiff. This is part of a larger script that I turned into a test bed to work my way through the Tiff image manipulation. So all I am trying to do here is convert a multipage tiff, into a new multipage tiff with a bit depth of 1 and a resolution of 200p (some baggage from the original script still present).

The only issue I am having is with the SaveAdd() method. I am sure there is something simple I have overlooked. With very few tiff resources for powershell to peruse, most of my sources are C# so there could be something I am missing in platform difference. I attempted using streams, as used here. I have also referenced MSDN's Example, Bob Powell's example, and every search result that seemed related. I have been able to get just about every approach to work for the first frame (page) but I always get the same error on additional pages. Many thanks in advance.

Here is the Code (Save it in a folder with a multipage tif and execute):
Function Test-Image{
[cmdletbinding()]
param(

)
#Image Resolution in DPI
$ImageRes = 200
$ImageQuality = 100 # %
$ImageBitDepth = 1 # ColorDepth
$FileObjects = Get-ChildItem -File -Recurse -Exclude "Test-Image.ps1"
$ImageFiles = ($FileObjects | Where-Object {".bmp",".gif",".jpg",".jpeg",".png",".tif",".tiff",".wmf" -eq $_.Extension -and !$_.PSIsContainer -and $_.fullname -notlike "$DropBoxPath\UnsupportedFiles\*"}).fullname

#Process Image Files
$imageCodec = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() |Where-Object {$_.MimeType -eq "image/tiff"}
$encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(3)
$encoderParams.Param[1] = New-Object System.Drawing.Imaging.EncoderParameter([System.Drawing.Imaging.Encoder]::Quality, $ImageQuality)
$encoderParams.Param[2] = New-Object System.Drawing.Imaging.EncoderParameter([System.Drawing.Imaging.Encoder]::ColorDepth, $ImageBitDepth)

foreach ($ImageFile in $ImageFiles) {

    #Load the Image
    $SourceImage = New-Object System.Drawing.Bitmap($ImageFile) -EV e
    If ($e.Count -eq 0) {
        #Generate Random File Name
        $NewFileName = "$([System.IO.Path]::GetFileNameWithoutExtension([System.IO.Path]::GetRandomFileName())).tif"
        $FrameCount = $SourceImage.GetFrameCount($SourceImage.FrameDimensionslist[0])
        # Make sure we start on the First Frame (Page)
        $SourceImage.SelectActiveFrame($SourceImage.FrameDimensionsList[0],0) | Out-Null
        #Create the New Tiff and load the first Frame.
        $NewImage = New-Object System.Drawing.Bitmap($SourceImage)
        $NewImage.setResolution($ImageRes,$ImageRes)

    for ($FrameIndex=0; $FrameIndex -lt $FrameCount; $FrameIndex++) {
            write-host "Processing $ImageFile Page $($FrameIndex + 1) of $FrameCount" #| Out-File -Append -FilePath $LogFile
            If ($FrameIndex -eq 0) {
                #First Page
                # Set Save Flag to MultiFrame
                $encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter([System.Drawing.Imaging.Encoder]::SaveFlag,"MultiFrame")
                # Save the First Frame (Page)
                $NewImage.Save("$([System.IO.Path]::GetDirectoryName($ImageFile))\$NewFileName",$imageCodec,$encoderParams)
            } Else {
                #Additional Pages
                # Set the Current Frame
                $SourceImage.SelectActiveFrame($SourceImage.FrameDimensionsList[0],$FrameIndex) | Out-Null
                # Switch Save Flag to add Frame
                $encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter([System.Drawing.Imaging.Encoder]::SaveFlag,"FrameDimensionPage")
                $NewImage.SaveAdd($SourceImage,$encoderParams)
            }
        }
        # Cleanup
        $SourceImage.Dispose()
        $encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter([System.Drawing.Imaging.Encoder]::SaveFlag,"Flush")
        $NewImage.SaveAdd($encoderParams)
        $NewImage.Dispose()
        #Remove-Item -Path $ImageFile -Force
    } Else {
        write-host "$ImageFile Could not be opened." | Out-File -Append -FilePath $LogFile
    }
}
#Clear Variable
Remove-Variable ImageFiles
}
Test-Image

Here are the Errors, which I'm assuming are caused by the same problem:
Exception calling "SaveAdd" with "2" argument(s): "A generic error occurred in GDI+."
At V:\jshaw\My Pictures\test\Test-Image.ps1:48 char:6+                     $NewImage.SaveAdd($SourceImage,$encoderParams)+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException+ FullyQualifiedErrorId : ExternalException

Exception calling "SaveAdd" with "1" argument(s): "A generic error occurred in GDI+."
At V:\jshaw\My Pictures\test\Test-Image.ps1:54 char:4+             $NewImage.SaveAdd($encoderParams)+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException+ FullyQualifiedErrorId : ExternalException



Viewing all articles
Browse latest Browse all 8156

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>