Saturday, January 9, 2010

Working With Screen Capture in QTP

Converting PNG to JPG format

I have seen one question in Advanced QTP forum, that is the user needed to capture screenshot in JPG format instead of PNG.
QTP cannot capture snapshot(s) as JPG format, but we have an option to convert it using DOT Net Factory. I hope this code will be useful for all QTP users. So I have posted this in my blog.

Here is the code to convert from PNG to JPG

sImagePath="C:\Documents and Settings\TEMP\Desktop\cap.png"'Make sure to mention your destination path for snapshot

Desktop.CaptureBitmap sImagePath'Capturing as PNG format

Call ConvertJPG(sImagePath)

Function ConvertJPG(sBmpPath)

Set oBmp=DotNetFactory.CreateInstance("System.Drawing.Bitmap","System.Drawing",sBmpPath)
Set oDir=DotNetFactory.CreateInstance("System.IO.Path","mscorlib")
Set oFile=DotNetFactory.CreateInstance("System.IO.File","mscorlib")

oBmp(sBmpPath)

sNewFile = oDir.GetDirectoryName(sBmpPath)
sNewFile = sNewFile&"\" & oDir.GetFileNameWithoutExtension(sBmpPath)
sNewFile = sNewFile&"." &"JPG"
oBmp.Save(sNewFile)'Saving as JPG format

oBmp.Dispose()
oFile.Delete(sBmpPath)'Deleting the previous PNG file.

Set oDir=Nothing
Set oFile=Nothing
Set oBmp=Nothing
End Function

5 comments:

  1. hi this is vinod, i have a query,
    I have a .jpg file which contain fields like (editbox, combobox,etc..) i want to read the field of that jpg file can we do this in QTP, please suggest me if possible ar any other solution .
    thanks
    Vinod

    ReplyDelete
  2. This code merely changes the file extention but internally the save as type is .png only...What is the solution to convert the file type to .jpg as well

    ReplyDelete
  3. Thanks Asiq, this worked a treat for me!!

    ReplyDelete
  4. Hi asiq this (vasanth) how to reduce the size of .png to .jpg file.By the above code only the file extention changes from .png to .jpg.But the size of the file remains same

    ReplyDelete