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

Taking screenshot programmatically

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

    Taking screenshot programmatically

    Are there any prewritten methods to take a chart screenshot programmatically? I developed a jpg graphic that plots statistical distributions, but I need to view what the chart was doing at the time for it to make any sense.

    #2
    texasnomad,

    It looks like someone figured out a way to do this. Here is a link to an example of sendmail sending a chart screen shot.



    Im sure you could probably adapt it to your needs.

    Please let me know if I may assist further.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Thanks. I'll take a look.

      Comment


        #4
        It's actually pretty easy. Use "ChartControl" in lieu of "chart and it works fine.

        Code:
        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(chart.ChartPanel.Width, chart.ChartPanel.Height, PixelFormat.Format16bppRgb555);
                        chart.ChartPanel.DrawToBitmap(bmp, chart.ChartPanel.ClientRectangle);

        Comment


          #5
          texasnomad,

          I am happy you have resolved your issue.

          Please don't hesitate to contact us should you require additional assistance.
          Adam P.NinjaTrader Customer Service

          Comment


            #6
            The ChartPanel only updates in a strategy and not in an indicator, per this post. Does anyone have a suggestion of how to make it update in an indicator?

            Comment


              #7
              Originally posted by texasnomad View Post
              The ChartPanel only updates in a strategy and not in an indicator, per this post. Does anyone have a suggestion of how to make it update in an indicator?

              http://www.ninjatrader.com/support/f...ght=ChartPanel
              Are you sure about that?

              I have found that when I try to capture any of the ChartPanel, the Parent, or the ParentForm, regardless, I get an empty window showing the Panel but no candles, or any other content drawn.

              Maybe they changed something in 7.x, in the way that they construct the chart? It is strange though, because even if I change the code to capture the entire screen instead of just the NT chart, it captures the screen, with all windows showing their uncovered content, except for the NT chart, which still shows up as a blank palette. Hm.

              Then again, all the ChartControl functions are unsupported, so there is nothing to cavil about.

              Comment


                #8
                No, it actually seems to be wrong. I took the post's word for it. When I tried it, I keep running into the same problems.

                Comment


                  #9
                  I tried using the chart's Paint and AfterPaint events to get the graphic and draw it to a bmp. I still can't get it working.

                  using System.Drawing.Imaging;
                  using System.Windows.Forms;

                  Code:
                   #region Variables
                          private Bitmap bmp;        
                          #endregion
                  
                          protected override void OnStartUp()
                          {
                              ChartControl.Paint += new PaintEventHandler(OnChartPaint);
                              bmp = new Bitmap(800,600);
                          }
                          
                          protected void OnChartPaint(object sender, PaintEventArgs e)
                          {
                              e.Graphics.DrawImageUnscaled(bmp,0,0);
                              bmp.Save(@"C:\testImage.jpg",ImageFormat.Jpeg);
                          }
                  
                          protected void OnTermination()
                          {
                              ChartControl.Paint -= new PaintEventHandler(OnChartPaint);    
                          }
                  Last edited by texasnomad; 01-03-2012, 05:07 PM.

                  Comment


                    #10
                    Originally posted by texasnomad View Post
                    I tried using the chart's Paint and AfterPaint events to get the graphic and draw it to a bmp. I still can't get it working.

                    using System.Drawing.Imaging;
                    using System.Windows.Forms;

                    Code:
                     #region Variables
                            private Bitmap bmp;        
                            #endregion
                    
                            protected override void OnStartUp()
                            {
                                ChartControl.Paint += new PaintEventHandler(OnChartPaint);
                                bmp = new Bitmap(800,600);
                            }
                            
                            protected void OnChartPaint(object sender, PaintEventArgs e)
                            {
                                e.Graphics.DrawImageUnscaled(bmp,0,0);
                                bmp.Save(@"C:\testImage.jpg",ImageFormat.Jpeg);
                            }
                    
                            protected void OnTermination()
                            {
                                ChartControl.Paint -= new PaintEventHandler(OnChartPaint);    
                            }
                    And when you do that, what does testImage.jpg look like?

                    Comment


                      #11
                      I had the same happening to me. I'm currently using the following to save my chart.

                      #region Global Variables

                      public string DefaultFileLocation = Environment.GetFolderPath(Environment.SpecialFolde r.MyDocuments) + "\\NinjaTrader 7\\charts\\";

                      #endregion

                      #region fnSaveChart

                      [Description("Saving the active chart to a file")]
                      public void fnSaveChart (ChartControl chart, string Description, string Location)
                      {
                      if ( Historical ) return;

                      if ( Location == "<default>" ) Location = DefaultFileLocation;

                      string tFileName = Location
                      + Description + " "
                      + Instrument.FullName.ToString() + " "
                      + "(" + BarsPeriod.Value + " " + BarsPeriod.Id + ") "
                      + Time[0].Year.ToString() + "-"
                      + Time[0].Month.ToString("00") + "-"
                      + Time[0].Day.ToString("00") + " "
                      + Time[0].Hour.ToString("00") + "."
                      + Time[0].Minute.ToString("00") + "."
                      + Time[0].Second.ToString("00") + "."
                      + "jpg";

                      if (chart.ParentForm.WindowState == FormWindowState.Minimized)
                      {
                      Print("[fnSaveChart]Chart form window state must be maximized or normal to capture. Chart not saved.");
                      return;
                      }

                      try
                      {
                      // create the bitmap (empty)
                      Bitmap bitmap = new System.Drawing.Bitmap(chart.ChartPanel.Width, chart.ChartPanel.Height, PixelFormat.Format16bppRgb555);
                      // get bitmap of chart panel
                      chart.ChartPanel.DrawToBitmap(bitmap, chart.ChartPanel.ClientRectangle);

                      bitmap.Save( tFileName, ImageFormat.Jpeg );
                      }

                      catch (Exception ex)
                      {
                      Print ("[fnSaveChart] Exception: " + ex);
                      }

                      return;
                      }

                      #endregion
                      Note that you need to create the "\charts" folder

                      The function is called via
                      fnSaveChart (ChartControl, "End Trade", @"<default>");
                      Which results in something like
                      Attached Files
                      Last edited by Hans B; 01-04-2012, 07:45 AM.

                      Comment


                        #12
                        Interesting. I have pretty much the same code except for escaping Historical. I will have to see if that is the real difference. As I stated above, I get a blank canvas when I use the same technique.

                        Comment


                          #13
                          And when you do that, what does testImage.jpg look like?
                          It's totally black for me, too.
                          Last edited by texasnomad; 01-04-2012, 02:13 PM.

                          Comment


                            #14
                            This is the part that I believe that I left out. I'll have to check on it when I get more time.

                            Code:
                            chart.ChartPanel.DrawToBitmap(bitmap, chart.ChartPanel.ClientRectangle);
                            I was only passing ChartPanel and not the ClientRectangle property directly.

                            Comment


                              #15
                              Sometimes I also use this one. Rather than saving the active chart, it saves the screen (or all of them if you have mutliple).

                              try
                              {
                              int Screens = Screen.AllScreens.Length; // dell8300 2 screen, laptop 1 screen

                              Bitmap bitmap = new Bitmap(Screens * Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); // adjusted for dual screens
                              Graphics graphics = Graphics.FromImage(bitmap as Image);
                              graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);

                              bitmap.Save(tFileName, ImageFormat.Jpeg);
                              }
                              I remember also having empty screens in the beginning, but don't recall the exact reason any more. I think it was because the SaveChart action was called before the NT had completed the build of the screen. Once I programmed it so that it would do the SaveChart at the moment of a trade or at a given time it worked (and hasn't failed me yet). Do note that I'm using it in Strategies only.
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by BarzTrading, Today, 07:25 AM
                              2 responses
                              21 views
                              1 like
                              Last Post BarzTrading  
                              Started by devatechnologies, 04-14-2024, 02:58 PM
                              3 responses
                              20 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by tkaboris, Today, 08:01 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post tkaboris  
                              Started by EB Worx, 04-04-2023, 02:34 AM
                              7 responses
                              163 views
                              0 likes
                              Last Post VFI26
                              by VFI26
                               
                              Started by Mizzouman1, Today, 07:35 AM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X