Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Take screenshot of chart and replace

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Take screenshot of chart and replace

    Hi,

    I am trying to take screenshot of the chart when I click a button and save it. when I click the same button I want it to take fresh new screenshot and replace the previous screenshot file which saved. At the moment my code is like below:

    private void OnScreenShotButtonClick(object sender, RoutedEventArgs rea)
    {
    System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
    if (button != null)

    Dispatcher.BeginInvoke(new Action(() =>
    {
    if (chart != null && takeShot == true)
    {

    RenderTargetBitmap screenCapture = chart.GetScreenshot(ShareScreenshotType.Chart);
    outputFrame = BitmapFrame.Create(screenCapture);

    if (screenCapture != null)
    {
    PngBitmapEncoder png = new PngBitmapEncoder();
    png.Frames.Add(outputFrame);



    using (Stream stream = File.Create(string.Format(@"{0}\{1}", Core.Globals.UserDataDir, "MyScreenshot.png")))
    png.Save(stream);

    Print("Screenshot saved to " + Core.Globals.UserDataDir);
    takeShot = false;
    }
    }
    }));
    }

    I have taken this code from a support forum post https://ninjatrader.com/support/foru...re?postcount=7 and found the sample called MyscreenTaker.cs

    my code above takes screenshot once, then if I click, it does not work or replace. I will appreciate if anyone can guide me to the right direction please. I am definitely doing something wrong here.

    Thanks in advance.

    #2
    Hello asmmbillah,

    Are you seeing any errors in the log tab of the control center? Can you otherwise upload the whole script where this is being used so I can see the full context?

    One item that I can see is that you are using:
    Code:
    Dispatcher.[B]BeginInvoke[/B](new Action(() =>
    because this sample was taken from the beta forums, this needs changed to:
    Code:
    Dispatcher.[B]InvokeAsync[/B](new Action(() =>

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      Thanks for your correction. Quick question, is it possible to action saving locations depending on chart time period. for example, if the chart is of 5 min, it will save in separate name or location and if it is 10 min it will be different? if yes can you please advise on that please? please note, I want to use the same button click action for both the cases. Thanks.

      Comment


        #4
        Hello asmmbillah,

        Yes you can change the filename/path used in what you provided. The line with the path is the following:
        Code:
        using (Stream stream = File.Create(string.Format(@"{0}\{1}", Core.Globals.UserDataDir, "MyScreenshot.png")))
        You can create whatever path suites your needs and supply a custom file name, it does not need to be a string like this you can also use variables in the path or name. I would suggest looking online at tutorials surrounding working with files in C# as that would help explain ways to manipulate filenames and paths in more detail. If you plan to use custom folders, make sure to make the folders before using them.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Thanks for your help.

          Comment


            #6
            Hi Jesse,

            Following your suggestion above, I have done my research around this.

            Now I am saving the screenshot with the following code:

            using (Stream stream = File.Create(string.Format(@"{0}\{1}.png", Core.Globals.UserDataDir, (BarsPeriod.Value.ToString()+BarsPeriod.BarsPeriod Type.ToString()))))
            png.Save(stream);

            Where for example for a hourly chart it is saving the screenshot as 60Minute; which is absolutely what I wanted. But the problem is when Ninjascript is trying to load the same file with same name with below code, it doesn't load.

            myBitmap = LoadBitmapFromContentFile(System.IO.Path.Combine(s tring.Format(@"{0}\{1}.png", Core.Globals.UserDataDir, (BarsPeriod.Value.ToString()+BarsPeriod.BarsPeriod Type.ToString()))));

            It shows an error "Error on calling 'OnRender' method on bar 6211: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.".

            Then I tried using below code to test where the problem is -
            myBitmap = LoadBitmapFromContentFile(System.IO.Path.Combine(C ore.Globals.UserDataDir, "60Minute.png"));

            But still shows the same error. I don't understand if it is creating the file with (string.Format(@"{0}\{1}.png", Core.Globals.UserDataDir, (BarsPeriod.Value.ToString()+BarsPeriod.BarsPeriod Type.ToString()))))
            png.Save(stream);
            file path, then why it is not reading the file with the same. Or is it different for NinjaTrader 8?

            Please guide how I can load the screenshot with the same file path and avoid this error?

            Thanks in advance.

            Comment


              #7
              Hello asmmbillah,

              Thank you for the reply.

              What is the need to re load the bitmap after saving it? Are you trying to display the image in some way or was that just a test? If that was just a test, please remove that logic and go back to just the saving portion which was not working correctly. After doing so can you attach the file for me to review please?



              I look forward to being of further assistance.

              JesseNinjaTrader Customer Service

              Comment


                #8
                Hi Jesse,

                This was not a test, Please find the script attached. Saving bit was working fine after the correction suggested by you. But I need to save the screenshots depending on the bars period which also saved ok. But when I am trying to load the screenshot, it shows error. Please see the script attached.

                When I click the left button, it saves and click on the right button should have load the screenshot on chart window with this file path.

                Thanks in advance for your assistance.
                Attached Files

                Comment


                  #9
                  Hello asmmbillah,

                  Thank you for providing the file.

                  If you are actually trying to render the screenshot saving it, you can take the logic from the following example:


                  The error you are seeing is related to the loading of the bitmap and the way that is being done. I can see from comparing the other sample that you have some other disposing logic which is not in the working forum sample so that may be one area which relates. I tried the linked sample and see that was working so I would suggest to delete the LoadBitmapFromContentFile you have and replace the OnRenderTargetChanged syntax with the syntax from that sample.

                  I look forward to being of further assistance.
                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Hi Jesse,

                    Thanks for your suggestions.

                    After the correction made, previously when I clicked the left button it would take a screenshot and replace previous screenshot if the name and file path is same. But now it does not. Can you advise why that might be? I haven't changed anything in Onbuttonclick of the left button which takes screenshot.

                    Also just to add to that, when I change the chart barperiod and take screenshot, it takes screenshot and saves. But it does not load and shows that error for corrupt memory.

                    I think, something missing. Please suggest.
                    Last edited by asmmbillah; 08-09-2019, 09:36 AM.

                    Comment


                      #11
                      Hello asmmbillah,

                      That could be if the file is in use, are you seeing any errors in the log?

                      This is not a usual use case so you may need to experiment here a little bit, you could potentially have the file locked due to loading it right after. If that is the case, you can test for this by temporarily disabling the loading of the bitmap to check if saving starts working again.

                      If that is true, you may need to make a toggle which saves the first image as Image1, then when you do it again it saves now as Image2, on a third click you can go back to Image1 etc.. Toggling between saving and loading separate files. That way the saved image is not being used when you go to save it, only after it is saved it would be used and then your on to the alternate named image the next time around.

                      I look forward to being of further assistance.
                      JesseNinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by alifarahani, Today, 09:40 AM
                      6 responses
                      38 views
                      0 likes
                      Last Post alifarahani  
                      Started by Waxavi, Today, 02:10 AM
                      1 response
                      18 views
                      0 likes
                      Last Post NinjaTrader_LuisH  
                      Started by Kaledus, Today, 01:29 PM
                      5 responses
                      15 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Started by Waxavi, Today, 02:00 AM
                      1 response
                      12 views
                      0 likes
                      Last Post NinjaTrader_LuisH  
                      Started by gentlebenthebear, Today, 01:30 AM
                      3 responses
                      17 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Working...
                      X