Sign In
Monad
Monad Technology Blog
Translate This Page
Translate this page
Powered by
Microsoft® Translator
Options
Blog Home
Email Blog Author
Share this
RSS for posts
Atom
RSS for comments
Search
Advanced search options...
Search In:
Everything
Blogs
Forums
People
Groups
Places
Pages
Date range:
All Time
Last Year
Last 6 Months
Last 3 Months
Last Month
Last Week
Last Two Days
Tags
No tags have been created or used yet.
Archive
Archives
April 2006
(10)
March 2006
(7)
February 2006
(8)
January 2006
(7)
December 2005
(6)
November 2005
(8)
October 2005
(2)
September 2005
(5)
August 2005
(7)
Image Conversion
MSDN Blogs
>
Monad
>
Image Conversion
Image Conversion
MSDNArchive
20 Oct 2005 4:30 PM
Comments
8
I needed to convert some images from bmp to gif for a web site I've been working on. I figured that I could script this pretty easy - here's what I came up with. It's a little more general than I needed, and I thought it might be useful to others. Anyway, it's useful to me.
# Convert one graphic image to another
param ( $inFile, $type = "gif", $outFile, [switch]$force )
#
# First check to see if our input file exists
$iFile = (resolve-path $inFile -ea silentlycontinue ).path
if ( ! $iFile ) { "File '$inFile' not found, exiting" ; exit }
# now check to see if the output file exists, if force
# we will continue, otherwise we exit
if ( ! $outFile )
{
$tFile = get-item (resolve-path $inFile)
$outFile = $tFile.Fullname -replace ($tFile.Extension + "`$"),".$type"
}
if ( (test-path $outFile) -and (! $force) ) { "File '$outFile' exists, exiting"; exit }
# make sure we have an encoder before changing anything on
# the filesystem
$codecs = [drawing.imaging.ImageCodecInfo]::GetImageEncoders() |
foreach { $h = @{} } { $h.($_.formatdescription) = $_ } { $h }
$encoder = $codecs.$type
if ( ! $encoder )
{
"No encoder of type '$type', exiting";
"Available encodings are: " + [string]::Join(", ", $h.keys)
exit
}
# This hoop is needed because resolve-path needs
# the file to actually exist. We shouldn't get here
# unless the file doesn't exist, or we're going to remove it
# by force.
if ( test-path $outFile ) { remove-item $outFile }
[void](new-item -type file $outFile)
$outFile = (resolve-path $outFile).path
remove-item $outFile
# read the image
$image = [system.drawing.image]::FromFile($iFile)
$image.Save($outFile, $encoder.FormatId)
$image.Dispose()
# Get the file we just created
get-item $outFile
8 Comments
Blog - Comment List MSDN TechNet
Comments
Loading...
Leave a Comment
Name
Comment
Please add 6 and 1 and type the answer here:
Post