Ranorex Capture Scrrenshot and store in a folder

Ask general questions here.
Tushar
Posts: 7
Joined: Wed Feb 12, 2020 1:40 pm

Ranorex Capture Scrrenshot and store in a folder

Post by Tushar » Wed Feb 19, 2020 11:41 am

I want to capture screenshot of the desktop and store it in given folder. I am using vb.net as -

Code: Select all


	Dim rect As Rectangle
	Dim img As Bitmap =  Ranorex.Imaging.CaptureDesktopImage(rect)
	Dim filename As String = "C:\Ranorex\screensot.jpg"
	img.Save(filename,System.Drawing.Imaging.ImageFormat.Jpeg)

But I am getting error as 'Object reference not set to an instance of an object.' at last line

Can anybody please help!

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Ranorex Capture Scrrenshot and store in a folder

Post by odklizec » Wed Feb 19, 2020 12:02 pm

Hi,

In my opinion, it's because you forgot to define rectangle size? Method CaptureDesktopImage requires either 'rectangle' or repo 'element' parameter. Your code, as it is now, creates 0x0 size image and so the 'save to file' fails. Just define rectangle and all should be fine, e.g. like this...

Code: Select all

Dim rect As New Rectangle(0, 0, 200, 200)
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

Tushar
Posts: 7
Joined: Wed Feb 12, 2020 1:40 pm

Re: Ranorex Capture Scrrenshot and store in a folder

Post by Tushar » Wed Feb 19, 2020 12:30 pm

Hello Guru,

Thanks a lot, it worked!

Is there any way I can capture the screenshot of entire desktop without specifying coordinates?

I have to run the same test on different devices so Test should adapt target device screen coordinates.

Thanks again,
Tushar

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Ranorex Capture Scrrenshot and store in a folder

Post by odklizec » Wed Feb 19, 2020 12:48 pm

Hi,

In my opinion, the easiest way would be to create a repo element, pointing to 'desktop' element. And then simply pass that repo element to CaptureDesktopImage method. Here is an example code:

Code: Select all

Public Sub CaptureDesktop(argument1 As Adapter)
    Dim image As Bitmap = Imaging.CaptureDesktopImage(argument1.Element)
    image.Save("C:\Ranorex\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
End Sub
Eventually, it seems that sending 'null' parameter should instruct method to create a screenshot of the whole desktop. But it throws "A generic error occurred in GDI+" error on my PC ;)
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration