• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Display image when button is clicked

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

    Display image when button is clicked

    Hi,

    First of all, I am trying to show an image on my chart when I click a button and remove that image when I click the button again. I have taken some sample code from this forum and NT8 help guide. Codes are like below:


    protected override void OnBarUpdate()
    {

    }
    private SharpDX.Direct2D1.Bitmap myBitmap = null;

    public override void OnRenderTargetChanged()
    {
    myBitmap = LoadBitmapFromContentFile(System.IO.Path.Combine(N injaTrader.Core.Globals.UserDataDir, "MyImage.png")); // Type the file location depending upon the folder and file location of the file

    }
    private SharpDX.Direct2D1.Bitmap LoadBitmapFromContentFile(string filePath)
    {
    if (RenderTarget == null) return null;
    SharpDX.Direct2D1.Bitmap newBitmap;

    // Neccessary for creating WIC objects.
    SharpDX.WIC.ImagingFactory imagingFactory = new SharpDX.WIC.ImagingFactory();
    SharpDX.IO.NativeFileStream fileStream = new SharpDX.IO.NativeFileStream(filePath, SharpDX.IO.NativeFileMode.Open, SharpDX.IO.NativeFileAccess.Read);

    // Used to read the image source file.
    SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder(imagingFactory, fileStream, SharpDX.WIC.DecodeOptions.CacheOnDemand);

    // Get the first frame of the image.
    SharpDX.WIC.BitmapFrameDecode frame = bitmapDecoder.GetFrame(0);

    // Convert it to a compatible pixel format.
    SharpDX.WIC.FormatConverter converter = new SharpDX.WIC.FormatConverter(imagingFactory);
    converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPRGBA);

    // Create the new Bitmap directly from the FormatConverter.
    if (RenderTarget != null) newBitmap = SharpDX.Direct2D1.Bitmap.FromWicBitmap(RenderTarge t, converter);
    else newBitmap = null;
    SharpDX.Utilities.Dispose(ref bitmapDecoder);
    SharpDX.Utilities.Dispose(ref fileStream);
    SharpDX.Utilities.Dispose(ref imagingFactory);

    return newBitmap;
    }

    private void OnMyMultiChartButtonClick(object sender, RoutedEventArgs rea)
    {
    System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
    if (button != null)
    {
    myBitmap = LoadBitmapFromContentFile(System.IO.Path.Combine(N injaTrader.Core.Globals.UserDataDir, "MyImage.png"));
    float startPoint = ChartPanel.X;
    float endPoint = (ChartPanel.Y + ChartPanel.H / 2);

    //SharpDX.RectangleF rect = new SharpDX.RectangleF(startPoint.X, startPoint.Y, width, height);
    RenderTarget.DrawBitmap(myBitmap, new SharpDX.RectangleF(startPoint, endPoint, (float)ChartPanel.W * 0.4f, (float)ChartPanel.H / 2), 1.0f, SharpDX.Direct2D1.BitmapInterpolationMode.Linear);

    }

    }

    It looks ok to me, but I think, something is missing somewhere, because it's not executing ie, after I click the button it does not display the MyImage.png.

    Secondly, I wanted to remove the image from the chart window.

    Can anyone guide me on this please how to resolve this issue?

    thanks.

    #2
    Hello asmmbillah, thanks for your post.

    SharpDX WIC objects need to be initialized through a file stream. The post linked below from our colleague Jim gives a good example on properly drawing bitmaps with SharpDX.

    https://ninjatrader.com/support/foru...062#post814062

    Please let me know if you have any questions.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi ChrisL,

      Thanks for the info. After reviewing the referred post, I have corrected the code like below:

      protected override void OnBarUpdate() { }


      public override void OnRenderTargetChanged()
      {
      base.OnRenderTargetChanged();
      if (RenderTarget == null || RenderTarget.IsDisposed) return;

      // Dispose all Render dependant resources on RenderTarget change.
      if (myBitmap != null) myBitmap.Dispose();
      if (fileStream != null) fileStream.Dispose();
      if (bitmapDecoder != null) bitmapDecoder.Dispose();
      if (converter != null) converter.Dispose();
      if (frame != null) frame.Dispose();

      // Neccessary for creating WIC objects.
      fileStream = new SharpDX.IO.NativeFileStream(System.IO.Path.Combine (NinjaTrader.Core.Globals.UserDataDir, "MyImage.png"), SharpDX.IO.NativeFileMode.Open, SharpDX.IO.NativeFileAccess.Read);
      // Used to read the image source file.
      bitmapDecoder = new SharpDX.WIC.BitmapDecoder(Core.Globals.WicImagingF actory, fileStream, SharpDX.WIC.DecodeOptions.CacheOnDemand);
      // Get the first frame of the image.
      frame = bitmapDecoder.GetFrame(0);
      // Convert it to a compatible pixel format.
      converter = new SharpDX.WIC.FormatConverter(Core.Globals.WicImagin gFactory);
      converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPRGBA);
      // Create the new Bitmap1 directly from the FormatConverter.
      myBitmap = SharpDX.Direct2D1.Bitmap.FromWicBitmap(RenderTarge t, converter);

      }


      protected override void OnRender(NinjaTrader.Gui.Chart.ChartControl chartControl, NinjaTrader.Gui.Chart.ChartScale chartScale)
      {
      base.OnRender(chartControl, chartScale);
      if (RenderTarget == null || Bars == null || Bars.Instrument == null || myBitmap == null)
      return;
      }
      private void OnMyMultiChartButtonClick(object sender, RoutedEventArgs rea)
      {
      System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
      if (button != null)
      RenderTarget.DrawBitmap(myBitmap, new SharpDX.RectangleF((float)ChartPanel.W / 2, (float)ChartPanel.H / 2, 150, 150), 1.0f, SharpDX.Direct2D1.BitmapInterpolationMode.Linear);
      }
      }

      But still it does not work. When I print for button, it does print the value for button. But though it compiles, it does not work. I think, something I am missing or doing wrong. Please guide to the right direction.

      Thanks.

      Comment


        #4
        Hello asmmbillah, thanks for your reply.

        I can't see your full code, make sure you add the additional disposal logic within State.Terminated from the sample where all the SharpDX resources are cleaned up. You must also call all rendering functions within OnRender because SharpDX has stuff going on before and after OnRender is called.

        I attached a modification of a Chart Trader button example that toggles an image when you click button 1 in chart trader. Replace woodenBox.png with your image file name. Add the attached file within Documents\NinjaTrader 8\bin\Custom\Indicators then compile.

        Best regards.
        Attached Files
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Thanks Chris for the info. I missed to declare a bool. fixed now. Thanks again.

          Comment


            #6
            Hi Chris, if you don't mind me asking, can I change time period of a chart with OnButtonClick() ? if yes, can you please suggest the syntax or give me some links to find that in NT8 guide please?
            Thanks in advance.

            Comment


              #7
              Hello asmmbillah, thanks for your reply.

              That is not possible to do through a script. Any editing of the primary data series must be done through the user interface either by typing the desired data series into the chart (e.g. "1m" for 1 minute) or through the data series menu.
              Chris L.NinjaTrader Customer Service

              Comment


                #8
                Thanks for your reply. In that case, is it possible to switch between bar plots of different data series on the same chart panel while only one plots and rest remain invisible? if yes, can I get some direction towards achieving that please?

                Comment


                  #9
                  Hello asmmbillah, thanks for your reply.

                  This does sound possible. You can use AddDataSeries to get the additional series, then override OnRender to render the bars yourself, rendering only the bars that you want to view. Here is an example of rendering your own bars, the Heiken Ashi indicator:
                  https://ninjatraderecosystem.com/use...heiken-ashi-8/

                  Best regards.

                  The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
                  Chris L.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by GLFX005, Today, 12:22 PM
                  2 responses
                  35 views
                  0 likes
                  Last Post NinjaTrader_Kate  
                  Started by AspenHiker, Today, 08:37 AM
                  1 response
                  9 views
                  0 likes
                  Last Post NinjaTrader_Kate  
                  Started by MartinT, Yesterday, 01:12 PM
                  2 responses
                  14 views
                  0 likes
                  Last Post MartinT
                  by MartinT
                   
                  Started by caveat_lector, Today, 11:58 AM
                  1 response
                  7 views
                  0 likes
                  Last Post NinjaTrader_Heath  
                  Started by Bionian, Today, 11:43 AM
                  1 response
                  8 views
                  0 likes
                  Last Post NinjaTrader_Kate  
                  Working...
                  X