Chinaunix首页 | 论坛 | 博客
  • 博客访问: 923723
  • 博文数量: 74
  • 博客积分: 10001
  • 博客等级: 上将
  • 技术积分: 2609
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-04 19:54
文章存档

2015年(1)

2009年(2)

2008年(71)

我的朋友

分类: WINDOWS

2008-07-25 22:43:43

 

挖坟是不太好的事情, 不过从PowerShell团队上看到下面的代码, 非常好. 这段代码是由: James W. Truher[MS] 编写. 将其存储成ps1文件.

执行:

PS C:\Documents and Settings\Administrator> .\Convert-Image.ps1 -inFile 01.jpg

GAC    Version        Location
---    -------        --------
True   v2.0.50727     C:\WINDOWS\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll

LastWriteTime : 2008-7-25 22:05:36
Length        : 135580
Name          : 01.gif

 

代码如下:

# Convert one graphic image to another

param ( $inFile, $type = "gif", $outFile, [switch]$force )

[reflection.assembly]::LoadWithPartialName("System.drawing")

#

# 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

阅读(1856) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~