How to use the screenshot image or manipulate it

Best practices, code snippets for common functionality, examples, and guidelines.
Simonez
Posts: 5
Joined: Fri Jul 21, 2017 12:26 pm

How to use the screenshot image or manipulate it

Post by Simonez » Fri Jul 21, 2017 12:46 pm

Good Afternoon,

I have created an script with a screenshot by Ranorex.

I would like to know if there is the way to use this screenshot (where is located) and manipulate the image.
At the end I need to get the image (screenshot) and to send an email with it. Of course, to do that there will be another automation software.

Thank you for your help.

Simonez
Posts: 5
Joined: Fri Jul 21, 2017 12:26 pm

Re: How to use the screenshot image or manipulate it

Post by Simonez » Fri Jul 21, 2017 3:03 pm

Hi all,

this is the code present:

Code: Select all

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading;
using WinForms = System.Windows.Forms;

using Ranorex;
using Ranorex.Core;
using Ranorex.Core.Testing;

namespace XXX
{
    public partial class ScreenShot
    {
        /// <summary>
        /// This method gets called right after the recording has been started.
        /// It can be used to execute recording specific initialization code.
        /// </summary>
        private void Init()
        {
            // Your recording specific initialization code goes here.
        }

        public void Mouse_Click_None2()
        {
            Report.Log(ReportLevel.Info, "Mouse", "Mouse Left Click item 'MozillaFi1.Grouping.None2' at 280;483.", repo.MozillaFi1.Grouping.None2Info);
            repo.MozillaFi1.Grouping.None2.Click("280;483");
        }

    }
 

}

and I found it in past threads:

Code: Select all

private void ReportScreenshot()
{
string oldreportname = Ranorex.Core.Reporting.TestReport.ReportEnvironment.ReportName;
string newreportname = "SCREENSHOTNAME";
Ranorex.Core.Reporting.TestReport.ReportFilename = newreportname;
Report.Screenshot(REPOELEMENT);
Ranorex.Core.Reporting.TestReport.ReportFilename = oldreportname;
}

Code: Select all

Bitmap bmp = Imaging.CaptureImage(repo.YourElement.Self);  
bmp.Save(@"C:\PathToAFolder\CustomName.jpg");  

Code: Select all

private static void screenshot(){
           //Screenshot Start
                     try{
              
                     var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                               Screen.PrimaryScreen.Bounds.Height,
                               PixelFormat.Format32bppArgb);
                  
                  // Create a graphics object from the bitmap.
                  var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
                  
                  // Take the screenshot from the upper left corner to the right bottom corner.
                  gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                                              Screen.PrimaryScreen.Bounds.Y,
                                              0,
                                              0,
                                              Screen.PrimaryScreen.Bounds.Size,
                                              CopyPixelOperation.SourceCopy);
                  
                  // Save the screenshot to the specified path that the user has chosen.
                  bmpScreenshot.Save("Screenshot123.png", ImageFormat.Png);
                  
                     }catch(Exception e){
                        Report.Log(ReportLevel.Error, "Screenshot", e.Message);
                     }
        }
but I am unable to merge with my code.

Thank you for your help.

User avatar
RobinHood42
Posts: 237
Joined: Fri Jan 09, 2015 3:24 pm

Re: How to use the screenshot image or manipulate it

Post by RobinHood42 » Mon Jul 24, 2017 12:25 pm

Hi Simonez,

I'm afraid that it's not clear what is meant by this:
"but I am unable to merge with my code."
Where exactly do you need help? Maybe you could also upload a small sample solution to give us a better understanding of your requirements.

Cheers,
Robin 8)

Simonez
Posts: 5
Joined: Fri Jul 21, 2017 12:26 pm

Re: How to use the screenshot image or manipulate it

Post by Simonez » Tue Jul 25, 2017 10:40 am

Hi Robin,

Thank you for your reply.

I want to capture the screenshot made by Ranorex (Image based) and save it on local folder.

