I wouldn’t really worry about it, but, the thing I see that’s not disposed is
the $Graphics variable. You can tell it needs to be by looking up the class on
MSDN<https://msdn.microsoft.com/en-us/library/system.drawing.graphics(v=vs.110).aspx>
and seeing that it implements IDiposable.
From a pattern perspective, all the disposes should be wrapped in try {…}
finally {$foo.Dispose()} blocks. In C#/VB you’d use a using {} block but AFAIK
PowerShell doesn’t have an equivalent. For example:
$NewImage = $null
$Graphics = $null
try
{
$NewImage = new-object System.Drawing.Bitmap $NewWidth,$NewHeight
$Graphics = [System.Drawing.Graphics]::FromImage($NewImage)
$Graphics.InterpolationMode =
[System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
$Graphics.DrawImage($OldImage, 0, 0, $NewWidth, $NewHeight)
$ImageFormat = $OldImage.RawFormat
}
finally
{
if ($Graphics -ne $null)
{
$Graphics.Dispose()
}
if ($NewImage -ne $null)
{
$NewImage.Dispose()
}
}
Thanks,
Brian Desmond
(w) 312.625.1438 | (c) 312.731.3132
From: [email protected] [mailto:[email protected]] On
Behalf Of Sean Martin
Sent: Monday, June 19, 2017 11:33 PM
To: [email protected]
Subject: Re: [NTSysADM] Set-ImageSize Help
Hey Brian,
Thanks for the input. An initial batch of about 1500 photos will be run in a
controlled scenario. For ongoing automation, the script will execute weekly and
process an average of 5-10 photos per execution. Can you elaborate on your
comment about the unmanaged resources? Any recommendation on releasing those
resources?
- Sean
On Sat, Jun 17, 2017 at 7:42 AM, Brian Desmond
<[email protected]<mailto:[email protected]>> wrote:
Also note that script does not dispose of all of the unmanaged resources it
uses. For one-offs you’re never going to notice anything, but, if you’re going
to run a ton of images through it at once or host it inside something other
than a one-off PowerShell window, you may see memory and/or handle counts grow
undesirably for the process.
Thanks,
Brian
Thanks,
Brian Desmond
w – 312.625.1438<tel:(312)%20625-1438> | c – 312.731.3132<tel:(312)%20731-3132>
From: [email protected]<mailto:[email protected]>
[mailto:[email protected]<mailto:[email protected]>]
On Behalf Of Sean Martin
Sent: Friday, June 16, 2017 6:03 PM
To: [email protected]<mailto:[email protected]>
Subject: Re: [NTSysADM] Set-ImageSize Help
Ah! That's where I was confused. That did the trick, thanks again for your help!
- Sean
On Fri, Jun 16, 2017 at 2:47 PM, Michael B. Smith
<[email protected]<mailto:[email protected]>> wrote:
Dot sourcing is “more-or-less” equivalent to ipmo.
So you need to separate that into 2 lines.
. .\Set-ImageSize.ps1
Set-ImageSize -Image $Source -Destination $Destination -WidthPx
$Width -HeightPx $Height
From: [email protected]<mailto:[email protected]>
[mailto:[email protected]<mailto:[email protected]>]
On Behalf Of Sean Martin
Sent: Friday, June 16, 2017 5:49 PM
To: [email protected]<mailto:[email protected]>
Subject: Re: [NTSysADM] Set-ImageSize Help
Thanks for the additional insight. Dot sourcing eliminated the error, but it's
not actually performing the resize. I haven't had to use dot sourcing in the
past, are there any particular considerations when passing parameters, or
should it be as simple as the following line:
. .\Set-ImageSize.ps1 $Source -Destination $Destination -WidthPx $Width
-HeightPx $Height
On Fri, Jun 16, 2017 at 12:10 PM, Michael B. Smith
<[email protected]<mailto:[email protected]>> wrote:
Don’t use ipmo. Use dot-sourcing.
From: [email protected]<mailto:[email protected]>
[mailto:[email protected]<mailto:[email protected]>]
On Behalf Of Sean Martin
Sent: Friday, June 16, 2017 3:54 PM
To: [email protected]<mailto:[email protected]>
Subject: Re: [NTSysADM] Set-ImageSize Help
Commenting the line that loads system.windows.forms didn't make a difference.
My script, along with set-image.ps1 are in the same directory. I use
"import-module .\set-imagesize.ps1 within a try/catch block and that succeeds.
The script fails at the point where it runs:
set-imagesize $sourcephoto -destination $destination -Widthpx $Width -HeightPx
$height
The variables are set as follows:
$sourcephoto = <uncpath to file>
$destination = <uncpath to directory>
$width = "96"
$height = "96"
I was able to test interactively by setting the same variables and running the
command. Kind of stumped as to why it won't run within my script.
On Fri, Jun 16, 2017 at 11:10 AM, Michael B. Smith
<[email protected]<mailto:[email protected]>> wrote:
One of the problems is that the script loads system.windows.forms – which is
inherently interactive. However, the script doesn’t appear to use it. So… take
that line out.
Tell me more about your setup for “running your script”?
From: [email protected]<mailto:[email protected]>
[mailto:[email protected]<mailto:[email protected]>]
On Behalf Of Sean Martin
Sent: Friday, June 16, 2017 2:35 PM
To: [email protected]<mailto:[email protected]>
Subject: [NTSysADM] Set-ImageSize Help
Good morning/afternoon,
Looking for a bit of assistance. I'm writing a script to import photos into
Active Directory and part of the process requires that the photos be resized,
so I downloaded this gem:
https://gallery.technet.microsoft.com/scriptcenter/Resize-Image-File-f6dd4a56
Importing and using the set-imagesize cmdlet works just fine when I run through
the process interactively. However, I get the error "The term 'Set-ImageSize'
is not recognized as the name of a cmdlet, function......" when I run my script.
I've verified the "module" imports successfully, so I've been banging my head
on why the cmdlet isn't recognized.
Anyone run into a similar scenario?