QTP中使用DotNetFactory转换图片格式

QTP可以通过DotNetFactory.CreateInstance 来创建.NET自带的类库实例并使用其方法。

以下示例代码展示,如何.ne t中的System.Drawing命名空间中的类来转换图片格式。'Convert image from one format to the another format Public Function ConvertImage(ByVal fromFile, ByVal toFile) Dim oImageLib ' as System.Drawing.Image 'Create the .NET image object Set oImageLib = DotNetFactory.CreateInstance("System.Drawing.Image") Dim oImage 'Get the Image from file Set oImage = oImageLib.FromFile(fromFile) 'Convert the file oImage.Save toFile, GetImageFormat(toFile) 'Destroy the image oImage.Dispose 'Clean up objects Set oImage = Nothing Set oImageLib = Nothing Set oImageFormats = Nothing End Function Public Function GetImageFormat(byVal fileName) 'Get the file extension from the destination file name 'Pass a dummy Nothing parameter to the constructor in place of Guid Set oImageFormats = DotNetFactory.CreateInstance ("System.Drawing.Imaging.ImageFormat","System.Drawing", Nothing) newFileExtension = GetFileExtension(lcase(fileName)) 'Get the image format based on the file name Select Case newFileExtension Case "jpg", "jpeg" Set oNewImgFormat = oImageFormats.Jpeg Case "gif" Set oNewImgFormat = oImageFormats.gif Case "tiff" Set oNewImgFormat = oImageFormats.Tiff Case "wmf" Set oNewImgFormat = oImageFormats.wmf Case "emf" Set oNewImgFormat = oImageFormats.emf Case "exif" Set oNewImgFormat = oImageFormats.Exif Case "bmp" Set oNewImgFormat = oImageFormats.Bmp Case "png" Set oNewImgFormat = oImageFormats.Png Case Else Set oNewImgFormat = oImageFormats.Png End Select Set GetImageFormat = oNewImgFormat Set oImageFormats = Nothing End Function 'Get the file extenstion of a given file name Public Function GetFileExtension(ByVal FileName) lastDot = InStrRev(FileName,".") If lastDot Then GetFileExtension = Mid(FileName, lastDot + 1) Else GetFileExtension = "" End If End Function

 

范例代码:

ConvertImage "c:/test.bmp","c:/test.jpg"

 

运行结果,将c盘下的test.bmp转换成jpg格式。

你可能感兴趣的:(QTP中使用DotNetFactory转换图片格式)