一个转换图片格式的script

该博客为转载内容,转载自https://www.cnblogs.com/hayate/archive/2005/11/02/267167.html ,原内容可能与C#相关。
也是在monad的 team blog上看到的

原code 如下
None.gif# Convert one graphic image to another 
None.gif
param ( $inFile, $type = "gif", $outFile, [switch]$force ) 
None.gif
# 
None.gif# First check to see if our input file exists 

None.gif
$iFile = (resolve-path $inFile -ea silentlycontinue ).path 
None.gif
if ( ! $iFile ) { "File '$inFile' not found, exiting" ; exit } 
None.gif 
None.gif
# now check to see if the output file exists, if force 
None.gif# we will continue, otherwise we exit 

None.gif
if ( ! $outFile )
None.gif{
None.gif    
$tFile = get-item (resolve-path $inFile)
None.gif    
$outFile = $tFile.Fullname -replace ($tFile.Extension + "`$"),".$type"
None.gif}
None.gif
if ( (test-path $outFile-and (! $force) ) { "File '$outFile' exists, exiting"exit } 
None.gif 
None.gif
# make sure we have an encoder before changing anything on 
None.gif# the filesystem 

None.gif
$codecs = [drawing.imaging.ImageCodecInfo]::GetImageEncoders() | 
None.gif        
foreach { $h = @{} } { $h.($_.formatdescription) = $_ } { $h } 
None.gif
$encoder = $codecs.$type 
None.gif
if ( ! $encoder )  
None.gif
None.gif        
"No encoder of type '$type', exiting"
None.gif        
"Available encodings are: " + [string]::Join("", $h.keys
None.gif        
exit 
None.gif
None.gif 
None.gif
# This hoop is needed because resolve-path needs
None.gif# the file to actually exist.  We shouldn't get here 
None.gif# unless the file doesn't exist, or we're going to remove it 
None.gif# by force. 

None.gif
if ( test-path $outFile ) { remove-item $outFile } 
None.gif[void](new
-item -type file $outFile
None.gif
$outFile = (resolve-path $outFile).path 
None.gifremove
-item $outFile 
None.gif 
None.gif
# read the image
None.gif
$image = [system.drawing.image]::FromFile($iFile
None.gif
$image.Save($outFile, $encoder.FormatId)
None.gif
$image.Dispose()
None.gif
# Get the file we just created 
None.gif
get-item $outFile 
None.gif

但是有一点问题 就是使用前必须[reflection.assembly]::LoadWithPartialName("System.drawing"),加在脚本里也行
使用方法 ./script.msh xxxx.bmp [gif] [new.gif] [-force]
如果省略后面三个参数 默认就是用相同文件名 以gif生成,此外还支持TIFF, JPEG, PNG, BMP。-force是当生成文件存在时 强制覆盖

有人评论说,这种脚本太复杂了,一些命令行工具加上一个简单的批处理就够了。确实如此,但是要注意的是,这个的脚本的特点就是不需任何executable,它调用那些只有C#(或别的什么)才能使用的功能,无须编译。
monad team的Bruce Payette说:This illustrates an important principle in Monad - progressive capability. Simple things are simple (e.g. calling a utility program) and complex things are possible (like directly using a .NET library). 这显示了monad的一个重要原则,自我改进的能力。简单的事情固然简单(比如,monad也可以调用可执行文件),复杂的事情也变得可能(直接使用.net类库)。

转载于:https://www.cnblogs.com/hayate/archive/2005/11/02/267167.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值