Is there a way to do that?

Thanks

User avatar
Stub
Posts: 173
Joined: Fri Jul 15, 2016 1:35 pm

Re: How to use the screenshot image or manipulate it

Post by Stub » Tue Jul 25, 2017 1:17 pm

I do something along these lines to take a Ranorex object and save an image to HDD:

Code: Select all

Ranorex.Unknown element = ...
Bitmap bmp = Ranorex.Imaging.CaptureDesktopImage(element);
CompressedImage actual_image = new CompressedImage(bmp, ImageFormat.Png);
actual_image.Store(filename);

User avatar
RobinHood42
Posts: 237
Joined: Fri Jan 09, 2015 3:24 pm

Re: How to use the screenshot image or manipulate it

Post by RobinHood42 » Wed Jul 26, 2017 9:03 am

Hi Simonez,

another possiblity would be:
public void SaveScreenshot(RepoItemInfo item)
        {
        	var adapter = item.CreateAdapter<Button>(false);
        	adapter.CaptureCompressedImage().Store(@"C:\temp\image.png");
        }
Hope this helps,
Robin

Simonez
Posts: 5
Joined: Fri Jul 21, 2017 12:26 pm

Re: How to use the screenshot image or manipulate it

Post by Simonez » Wed Jul 26, 2017 4:28 pm

Hi all,

thank you for all the replies.

I wasn't unable to add your code and to find a solution.

@Stub: there were a lot of fields to fill in and I tried them in different way but without result.
@ RobinHood42: I put your code, as well, in ScreenShot.UserCode.cs but it complains about RepoItemInfo item.

Below you will the code of ScreenShot.UserCode.cs. The script automatically creates the method private void Init() even if I overwrite it.

Code: Select all

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading;
using System.IO;
using WinForms = System.Windows.Forms;
using Ranorex;
using Ranorex.Core;
using Ranorex.Core.Testing;

namespace Blue
{
    public partial class ScreenShot
    {
        /// <summary>
        /// This method gets called right after the recording has been started.
        /// It can be used to execute recording specific initialization code.
        /// </summary>
    
       public void SaveScreenshot(RepoItemInfo item)  
        {  
            var adapter = item.CreateAdapter<Button>(false);  
            adapter.CaptureCompressedImage().Store(@"C:\temp\image.png");  
        }

        private void Init()
        {
            // Your recording specific initialization code goes here.
        }  
        
        
        

    }
}
Please let me know if you need more information.

User avatar
RobinHood42
Posts: 237
Joined: Fri Jan 09, 2015 3:24 pm

Re: How to use the screenshot image or manipulate it

Post by RobinHood42 » Thu Jul 27, 2017 9:46 am

Hi,

Please find attached a sample recording, which simply can be added to your current Rx solution.

Cheers,
Robin 8)
Attachments
CaptureImageSolution.zip
(2.82 KiB) Downloaded 40 times

Simonez
Posts: 5
Joined: Fri Jul 21, 2017 12:26 pm

Re: How to use the screenshot image or manipulate it

Post by Simonez » Thu Jul 27, 2017 4:55 pm

RobinHood42 wrote:Hi,

Please find attached a sample recording, which simply can be added to your current Rx solution.

Cheers,
Robin 8)
Dear RobinHood42,

First, thank you very much for your help.
I downloaded your solution. Created a new module named 'Recording1' save and close Ranorex. Copied the files: Recording1.cs and Recording1.UserCode.cs into the folder of the project.
Restarted Ranorex. It works correctly but doesn't leave no one picture.

Do I have done something wrong?

Cheers :)

I hope that I don't bother you.

User avatar
RobinHood42
Posts: 237
Joined: Fri Jan 09, 2015 3:24 pm

Re: How to use the screenshot image or manipulate it

Post by RobinHood42 » Wed Aug 02, 2017 1:51 pm

Hi,

Well, did you check if the user code method is actually called?

Cheers,
Robin