我的csdn博客地址: http://blog.csdn.net/blizzardlyk

QTP中使用DotNetFactory转换图片格式

上一篇 / 下一篇  2010-08-25 10:59:12 / 个人分类:QTP

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

以下示例代码展示,如何.ne t中的System.Drawing命名空间中的类来转换图片格式。view plaincopy to clipboardprint?
'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 ImageLib = DotNetFactory.CreateInstance("System.Drawing.Image")  
    
  Dim oImage  
    
  'Get the Image from file  
  Set Image =  oImageLib.FromFile(fromFile)  
 
  'Convert the file  
  oImage.Save toFile, GetImageFormat(toFile)  
    
  'Destroy the image  
  oImage.Dispose  
 
  'Clean up objects  
  Set Image = Nothing 
  Set ImageLib = Nothing 
  Set ImageFormats = 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 ImageFormats = 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 NewImgFormat = oImageFormats.Jpeg  
    Case "gif" 
      Set NewImgFormat = oImageFormats.gif  
    Case "tiff" 
      Set NewImgFormat = oImageFormats.Tiff  
    Case "wmf" 
      Set NewImgFormat = oImageFormats.wmf  
    Case "emf" 
      Set NewImgFormat = oImageFormats.emf  
    Case "exif" 
      Set NewImgFormat = oImageFormats.Exif  
    Case "bmp" 
      Set NewImgFormat = oImageFormats.Bmp  
    Case "png" 
      Set NewImgFormat = oImageFormats.Png  
    Case Else 
      Set NewImgFormat = oImageFormats.Png  
  End Select 
 
  Set GetImageFormat = oNewImgFormat  
    
  Set ImageFormats = 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 
'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 ImageLib = DotNetFactory.CreateInstance("System.Drawing.Image")
 
  Dim oImage
 
  'Get the Image from file
  Set Image =  oImageLib.FromFile(fromFile)

  'Convert the file
  oImage.Save toFile, GetImageFormat(toFile)
 
  'Destroy the image
  oImage.Dispose

  'Clean up objects
  Set Image = Nothing
  Set ImageLib = Nothing
  Set ImageFormats = 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 ImageFormats = 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 NewImgFormat = oImageFormats.Jpeg
    Case "gif"
      Set NewImgFormat = oImageFormats.gif
    Case "tiff"
      Set NewImgFormat = oImageFormats.Tiff
    Case "wmf"
      Set NewImgFormat = oImageFormats.wmf
    Case "emf"
      Set NewImgFormat = oImageFormats.emf
    Case "exif"
      Set NewImgFormat = oImageFormats.Exif
    Case "bmp"
      Set NewImgFormat = oImageFormats.Bmp
    Case "png"
      Set NewImgFormat = oImageFormats.Png
    Case Else
      Set NewImgFormat = oImageFormats.Png
  End Select

  Set GetImageFormat = oNewImgFormat
 
  Set ImageFormats = 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格式。


 


TAG: QTP 转换图片格式

 

评分:0

我来说两句

Open Toolbar